【问题标题】:MailMessage throws a Configuration ExceptionMailMessage 引发配置异常
【发布时间】:2018-05-29 10:28:37
【问题描述】:

我不明白需要将什么添加到配置文件或其他任何地方。教程没有显示任何其他内容,我找不到答案。请帮忙。 我删除了所有其他代码,只留下了这部分,因为这是问题所在。 代码如下:

using CommandLine; 
using CommandLine.Text; 
using System; 
using System.Configuration; 
using System.Net.Mail; 
using System.Text.RegularExpressions;

namespace SendMail {
    public class SendMail
    {
        public static void Main(string[] args)
        {
            //code that executes just fine and has nothing to do with the MailMessage.
            MailMessage mail = new MailMessage();
            //unreachable code that can't be at fault because it doesn't reach here.
        }
    } 

这是 App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <add key="COMPANYEMAIL" value="*******************" />
  <add key="COMPANYEMAILPASSWORD" value="*******" />
</configuration>

此信息稍后在代码中使用,因此它不会成为问题,因为它没有达到我调用它的地步。它很好地传递了代码的第一部分,然后在尝试执行该行时它会中断。有人请帮助我。

【问题讨论】:

  • 错误信息是什么?

标签: c# exception configuration mailmessage


【解决方案1】:

找到了。我需要将 &lt;add&gt; 元素包装在 &lt;appSettings&gt; 元素中

【讨论】:

    【解决方案2】:

    你应该使用mailsettings配置元素

    <configuration>  
      <system.net>  
        <mailSettings>  
          <smtp deliveryMethod="network">  
            <network  
              host="localhost"  
              port="25"  
              defaultCredentials="true"  
            />  
          </smtp>  
        </mailSettings>  
      </system.net>  
    </configuration>  
    

    Here有一个发送消息的例子

    【讨论】:

    • 我做到了。它没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多