2016-01-28 63 views
0

我正在嘗試使用C#SmtpClient類發送電子郵件。 「catch」塊在嘗試「發送」消息後被執行。當我去看異常時,InnerException說「無法連接到遠程服務器」,並且消息顯示「發送郵件失敗」。 StatusCode是「一般故障」。嘗試使用SMTP類發送電子郵件(C#)

我是否缺少某些證書?我是否需要爲SmtpClient或MailMessage的實例設置更多的字段?

下面是代碼:

private void sendEmail(string recepientEmailAddress) { 
     string subjectEmail = "Subject Email"; 
     MailAddress to = new MailAddress("[email protected]"); 
     MailAddress from = new MailAddress("[email protected]"); 

     MailMessage message = new MailMessage(from, to); 
     message.Subject = subjectEmail; 
     message.IsBodyHtml = true; 
     message.Body = "Body example"; 

     try 
     { 
      SmtpClient sc = new SmtpClient(ConfigurationManager.AppSettings["MailServer"].ToString()); 
      sc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; 
      sc.DeliveryMethod = SmtpDeliveryMethod.Network; 
      sc.EnableSsl = true; 
      sc.UseDefaultCredentials = true; 
      sc.Send(message); 
     } 
     catch (Exception e) 
     { 
      Helpers.ErrorLogger.ProcessError(e.Message, e.StackTrace, "RegistrationController", "SendEmail"); 
     } 
    } 
+0

似乎'AppSettings'中指定的'MailServer'無效。 – user700390

+0

缺少用戶名和密碼的傢伙,你嘗試從哪個電子郵件發送 –

+0

@AsafShazar將用戶和密碼附加到我發送的電子郵件的方式是什麼?謝謝。 – tonyleMill

回答

2
private void sendEmail(string recepientEmailAddress) { 
     string subjectEmail = "Subject Email"; 
     MailAddress to = new MailAddress("[email protected]"); 
     MailAddress from = new MailAddress("[email protected]"); 

     MailMessage message = new MailMessage(from, to); 
     message.Subject = subjectEmail; 
     message.IsBodyHtml = true; 
     message.Body = "Body example"; 

     try 
     { 
      SmtpClient sc = new SmtpClient(ConfigurationManager.AppSettings["MailServer"].ToString()); 
      sc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; 
      sc.DeliveryMethod = SmtpDeliveryMethod.Network; 
      sc.EnableSsl = true; 
      sc.Host="smtp.gmail.com" 
      sc.port = 587; 
      sc.UseDefaultCredentials = true; 
      sc.Send(message); 
     } 
     catch (Exception e) 
     { 
      Helpers.ErrorLogger.ProcessError(e.Message, e.StackTrace, "RegistrationController", "SendEmail"); 
     } 
    } 

添加端口(GMAIL:587)

添加主機(smtp.gmail.com

如果不工作嘗試添加用戶名和密碼 從

變更爲
sc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; 

TO

sc.Credentials = new System.Net.NetworkCredential("username","password") 
+0

感謝您的迴應。我發送的「來自」的電子郵件是Outlook。那麼「sc.Host = smtp.outlook.com」會起作用嗎? – tonyleMill

+0

@tonyleMill你是從Outlook發送的意思是什麼?除非您的網站託管在outlook.com上,否則請勿將其從outlook.com發送。 – mason

+0

@mason我只是想了解爲什麼主機和端口上有「gmail」。 – tonyleMill

0
public static bool Send() 
    { 
     try 
     { 
      var fromAddress = new MailAddress("[email protected]"); 
      var toAddress = new MailAddress("[email protected]"); 
      const string subject = "subjects"; 
      const string userName = "serverCredentail"; 
      const string Password = "serverpassword"; 
      const string body = "Body"; 
      var smtp = new SmtpClient 
      { 
       Host = "servernameexample.com", 
       Port = 25, 
       EnableSsl = true, 
       DeliveryMethod = SmtpDeliveryMethod.Network, 
       UseDefaultCredentials = false, 
       Credentials = new NetworkCredential(userName, Password) 
      }; 
      using (var message = new MailMessage(fromAddress, toAddress) 
      { 
       Subject = subject, 
       Body = body 
      }) 
      { 
       smtp.Send(message); 
      } 
     } 
     catch(Exception ex) 
     { 
      return false; 
     } 
     return true; 
    } 

此代碼應工作,我只是測試它。