2013-10-31 41 views
0

我使用SmtpClient.Send Method發送電子郵件,但是當我嘗試發送到一個不存在的電子郵件帳戶時,Send方法不會引發異常,我怎麼能告訴收件人郵件地址有效,或郵件不會到達,或檢測到此問題。當收到不正確的電子郵件地址時,Smtp.Send拋出錯誤

代碼:

  Properties.Settings appSettings = new Properties.Settings(); 
      MailMessage message = new MailMessage(); 

      message.From = new MailAddress(appSettings.senderMail);   
      message.Subject = appSettings.mailsubj; 
      message.Body = appSettings.mailbody; 
      Attachment attach = new Attachment(sOutput); 
      message.Attachments.Add(attach); 

      SmtpClient client = new SmtpClient(appSettings.SMTP); 

      client.SendCompleted += this.SendCompletedCallback; 
      foreach (String clientEmail in clientEmailList) 
      { 
       message.To.Clear(); 
       message.To.Add(clientEmail); 
       //client.Send(message); 

       try 
       { 
        client.Send(message); 
        AddToLog(String.Format("\t{0}{1}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_SUCCESS"), clientEmail)); 
       } 
       catch (SmtpFailedRecipientException ex) 
       { 
        AddToLog(String.Format("\t{0}{1}:\n\t{2}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_ERROR"), clientEmail, ex.Message)); 
       } 
       catch (SmtpException smtp_Ex) 
       { 
        AddToLog(String.Format("\t{0}{1}:\t{2}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_ERROR"), clientEmail, "Cannot connect to SMTP server.")); 
       } 
       catch (Exception ex) 
       { 
        AddToLog(String.Format("\t{0}{1}:\n\t{2}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_ERROR"), clientEmail, ex.Message)); 
       } 
       // client.SendAsync(message, clientEmail); 
      } 
+0

[在.NET中捕獲SMTP錯誤]的可能的重複(http://stackoverflow.com/questions/5052282/capture-smtp-errors-in-net) –

+0

參見http://stackoverflow.com/a/ 16724477/56778 –

回答

3

當SMTP客戶端發送消息時,它並不知道有關電子郵件地址的有效性什麼,如果該地址屬於這個域,從SMTP服務器的域名不同。

只有當您的SMTP服務器嘗試將郵件傳遞給來自其他域的收件人時,纔會知道這一點。通常,在這種情況下,您會收到一封包含錯誤的郵件。

+0

我想顯示和錯誤,在這種情況下,我如何發現,以便我可以顯示一條消息,說明郵件尚未交付 – AlexandruC

+0

您無法使用SMTP同步執行此操作。從SMTP客戶端發送郵件。 – Dennis

+1

@ A.K:[我對一個類似問題的回答](http://stackoverflow.com/a/5052382/56778)在一個更詳細的解釋。 –

相關問題