出于某种原因,既不是接受的答案,也没有其他工作为我“通过Gmail在。net发送电子邮件”。为什么它们不会起作用?

更新:我已经在另一个问题中尝试了所有的答案(接受的和其他的),但没有一个有效。

我只是想知道它是否适用于其他人,否则谷歌可能已经改变了一些东西(以前发生过)。

当我尝试使用SmtpDeliveryMethod的代码段时。网络,我很快收到一个SmtpException发送(消息)。信息是

SMTP服务器需要安全连接,否则客户端未经过身份验证。

服务器的回复是:

5.5.1认证要求在“<——说真的,它到此结束。

更新:

这是我很久以前问过的一个问题,公认的答案是我在不同的项目中使用过很多很多次的代码。

我采用了这篇文章和其他EmailSender项目中的一些想法,在Codeplex上创建了一个EmailSender项目。它是为可测试性而设计的,支持我最喜欢的SMTP服务,如GoDaddy和Gmail。


当前回答

Dim SMTPClientObj As New Net.Mail.SmtpClient
SMTPClientObj.UseDefaultCredentials = False
SMTPClientObj.Credentials = New System.Net.NetworkCredential("myusername@gmail.com", "mypwd")
SMTPClientObj.Host = "smtp.gmail.com"
SMTPClientObj.Port = 587
SMTPClientObj.EnableSsl = True
SMTPClientObj.Send("myusername@gmail.com","yourusername@gmail.com","test","testbody")

如果您得到诸如“SMTP服务器需要安全连接或客户端未经过身份验证。服务器的响应是:5.5.1需要身份验证。要了解更多信息,请访问“在此之前,确保行SMTPClientObj。UseDefaultCredentials = False,这一行应该在smtpclientb . credentials之前。

我确实尝试将这两行反向切换,并返回5.5.1 Authentication Required错误。

其他回答

更改您的gmail密码,再试一次,它应该工作之后。

不知道为什么,但每次你改变你的主机,你必须改变你的密码。

我不确定这需要哪个.NET版本,因为eglasius提到没有匹配的enableSsl设置(我使用。net 4.0,但我怀疑它可以在。net 2.0或更高版本中工作),但这个配置只是为我工作(并且不需要您使用任何编程配置):

<system.net>
  <mailSettings>
    <smtp from="myusername@gmail.com" deliveryMethod="Network">
      <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587" password="password" userName="myusername@gmail.com"/>
    </smtp>
  </mailSettings>
</system.net>

您可能需要先在Gmail帐户上启用POP或IMAP: https://mail.google.com/mail/?shva=1#settings/fwdandpop

我建议先用普通的邮件客户端尝试一下…

@Andres Pompiglio:是的,你必须至少修改一次密码。 这个代码工作得很好:

//Satrt Send Email Function
public string SendMail(string toList, string from, string ccList,
    string subject, string body)
{

    MailMessage message = new MailMessage();
    SmtpClient smtpClient = new SmtpClient();
    string msg = string.Empty;
    try
    {
        MailAddress fromAddress = new MailAddress(from);
        message.From = fromAddress;
        message.To.Add(toList);
        if (ccList != null && ccList != string.Empty)
            message.CC.Add(ccList);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        // We use gmail as our smtp client
        smtpClient.Host = "smtp.gmail.com";   
        smtpClient.Port = 587;
        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential(
            "Your Gmail User Name", "Your Gmail Password");

        smtpClient.Send(message);
        msg = "Successful<BR>";
    }
    catch (Exception ex)
    {
        msg = ex.Message;
    }
    return msg;
}
//End Send Email Function

并且你可以使用以下方法调用函数:

Response.Write(SendMail(recipient Address, "UserName@gmail.com", "ccList if any", "subject", "body"))

如果以上方法都失败了,下面的方法几乎可以肯定是你问题的答案:

我得到了完全相同的错误,原来谷歌的新密码强度测量算法已经改变,认为我目前的密码太弱,并没有告诉我一件事(甚至没有消息或警告)…我是怎么发现的?好吧,我选择更改密码,看看它是否有帮助(尝试了所有其他方法都无济于事),当我更改密码时,它起作用了!

Then, for an experiment, I tried changing my password back to my previous password to see what would happen, and Gmail didn't actually allow me to do this, citing the reason "sorry we cannot allow you to save this change as your chosen password is too weak" and wouldn't let me go back to my old password. I figured from this that it was erroring out because either a) you need to change your password once every x amount of months or b). As I said before, their password strength algorithms changed and therefore the weak password I had was not accepted, even though they did not say anything about this when trying to login ANYWHERE! This (number 2) is the most likely scenario, as my weak password was about 4 months old, and it let me use it in Gmail.

他们对此只字不提是很糟糕的,但这也说得通。因为大多数被劫持的电子邮件都是使用gmail之外的软件登录的,我猜如果你想在gmail环境之外使用gmail,你需要一个更强的密码。

谷歌不再允许“不太安全的应用程序”,所以这对普通Gmail用户来说是不可能的。

2022年5月后谷歌更改。

对于这些更改,我们必须对代码进行以下更改。

First go to Gmail account security. Enable two-factor authentication. Configure the App password in the google account. Use the App password in the application. MailMessage mail = new MailMessage(); mail.To.Add(email.Text.ToString().Trim()); mail.From = new MailAddress("your_Gmail_address"); mail.Subject = "Hello test email"; mail.Body = "<p>hello user<br/> How are you?</p>"; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Port = 587; // 25 465 smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Host = "smtp.gmail.com"; smtp.Credentials = new System.Net.NetworkCredential("your_Gmail_address", "Your_gmail_app_password"); smtp.Send(mail);