GMail + C# + Web.Config: Send Mail Works Programma

2019-01-22 14:01发布

Given the following section in Web.Config:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="SomeWebsite Admin &lt;someuser@gmail.com&gt;">
            <network host="smtp.gmail.com" port="587" defaultCredentials="true" userName="someuser@gmail.com" password="somepassword" />
        </smtp>
    </mailSettings>
</system.net>

And the following code snippet:

                smtp   = new SmtpClient();

                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential( "someuser@gmail.com", "somepassword" );

                smtp.Send( mailMessage );

The above works fine, however commenting out the programmatic overrides, like this:

                smtp   = new SmtpClient();

                //smtp.Host = "smtp.gmail.com";
                //smtp.Port = 587;
                smtp.EnableSsl = true;
                //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                //smtp.UseDefaultCredentials = false;
                //smtp.Credentials = new NetworkCredential( "someuser@gmail.com", "somepassword" );

                smtp.Send( mailMessage );

fails with:

System.Net.Mail.SmtpException: {"The SMTP server requires a secure connection or the 
client was not authenticated. The server response was: 5.5.1 Authentication Required. 
Learn more at                              "}

Yes, the blank space after the "learn more at" is really there, and it makes things extra fun. Thing is, I can't be hardcoding these values, as they change from development to internal staging to live. So I'm wondering why it appears to fail to work with the Web.Config defaults. I'm figuring I'm missing one critical something-or-other?

* EDIT *

Here's some more information, looking at the values of my smtp object (without programmatic overrides except EnableSSL = true):

Host = smtp.gmail.com (makes sense -- proves I'm actually editing the right Web.Config...
DeliveryMethod = Network
Port = 587
Credentials.UserName = <blank> (Problem???)
Credentials.Password = <blank> (Problem???)
Credentials.Domain = <blank>

* EDIT of the EDIT *

The missing Username and Password values, and the accepted response, below, clued me in to the problem: defaultCredentials in Web.Config must be false.

6条回答
甜甜的少女心
2楼-- · 2019-01-22 14:11

this is a configuration that worked perfectly for me:

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="my-email@gmail.com">
        <network defaultCredentials="false" host="smtp.gmail.com" port="587"  userName="my-username" password="my-password"/>
      </smtp>
    </mailSettings>
  </system.net>

there is no way to add the SSL parameter to the network tag. You will have to set it from code:

var smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.Send(message);

If you don't want to send the emails out using an external smtp, you can configure it to use the localhost and use a desktop email receiver like PaperCut (http://papercut.codeplex.com/). This is the config to achieve that:

<system.net>
    <mailSettings>
      <smtp>
        <network host="127.0.0.1" port="25" />
      </smtp>
    </mailSettings>
  </system.net>

Hope this helps!

查看更多
Ridiculous、
3楼-- · 2019-01-22 14:14

I think this part should be <smtp deliveryMethod="Network" from="SomeWebsite Admin <someuser@gmail.com>"> Like this <smtp deliveryMethod="Network" from="someuser@gmail.com">

查看更多
做自己的国王
4楼-- · 2019-01-22 14:19

If you're using 2-step verification, don't forget to generate an application specific password and use that, not the password you use to log on to Gmail.

(Sorry I can't add this as a comment. Not enough rep at the time of posting this.)

查看更多
够拽才男人
5楼-- · 2019-01-22 14:20

For googlers ending up here, the correct way to do this is:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="SomeWebsite Admin &lt;someuser@gmail.com&gt;">
            <network host="smtp.gmail.com" port="587" enableSsl="true" defaultCredentials="false" userName="someuser@gmail.com" password="somepassword" />
        </smtp>
    </mailSettings>
</system.net>

And your code is then:

           smtp   = new SmtpClient();
           smtp.Send( mailMessage );
查看更多
ら.Afraid
6楼-- · 2019-01-22 14:22

I am not familiar with working this way but in the web.config defaultCredentials is true, you set it to false when doing it programatically is that the difference?

查看更多
【Aperson】
7楼-- · 2019-01-22 14:27

There is no way to specify whether a SSL / Secure connection should be used when defining the mail settings in the web config file.

I'm pretty sure that's why you get an error.

One way to solve that is to create your own mail sending wrapper, and then using it in your application, but you probably already got that :)

It gets more complicated when using the .net membership controls, like forgot password, create user etc. A way to solve that, is to override the SendingMail event, and then set the EnableSSL property of the mailclient to true.

I hope i was able to help :)

查看更多
登录 后发表回答