GMail + C# + Web.Config:以编程方式发送邮件,使用 Web.Config 值引发异常

GMail + C# + Web.Config: Send Mail Works Programmatically, Throws Exception Using Web.Config Values(GMail + C# + Web.Config:以编程方式发送邮件,使用 Web.Config 值引发异常)
本文介绍了GMail + C# + Web.Config:以编程方式发送邮件,使用 Web.Config 值引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于 Web.Config 中的以下部分:

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>

还有下面的代码片段:

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

失败:

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                              "}

是的,learn more at"后面的空格确实存在,它让事情变得更加有趣.问题是,我不能硬编码这些值,因为它们从开发变为内部阶段再到生活.所以我想知道为什么它似乎无法使用 Web.Config 默认值.我在想我错过了一件重要的事情吗?

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?

* 编辑 *

这里有更多信息,查看我的 smtp 对象的值(除了 EnableSSL = true 之外没有编程覆盖):

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 的 EDIT *

缺少的 UsernamePassword 值以及下面接受的响应将我引向了问题:Web.Config 中的 defaultCredentials必须为 false.

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

推荐答案

对于谷歌人来说,正确的做法是:

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>

然后你的代码是:

           smtp   = new SmtpClient();
           smtp.Send( mailMessage );

这篇关于GMail + C# + Web.Config:以编程方式发送邮件,使用 Web.Config 值引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)