【问题标题】:The SMTP server requires a secure connectionSMTP 服务器需要安全连接
【发布时间】:2020-09-03 14:03:15
【问题描述】:
我正在使用 MVC 框架 4.5 C# 并在 Windows server 2012 R2 上发布我的项目。在该服务器中,当我尝试使用 gmail 发送邮件但它无法发送邮件并给出以下描述。
SMTP 服务器需要安全连接,或者客户端没有
已通过身份验证。服务器响应为:5.5.1 需要身份验证。
我安装了 SMTP 服务和所有相关配置。
相同的邮件配置正在运行我的开发服务器。
【问题讨论】:
标签:
c#
asp.net-mvc-4
smtp
windows-server-2012-r2
smtpclient
【解决方案1】:
这是由于 gmail 的安全检查,所以请按照以下步骤操作。
- 通过远程访问登录生产服务器,并使用您的凭据登录一次 gmail。他们会要求确认,确认一下
【解决方案2】:
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Credentials = new System.Net.NetworkCredential("your email address", "password");
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
// Specify the email sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("your email address");
// Set destinations for the email message.
MailAddress to = new MailAddress(textBox_SedToEmail.Text);
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test email message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Attachments.Add(new Attachment(@"C:\Users\eddie\Pictures\2.jpg"));
// Set the method that is called back when the send operation ends.
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1";
client.SendAsync(message, userState);