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

Cos*_*sta 1 .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);            
        }


    }
Run Code Online (Sandbox Code Playgroud)

Mic*_*urr 7

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

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