2010-01-24 26 views
1

我使用以下工具類發送電子郵件,如果接收者[email protected]不存在或者電子郵件從未以某種原因到達他,我希望我的應用程序能夠被通知。

它只適用於當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 [email protected] 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);    
     } 


    } 

回答

7

SMTP是「盡力而爲」的傳輸方式。一旦將郵件發送到SMTP服務器,您無法真正知道它是否到達目的地(如果沿途出現連接問題,可能需要幾天時間)。您可能會收到來自某個SMTP服務器的電子郵件(就像我之前說的 - 可能在幾天後),但發送失敗的方式。

請注意,即使您向消​​息添加了一個標題,表明您希望確認收到該消息,但可以配置或指示客戶端不發送此類通知。

+0

我不知道是否有某種方式可以使用該協議至少返回一個基於現有地址的錯誤代碼? – IrishChieftain 2010-01-24 19:39:04

+1

@IrishChieftain:有可能您連接的服務器不知道。爲了有機會了解它,您必須連接到SMTP服務器才能訪問該電子郵件地址所在的域。即使如此,也不能保證服務器知道該地址是否有效。 SMTP被設計爲在服務器不知道目的地的所有信息的情況下工作 - 它只需要知道足夠的信息就可以將其傳遞給更應該瞭解的內容。 – 2010-01-24 19:45:09

+0

即使已知地址在特定的SMTP服務器上無效,大多數服務器現在都會返回「OK」。這也是對付垃圾郵件的有限對策。 – JasonTrue 2010-01-25 05:24:22

2

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

+2

和他們的客戶端下載它們(Outlook不會默認,也不是雅虎) – dkackman 2010-01-24 19:25:11

+0

Oho ...這是新的和酷的東西。 – 2010-01-24 19:25:41

+1

即使他們允許HTML,許多電子郵件客戶端現在會阻止外部鏈接的圖像,直到您給予他們明確的權限,這主要歸功於垃圾郵件發送者濫用它們來驗證電子郵件地址目標。 – JasonTrue 2010-01-24 19:26:28

相關問題