服务器不支持C#中的安全连接

时间:2016-08-04 06:35:00

标签: asp.net c#-4.0 smtpclient

我在下面的代码中收到错误“服务器不支持安全连接”。

            SmtpClient smtp = new SmtpClient();
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("*****@gmail.com");
            mail.To.Add(recieverId);
            mail.Subject = "Invoice Copy and Delivery Confirmation for booksap.com Order " + orderno + ". Please Share Feedback.";
            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment(Server.MapPath("OrderMail\\Invoice.pdf"));
            mail.Attachments.Add(attachment);
            MailBody = "We are pleased to inform that the following items in your order " + orderno + " have been placed. This completes your order. Thank you for shopping! ";
            StreamReader reader = new StreamReader(Server.MapPath("~/MailHTMLPage.htm"));
            string readFile = reader.ReadToEnd();
            string myString = "";
            myString = readFile;
            myString = myString.Replace("$$Name$$", ContactPersonName);
            myString = myString.Replace("$$MailBody$$", MailBody);
            mail.Body = myString.ToString();
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = true;
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("*******@gmail.com", "*******");
            smtp.Send(mail);
            mail.Dispose();
            mail = null;

如何解决此问题? 如果我设置

 EnabledSsl = false

它将返回错误: SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.7.1客户端未经过身份验证。

2 个答案:

答案 0 :(得分:0)

该错误消息通常由以下之一引起:

  • 连接设置不正确,例如指定了错误的端口 安全或非安全连接

  • 凭据不正确。我会验证用户名和密码
    组合,以确保凭证是正确的。

如果没问题,我认为你必须设置DeliveryMethod = SmtpDeliveryMethod.Network

试试吧..

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };
using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = body
                     })
{
    smtp.Send(message);
}

ALSO 为不太安全的应用更改帐户访问权限 Go to the "Less secure apps" section in My Account.

尝试选项2:

答案 1 :(得分:0)

@Abhishek是的。您编写的代码会将消息发送到服务器,只需检查您的邮箱(***@gmail.com),但谷歌的smtp客户端阻止您进入他们的域。 您必须使您的gmail不那么安全,您需要允许登录尝试。enter image description here

选中您的邮箱,您应该从gmail域获得此类响应,并且您需要降低帐户的安全性。 enter image description here

我认为它关于验证码和机器人阻止此类登录,知道的人可以对这些事情有所了解。使您的帐户不那么可疑。希望这有帮助