Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

authentication - GMail + C# + Web.Config: Send Mail Works Programmatically, Throws Exception Using Web.Config Values

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...