GMail的+ C#+ Web.Config中:发送邮件工程编程,抛出异常使用的Web.Config值 [英] GMail + C# + Web.Config: Send Mail Works Programmatically, Throws Exception Using Web.Config Values

查看:169
本文介绍了GMail的+ C#+ Web.Config中:发送邮件工程编程,抛出异常使用的Web.Config值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下部分在的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>

和以下code片断:

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

是的,后空格了解详情在真的是在那里,它使事情额外的乐趣。事情是,因为他们从开发改为内部分级住我不能硬编码这些值。所以,我不知道为什么它似乎无法与的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 =真正的纲领性覆盖):

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>

*的编辑的编辑*

缺少用户名密码值,并且接受了回应,下面,避让我的问题: 的DefaultCredentials Web.Config中必须

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

推荐答案

我不熟悉这种方式工作,但在Web.config的DefaultCredentials是真实的,你这样做编程时,它设置为false是区别?

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?

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆