开发者

Sending an email in C#

开发者 https://www.devze.com 2023-01-29 00:09 出处:网络
Is it possible to send an email in C# console, without needing and SMTP server? Edit: Why do I need another SMTP server? Can not I use my localhost machine as a server..?

Is it possible to send an email in C# console, without needing and SMTP server?

Edit: Why do I need another SMTP server? Can not I use my localhost machine as a server..?

Edit:

I just need to send an email fr开发者_运维问答om with my domain name, for example abc@mydomain.com

Is that possible? what do I need to do this in my C# program... I do not care about receiving emails, I just care about sending them....

Thanks


You don't have to depend on a local SMTP server if you don't have one. However, you will have to connect to a SMTP server anyway. Here is why.

You must achieve the following steps:

  • Determine what is the mail exchange servers of a given domain.
  • Connect to that mail exchange server and deliver your mail.

Those steps are normally done by your local SMTP server. Another advantage of your local SMTP server is that it will handle its queue and continue to try to deliver your email if it fail.

How to determine the MX records of a give domain.

I suggest you to have a look at this answer. Basically, you have to do a query on a DNS server to get the list of MX records of the domain name of the email address you want to send an email to.

How to connect to a mail exchange server

Well the answer will disappoint you. Exactly like you connect to your local SMTP server. Using the TcpClient, you connect to one of the mail exchange server you got at the previous step on port 25 and start the delivering process using the SMTP protocol.

The trick here is that you must handle multiple MX servers. They are usually listed with a preference. If the first one is unreachable, you try the next one and so on...

That is something your SMTP server can handle for you too.

If you really want to build that logic yourself, please have a look at the DirectSend method of the SmtpClient class of this open source project I'm involved in.


As @TomTom points out, the entire mail infrastructure depends on SMTP. What you can do is to skip the output (relaying) SMTP server and send the message directly to the receiving SMTP server.

To do that you need to create some kind of queuing mechanism (there is no guarantee that the receiving SMTP server can serve you when you try to connect) and that you can look it up.

MX records are entries stored in DNS servers and are used to find SMTP servers. You can find a article here with a MX lookup example: http://www.codeproject.com/KB/IP/dnslookupdotnet.aspx

However, I DO recommend that you install a local SMTP server and let it take care of the above mentioned issues.


Yes, Basically figure out where to sent the email and send it. i.e. a DNS MX lookup for the domain to find out the SMTP server.

Every email needs an SMTP server on the receiving side.


You can use gmail or yahoo SMTP server, if you don't want to install your own.

Before sending mail you first need to authenticate, otherwise sending mail is not going to possible.


You need access to some kind of email server to send your email, and your email will most likely pass through one or more SMTP servers on it's way to the recipient. However, the email server you connect to might let you send the email without using SMTP. For example, Exchange might let you use MAPI or CDO to send emails. Though I think that CDO is not officially supported by .Net and simple MAPI is deprecated in Windows and should not be used. You might be able to use Exchange Web Services as described here: Introducing the Exchange Web Services Managed API 1.0

If you have another email server than Microsoft Exchange, that server might have some kind of API you can use.


Something I do often is to create gmail account and send through that account.

You just need your SmtpClient to connect to the host smtp.gmail.com on port 587 with the username, password, and enableSSL property set to true.

0

精彩评论

暂无评论...
验证码 换一张
取 消