2016-08-02 142 views
0

我在本地計算機上通過visual studio進行調試時提供的NetworkCredential工作正常,但在部署到生產環境時不起作用。NetworkCredential適用於本地計算機,但不適用於生產服務器

我得到以下錯誤:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated

private static void SendEmail(string fromEmailAddress, string[] toEmailAddresses, string[] CC, string subject, string mailBody, bool showMessageInConsole, string smtpServer, bool useDefaultCredentialToSendMail, string mailUserName, string mailPassword) 
{ 
    try 
    { 
     MailMessage mailMessage = new MailMessage(); 
     mailMessage.From = new MailAddress(fromEmailAddress); 

     for (int i = 0; i < toEmailAddresses.Length; i++) 
     { 
      if (!string.IsNullOrEmpty(toEmailAddresses[i])) 
      { 
       mailMessage.To.Add(toEmailAddresses[i]); 
      } 
     } 

     if (CC != null) 
     { 
      for (int i = 0; i < CC.Length; i++) 
      { 
       mailMessage.CC.Add(CC[i].ToString().Trim()); 
      } 
     } 

     mailMessage.Subject = subject; 

     mailMessage.Body = mailBody; 

     SmtpClient smtpClient = new SmtpClient(smtpServer); 

     if (useDefaultCredentialToSendMail) 
     { 
      smtpClient.UseDefaultCredentials = true; 
     } 
     else 
     { 
      smtpClient.Credentials = new System.Net.NetworkCredential(mailUserName, mailPassword); 
     } 
     smtpClient.Send(mailMessage); 
    } 
    catch (System.Exception exception) 
    { 
     logger.Error("SendEmail Exception", exception); 
     MessageHandler.ShowErrorMessage(showMessageInConsole, exception); 
    } 
    finally 
    { 
     //PressAnyKeyToContinue(showMessageInConsole); 
    } 
} 

回答

0

它的工作在生產環境中,當我使用的端口:

SmtpClient smtpClient = new SmtpClient(smtpServer, 25);

相關問題