我在下面的代码中收到错误“服务器不支持安全连接”。
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客户端未经过身份验证。
答案 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)