• <i id='qTcZz'><tr id='qTcZz'><dt id='qTcZz'><q id='qTcZz'><span id='qTcZz'><b id='qTcZz'><form id='qTcZz'><ins id='qTcZz'></ins><ul id='qTcZz'></ul><sub id='qTcZz'></sub></form><legend id='qTcZz'></legend><bdo id='qTcZz'><pre id='qTcZz'><center id='qTcZz'></center></pre></bdo></b><th id='qTcZz'></th></span></q></dt></tr></i><div id='qTcZz'><tfoot id='qTcZz'></tfoot><dl id='qTcZz'><fieldset id='qTcZz'></fieldset></dl></div>
      <bdo id='qTcZz'></bdo><ul id='qTcZz'></ul>
    <tfoot id='qTcZz'></tfoot>

  • <small id='qTcZz'></small><noframes id='qTcZz'>

      1. <legend id='qTcZz'><style id='qTcZz'><dir id='qTcZz'><q id='qTcZz'></q></dir></style></legend>

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

        时间:2023-10-06

        <small id='DTbRm'></small><noframes id='DTbRm'>

        <tfoot id='DTbRm'></tfoot>

          <tbody id='DTbRm'></tbody>

            <legend id='DTbRm'><style id='DTbRm'><dir id='DTbRm'><q id='DTbRm'></q></dir></style></legend>
          • <i id='DTbRm'><tr id='DTbRm'><dt id='DTbRm'><q id='DTbRm'><span id='DTbRm'><b id='DTbRm'><form id='DTbRm'><ins id='DTbRm'></ins><ul id='DTbRm'></ul><sub id='DTbRm'></sub></form><legend id='DTbRm'></legend><bdo id='DTbRm'><pre id='DTbRm'><center id='DTbRm'></center></pre></bdo></b><th id='DTbRm'></th></span></q></dt></tr></i><div id='DTbRm'><tfoot id='DTbRm'></tfoot><dl id='DTbRm'><fieldset id='DTbRm'></fieldset></dl></div>
                  <bdo id='DTbRm'></bdo><ul id='DTbRm'></ul>
                • 本文介绍了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 值引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 localhost SMTP 发送邮件 下一篇:用于开发新windows azure管理门户的框架?

                  相关文章

                  1. <small id='QW7yj'></small><noframes id='QW7yj'>

                  2. <tfoot id='QW7yj'></tfoot>
                  3. <legend id='QW7yj'><style id='QW7yj'><dir id='QW7yj'><q id='QW7yj'></q></dir></style></legend>

                      <bdo id='QW7yj'></bdo><ul id='QW7yj'></ul>

                    <i id='QW7yj'><tr id='QW7yj'><dt id='QW7yj'><q id='QW7yj'><span id='QW7yj'><b id='QW7yj'><form id='QW7yj'><ins id='QW7yj'></ins><ul id='QW7yj'></ul><sub id='QW7yj'></sub></form><legend id='QW7yj'></legend><bdo id='QW7yj'><pre id='QW7yj'><center id='QW7yj'></center></pre></bdo></b><th id='QW7yj'></th></span></q></dt></tr></i><div id='QW7yj'><tfoot id='QW7yj'></tfoot><dl id='QW7yj'><fieldset id='QW7yj'></fieldset></dl></div>