SMTP服务器需要安全连接

时间:2019-06-26 13:33:11

标签: c# asp.net-mvc-4 smtp windows-server-2012-r2 smtpclient

我正在使用MVC Framework 4.5 C#,并在Windows Server 2012 R2上发布我的项目。在该服务器上,当我尝试使用gmail发送邮件但无法发送邮件并给出以下说明时。

  

SMTP服务器需要安全连接,否则客户端将无法连接   服务器响应为:5.5.1需要身份验证。

我安装了SMTP服务以及与此有关的所有配置。

相同的邮件配置正在运行我的开发服务器。

2 个答案:

答案 0 :(得分:0)

这是由于对gmail进行的安全检查,因此请按照以下步骤操作。

  • 通过远程访问登录生产服务器,然后使用您的凭据登录gmail。他们将要求确认,确认

答案 1 :(得分:0)

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);