无法阅读< mailSettings>从app.config文件中,为什么???? [英] Can't read <mailSettings> from app.config file, why??????

查看:78
本文介绍了无法阅读< mailSettings>从app.config文件中,为什么????的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常识告诉我它应该可以工作.代码可以编译,但是主机始终为空.无论我如何尝试,都无法从app.config文件中获取任何数据.我有一个类库,我正在尝试构建一个电子邮件组件,该组件使用app.config文件中的mailSettings发送电子邮件.我正在使用VS 2005.

< 配置 >

< system.net >

< mailSettings >

< smtp >

< 网络 主机 = " smtp.mailserver.com " 密码 = "<字体颜色=#0000ff">密码"" 用户名 = "" 用户名"</>

</ smtp >

</ mailSettings >

</ system.net >

</ 配置 >

公共 静态 bool SendEmail( string 发​​件人, string 收件人,字符串主题,字符串正文)

{

try

{

Configuration mConfigurationFile = ConfigurationManager .OpenExeConfiguration(''D:\\ Projects \\ EmailSolution \\ Email \\ App.config; );

MailSettingsSectionGroup mMailSettings = mConfigurationFile.GetSectionGroup("system.net/mailSettings" ) MailSettingsSectionGroup ;

string mHost = string .Empty;

if (mMailSettings!= null )

{

//int mPort = mMailSettings.Smtp.Network.Port;

mHost = mMailSettings.Smtp.Network.Host;

//string mPassword = mMailSettings.Smtp.Network.Password;

//字符串mUsername = mMailSettings.Smtp.Network.UserName;

}

//允许应用程序使用简单邮件传输协议(SMTP)发送电子邮件.

SmtpClient mailClient = SmtpClient (mHost);

//将电子邮件发送到SMTP服务器以进行传递.

mailClient.Send( EmailMessage .CreateEmailMessage(发件人,收件人,主题,正文));

return true ;

}

解决方案

您正在使用的OpenExeConfiguration重载使用时需要EXE文件的路径,而不是App.Config的路径.在应用程序的bin文件夹的Debug或Release文件夹中,您会发现您的App.config被复制为YourApp.exe.config,这就是将要打开的文件.

如果您想始终打开当前应用程序的配置文件,那么最好使用其他重载的OpenExeConfiguration()方法:

配置mConfigurationFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);


Common sense tells me it should work. The code compiles but the host is always null. Whatever I try I can not get any data from the app.config file. I have a Class Library and I'm trying to build an email component that sends out email using the mailSettings in the app.config file. I'm using VS 2005.

 <configuration>

  <system.net>

    <mailSettings>

      <smtp>

        <network host="smtp.mailserver.com" password="password" userName="username"/>

      </smtp>

    </mailSettings>

  </system.net>

</configuration>

 

public static bool SendEmail(string sender, string recipient, string subject, string body)

        {

            try

            {

                Configuration mConfigurationFile = ConfigurationManager.OpenExeConfiguration("D:\\Projects\\EmailSolution\\Email\\App.config");

                MailSettingsSectionGroup mMailSettings = mConfigurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

 

                string mHost = string.Empty;

 

                if (mMailSettings != null)

                {

                    //int mPort = mMailSettings.Smtp.Network.Port;

                    mHost = mMailSettings.Smtp.Network.Host;

                    //string mPassword = mMailSettings.Smtp.Network.Password;

                    //string mUsername = mMailSettings.Smtp.Network.UserName;

                }

                //Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).

                SmtpClient mailClient = new SmtpClient(mHost);

 

                //Sends an e-mail message to an SMTP server for delivery.

                mailClient.Send(EmailMessage.CreateEmailMessage(sender, recipient, subject, body));

               

                return true;

            }

解决方案

OpenExeConfiguration overload you are using needs a path to EXE file and not path to App.Config as you are using it. In the Debug or Release folder of your application's bin folder you will find that your App.config gets copied as YourApp.exe.config, and this is the file that would get opened.

If you want to always open current application's config file then you are better off with the other overloaded OpenExeConfiguration() method:

Configuration mConfigurationFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);


这篇关于无法阅读&lt; mailSettings&gt;从app.config文件中,为什么????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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