NetworkCredential适用于本地计算机,但不适用于生产服务器

时间:2016-08-02 06:28:16

标签: c# .net-4.0 console

当我通过本地计算机中的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);
    }
}

1 个答案:

答案 0 :(得分:0)

当我使用port时,它在生产环境中工作:

SmtpClient smtpClient = new SmtpClient(smtpServer, 25);