簡體   English   中英

如何知道電子郵件是否無法到達接收者

[英]How to know if Email failed to reach its receivers

我正在使用以下實用程序類發送電子郵件,如果不存在接收者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);            
        }


    }

SMTP是一種“盡力而為”的傳輸方式。 將郵件發送到SMTP服務器后,您就無法真正知道它是否到達目的地(如果途中存在連接問題,則可能需要幾天的時間)。 在傳送失敗的過程中,您可能會從某些SMTP服務器收到一封電子郵件(如我所說-可能幾天后)。

請注意,即使您在消息中添加標頭以表明您希望肯定確認已收到該消息,也可以配置或指示客戶端不發送此類通知。

Web信標是最常用的電子郵件跟蹤工具。 它通常是嵌入在電子郵件html中的1px x 1px的小透明圖像。 通過嵌入,我的意思是一個img標簽-這是一個示例: <img src="www.somedomainhere.com/someimge.gif?[tracking_code]" /> 然后,您可以將每個請求記錄到該圖像。 僅當用戶能夠閱讀HTML格式的消息時,此方法才有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM