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

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

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

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

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

服务器的回复是:

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

更新:

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

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


当前回答

哦……令人惊奇的… 首先,我不知道为什么不能发电子邮件。 但是当我改变了这两行的顺序后,它可以很好地工作。

//(1)
client.UseDefaultCredentials = true;
//(2)
client.Credentials = new System.Net.NetworkCredential("username@gmail.com", "password");

其他回答

我从我的gmail帐户发送电子邮件也遇到了一些问题,这是由于上述几种情况。 以下是我如何让它工作的总结,同时保持它的灵活性:

首先,建立你的GMail账户: 启用IMAP并断言正确的最大消息数(可以在这里执行) 确保你的密码至少有7个字符,并且是强密码(根据谷歌) 确保你不需要先输入验证码。您可以通过从浏览器发送测试电子邮件来做到这一点。 在web中进行更改。config(或app.config,我还没有尝试过,但我认为它也很容易让它在windows应用程序中工作):

<configuration>
    <appSettings>
        <add key="EnableSSLOnMail" value="True"/>   
    </appSettings>

    <!-- other settings --> 

    ...

    <!-- system.net settings -->
    <system.net>
        <mailSettings>
            <smtp from="yourusername@gmail.com" deliveryMethod="Network">
                <network 
                    defaultCredentials="false" 
                    host="smtp.gmail.com" 
                    port="587" 
                    password="stR0ngPassW0rd" 
                    userName="yourusername@gmail.com"
                    />
                <!-- When using .Net 4.0 (or later) add attribute: enableSsl="true" and you're all set-->
            </smtp>
        </mailSettings>
    </system.net>
</configuration>
Add a Class to your project:

Imports System.Net.Mail

Public Class SSLMail

    Public Shared Sub SendMail(ByVal e As System.Web.UI.WebControls.MailMessageEventArgs)

        GetSmtpClient.Send(e.Message)

        'Since the message is sent here, set cancel=true so the original SmtpClient will not try to send the message too:
        e.Cancel = True

    End Sub

    Public Shared Sub SendMail(ByVal Msg As MailMessage)
        GetSmtpClient.Send(Msg)
    End Sub

    Public Shared Function GetSmtpClient() As SmtpClient

        Dim smtp As New Net.Mail.SmtpClient
        'Read EnableSSL setting from web.config
        smtp.EnableSsl = CBool(ConfigurationManager.AppSettings("EnableSSLOnMail"))
        Return smtp
    End Function

End Class

现在,无论何时想要发送电子邮件,都需要调用SSLMail。发送邮件:

例如,在带有PasswordRecovery控件的Page中:

Partial Class RecoverPassword
Inherits System.Web.UI.Page

Protected Sub RecoverPwd_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles RecoverPwd.SendingMail
    e.Message.Bcc.Add("webmaster@example.com")
    SSLMail.SendMail(e)
End Sub

End Class

或者在代码的任何地方都可以调用:

SSLMail.SendMail(New system.Net.Mail.MailMessage("from@from.com","to@to.com", "Subject", "Body"})

我希望这能帮助任何遇到这篇文章的人!(我用的是VB。NET,但是我认为把它转换成任何。NET语言都很简单。)

这些错误有一个或多个原因。

Log in with your Gmail( or any other if ) in your local system. Also check for Less Secure App and Set it to "Turn On" here is the Link for GMAIL. https://www.google.com/settings/security/lesssecureapps check for EnableSsl in your Email code, and also set it to true. smtp.EnableSsl = true; Also check which port are you using currently. 25 is Global, but you can check it for others too like 587. check here. Does all SMTP communication happen over 25? IF YOU ARE ON REMOTE : Check the answer of Vlad Tamas above.

You can also connect via port 465, but due to some limitations of the System.Net.Mail namespace you may have to alter your code. This is because the namespace does not offer the ability to make implicit SSL connections. This is discussed at http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx, and I have supplied an example of how to use the CDO (Collaborative Data Object) in another discussion (GMail SMTP via C# .Net errors on all ports).

解决这个问题的简单步骤:

1)登录你的Gmail

2)导航到此页面https://www.google.com/settings/security/lesssecureapps并设置“打开”

谷歌不再允许“不太安全的应用程序”,所以这对普通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);