[英]Server does not support secure connections in C#
我在下面的代码中收到错误“服务器不支持安全连接”。
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 客户端未通过身份验证。
该错误消息通常是由以下原因之一引起的:
连接设置不正确,例如为安全或非安全连接指定了错误的端口
凭据不正确。 我会验证用户名和密码
组合,以确保凭据正确。
如果可以,我认为您必须设置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);
}
还更改不太安全的应用程序的帐户访问权限转到“我的帐户”中的“不太安全的应用程序”部分。
尝试选项 2:
如果您在任何特定域(公司)工作,那么公司可能有个人代理身份验证,因此您将不得不绕过代理身份验证以防止错误。
首先你需要添加
smtpServer.EnableSsl = false;
然后在您的 web.config 中添加以下代码
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="True" bypassonlocal="True" />
</defaultProxy>
</system.net>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.