开发者

How to confirm that mail has been delivered or not?

开发者 https://www.devze.com 2023-03-28 14:04 出处:网络
Below is my coding, just have a look at it System.Net.Mail.MailMessage oMail = new System.Net.Mail.MailMessage();

Below is my coding, just have a look at it

System.Net.Mail.MailMessage oMail = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
oMail.From = new System.Net.Mail.MailAddress("one@gmail.com");
oMail.To.Add(TextBox1.Text.Trim());
oMail.Su开发者_Python百科bject = "Subject*";
oMail.Body = "Body*";
oMail.IsBodyHtml = true;
smtp.Host = "smtp.sendgrid.net";
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("myusername", "mypassword");
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
smtp.Send(oMail);

Here I need to check whether that mail has been delivered or not.


You can't. Since you use SMTP, in general case, it's impossible to tell whether delivery succeeded or not. Read SMTP specification. Mail is routed while being delivered, so:

  1. There's no guarantee your message is sent as soon as you call smtp.Send().
  2. Since SMTP is routed, you can't be sure that some node on the route won't fail with delivery to uplink.


You can set the DeliveryNotificationOptions property of the MailMessage to OnSuccess.

There's more info on this here: http://msdn.microsoft.com/en-us/library/system.net.mail.deliverynotificationoptions.aspx

and here: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.deliverynotificationoptions.aspx

As has been pointed out in the comments, this method is not 100% reliable. It's just one option.


Here are some best practices to ensure email deliverability:

  • Set up a single no reply address as an actual inbox and then go into the email account using pop3 and look for bounce back messages.
  • Verify the email address is valid before you send it using something like this email validation library:

http://www.kellermansoftware.com/p-37-net-email-validation.aspx


In case somebody stumbles across. Here is my experience on an old Win 2003 server with Exchange. My .Net app uses SMTP to send emails and used a group address as the "From" so any replies would go to the entire group. This is a dispatching operation and we wanted anyone in the group to assist with any replies. We expected any non delivery notices to be sent to the group. It does not happen. I even went into the Message Tracking and saw that Exchange said it sent the notification to the group, but they never got it. Next I went into SMTP Virtual Server Properties and entered the group email address in the "Send copy of Non-Delivery Report to:". Same result, the Message Tracking says it sent it to the group but it was never delivered. Apparently the non delivery notices will only be delivered to an email address of an individual not a group. I changed it to an individual address and it works.


If you don't receive any exceptions in your code or on your SMTP server, you can assume that it has been delivered. That is a problem with e-mail, there is no way to guaranteeing delivery.


If you have access to the server containing the SMTP log, depending on the type of server used and the level of detail used, you might be able to get a much better idea as to the actual success of the delivery.

For example if you are using SmarterMail, you could write a simple parser to read the smtp log and report back to your application. Not that this shouldn't be a blocking operation, as if your email is greylisted, it may take many minutes before it is delivered.

Another option is to create a catch all "noreply" email address, and send your emails with an email-id included in the sender ie noreply-bounces-32432@bounces.mydomain.com and use a pop3 reader to check the inbox for returned emails. Using the email id in the "sender" you can work out which email failed to be sent. You could also do some filtering do determine if its a "unable to deliver" or simply an "out of office" type reply.

Lots of options available, just depends how much sweat you want to put into it.

0

精彩评论

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

关注公众号