错误:SMTP服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.0需要身份验证

时间:2020-04-29 21:07:51

标签: asp.net azure email model-view-controller smtp

发送电子邮件可以在localhost上正常工作,但是可以在运行于azure上的实时网站上发送smtp错误。 SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.7.0需要身份验证。

// Email.cs
public string SendMail(string recieverEmail, string subject, string body)
        {
            try
            {
                string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"];
                string senderPassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"];
                string smtpPort = System.Configuration.ConfigurationManager.AppSettings["SMTPPort"];
                string host = System.Configuration.ConfigurationManager.AppSettings["Host"];

                var sender = new MailAddress(senderEmail, "Petersham");
                var receiver = new MailAddress(recieverEmail, "Receiver");
                var password = senderPassword;
                var smtp = new SmtpClient
                {
                    Host = host,
                    Port = Convert.ToInt16(smtpPort),
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(sender.Address, password)
                };
                using (var mail = new MailMessage(senderEmail, recieverEmail)
                {
                    Subject = subject,
                    Body = body,
                    IsBodyHtml = true,
                })
                {
                    smtp.Send(mail);
                }
                return "sent";

            }
            catch (Exception e)
            {
                return e.ToString();
            }

        }

        public string MessageBody(string title, string paragraph, string footer)
        {
            string body = string.Empty;
            using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/Views/Shared/EmailTemplate.html")))
            {
                body = sr.ReadToEnd();
            }
            //parameters --- set link 
            body = body.Replace("{#TITLE}", title);
            body = body.Replace("{#FooterText}", footer);
            body = body.Replace("{#Paragraph}", paragraph);

            return body;
        }

// Call
Email obj = new Email();
                string paragraph = @"my Paraghraph";
                var emailMsg = obj.SendMail(model.Email, "Go2Eat", obj.MessageBody("Go2Eat Demo Link", paragraph, "Go2Eat"));
                if (emailMsg == "sent")
                {
                    ViewBag.Message = "Please check your email...";
                }
                else
                {
                    ViewBag.Message = "Error: " + emailMsg;
}

// Web.config
<add key="SenderEmail" value="myemail" />
    <add key="SenderPassword" value="mypassword" />
    <add key="SMTPPort" value="587" />
    <add key="Host" value="smtp.gmail.com" />

此代码在本地主机上完全正常运行... 我该怎么办才能解决此错误并发送邮件?

0 个答案:

没有答案