当我通过本地计算机中的visual studio进行调试时,我提供的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 :(得分:0)
当我使用port时,它在生产环境中工作:
SmtpClient smtpClient = new SmtpClient(smtpServer, 25);