繁体   English   中英

NetworkCredential可在本地计算机上工作,但不能在生产服务器上工作

[英]NetworkCredential works in local computer but not production server

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

使用port时,它可以在生产环境中工作:

SmtpClient smtpClient = new SmtpClient(smtpServer, 25);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM