如何知道电子邮件是否无法到达其接收者

时间:2010-01-24 19:14:21

标签: .net asp.net email

我使用以下实用程序类发送电子邮件,如果收件人foo@foo.com不存在或者电子邮件因某种原因从未联系到他,我希望我的应用程序得到通知。

它仅在smtp客户端无法连接到smtp服务器时起作用,在这种情况下我得到一个例外,否则,了解电子邮件是否无法到达客户端的唯一方法是检查客户帐户。

 public static class EmailUtil
    {
        /// <summary>
        /// Fires exception if string is null or empty
        /// </summary>
        /// <param name="param">param value</param>
        /// <param name="paramName">is the parameter name to be shown in the exception message</param>
        private static void CheckStringParam(string parameter, string paramName)
        {
            if (String.IsNullOrEmpty(parameter))
            {
                throw new ArgumentException(String.Format("{0} can't be null or empty", paramName));
            }
        }

        public static void SendEmail(EmailArgument emailArg)
        {
            CheckStringParam(emailArg.FromEmail, "emailArg.FromEmail");
            CheckStringParam(emailArg.Subject, "emailArg.Subject");
            CheckStringParam(emailArg.Body, "emailArg.Body");
            string body = emailArg.Body;

            MailMessage mailMsg = new MailMessage();
            mailMsg.From = new MailAddress(emailArg.FromEmail);

            foreach(string recipient in emailArg.ToEmails)
            {
                if (String.IsNullOrEmpty(recipient))
                {
                    throw new ArgumentException("One of the values in the emailArg.ToEmails array is null or empty");
                }

                mailMsg.To.Add(new MailAddress(recipient));
            }

            mailMsg.IsBodyHtml = emailArg.IsHtml;

            if (emailArg.PutHtmlTags)
            {
                body = String.Format("{0}" + body + "{1}", "<HTML><Body>", "</Body></HTML>");
            }

            mailMsg.Body = body;
            mailMsg.BodyEncoding = emailArg.BodyEncoding;

            // Get client info from the config file , it is tested with Web.config, need to be tested with App.config
            SMTPConfiguration smtpConfig = (SMTPConfiguration)System.Configuration.ConfigurationManager.GetSection("SMTPConfigurationGroup/SMTPServer");

            SmtpClient client = new SmtpClient(smtpConfig.Host, smtpConfig.Port);
            client.EnableSsl = smtpConfig.EnableSSL;
            client.Credentials = new System.Net.NetworkCredential(smtpConfig.Username, smtpConfig.Password);



            // The notifications of failure are sent only to the client email.
            // Exceptions are not guranteed to fire if the Receiver is invalid; for example gmail smtp server will fire exception only when receiver@gmail.com is not there. 
            // Exceptions will be fired if the server itself timeout or does not responding (due to connection or port problem, extra).
            mailMsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            client.Send(mailMsg);            
        }


    }

2 个答案:

答案 0 :(得分:7)

SMTP是一种“尽力而为”的传输方式。一旦将消息发送到SMTP服务器,您就无法确切知道它是否到达目的地(如果在此过程中存在连接问题,则可能需要数天)。您可能会收到一封来自某个SMTP服务器的电子邮件(就像我说的那样 - 可能几天后),传递失败的方式也是如此。

请注意,即使您在邮件中添加标题,表明您希望确认收到邮件,也可以配置或指示客户端不发送此类通知。

答案 1 :(得分:2)

Web beacon是最常用的电子邮件跟踪工具。它通常是一个小的透明图像1px x 1px嵌入在电子邮件的html中。嵌入我的意思是一个img标签 - 这是一个例子:<img src="www.somedomainhere.com/someimge.gif?[tracking_code]" />。然后,您可以将每个请求记录到该图像。 仅当用户能够读取HTML格式化消息时,此方法才有效。