简体   繁体   English

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

[英]GMail + C# + Web.Config: Send Mail Works Programmatically, Throws Exception Using Web.Config Values

Given the following section in 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>

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. 所以我想知道为什么它似乎无法使用Web.Config默认值。 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): 这里有一些更多信息,查看我的smtp对象的值(没有编程覆盖,除了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 . 下面缺少的UsernamePassword值以及接受的响应使我明白了问题:Web.Config中的defaultCredentials必须为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>

And your code is then: 然后你的代码就是:

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

我不熟悉这种方式,但是在web.config中,defaultCredentials为true,你以编程方式将它设置为false是不同的?

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. 无法将SSL参数添加到网络标记。 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/ ). 如果您不想使用外部smtp发送电子邮件,可以将其配置为使用localhost并使用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! 希望这可以帮助!

我认为这部分应该是<smtp deliveryMethod =“Network”from =“SomeWebsite Admin <someuser@gmail.com>”>喜欢这个<smtp deliveryMethod =“Network”from =“someuser@gmail.com”>

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. 如果您使用的是两步验证,请不要忘记生成应用专用密码并使用该密码,而不是用于登录Gmail的密码。

(Sorry I can't add this as a comment. Not enough rep at the time of posting this.) (对不起,我不能将此作为评论添加。发布此帖时没有足够的代表。)

There is no way to specify whether a SSL / Secure connection should be used when defining the mail settings in the web config file. 在Web配置文件中定义邮件设置时,无法指定是否应使用SSL / Secure连接。

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. 使用.net成员资格控件时会变得更加复杂,比如忘记密码,创建用户等。解决这个问题的方法是覆盖SendingMail事件,然后将mailclient的EnableSSL属性设置为true。

I hope i was able to help :) 我希望我能帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM