无法从ASP.NET中的Gmail帐户发送邮件

时间:2013-12-16 09:36:10

标签: c# asp.net email gmail

我正在使用此代码发送邮件

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

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("abc@gmail.com");
    mail.To.Add("xyz@gmail.com");
    mail.IsBodyHtml = true;
    mail.Subject = "Email Sent";
    mail.Body = "Mail Done";
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "123456");
    smtp.EnableSsl = true;
    smtp.Send(mail);
    Label1.Text = "Mail Sent";

我正在使用 abc@gmail.com (一个电子邮件ID)发送电子邮件,邮件会成功发送但是当我使用 pqr@gmail.com 时(另一个邮件ID)邮件发送失败。在本地服务器上“ abc ”& “ pqr ”工作正常。

请帮我解决这个问题。

错误消息

the smtp server requires a secure connection or the client is not authenticated the server response was 5.5.1 authentication requires

3 个答案:

答案 0 :(得分:1)

将您的代码更新为以下内容:

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "123456");
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);

答案 1 :(得分:0)

enableSSl=true代码

之后尝试此行
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

答案 2 :(得分:0)

Gmail添加了一项新的安全功能,不允许使用安全性较低的应用发送电子邮件。您必须更改gmail帐户的设置,以使用Google在以下链接中给出的步骤允许安全性较低的应用访问

https://support.google.com/a/answer/6260879?hl=en

启用安全性较低的应用访问帐户 1.登录到您的Google管理控制台。

  1. 单击安全性>基本设置。

  2. 在“安全性较低的应用程序”下,选择“转到安全性较低的应用程序的设置”。 在子窗口中,选择“允许用户管理对不太安全的应用程序的访问”单选按钮。 设置“允许用户管理对安全性较低的应用程序的访问权限”后,所选组或组织单位内的受影响用户将可以自行打开或关闭对安全性较低的应用程序的访问权限。

相关问题