Can't read from app.config file, why??????

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p class=MsoNormal>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 Im trying to build an email component that sends out email using the mailSettings in the app.config file. Im using VS 2005.
<p class=MsoNormal> <span><<span>configuration<span>>
<p class=MsoNormal><span><span>  <<span>system.net<span>>
<p class=MsoNormal><span><span>    <<span>mailSettings<span>>
<p class=MsoNormal><span><span>      <<span>smtp<span>>
<p class=MsoNormal><span><span>        <<span>network<span> <span>host<span>=<span>"<span>smtp.mailserver.com"<span> <span>password<span>="<font color="#0000ff password</font>"<span> <span>userName<span>="<font color="#0000ff username</font>"<span>/>
<p class=MsoNormal><span><span>      </<span>smtp<span>>
<p class=MsoNormal><span><span>    </<span>mailSettings<span>>
<p class=MsoNormal><span><span>  </<span>system.net<span>>
<p class=MsoNormal><span></<span>configuration<span>>
<p class=MsoNormal><span> 
<p class=MsoNormal><span>public<span> <span>static <span>bool SendEmail(<span>string sender, <span>string recipient, <span>string subject, <span>string body)
<p class=MsoNormal><span><span>        {
<p class=MsoNormal><span><span>            <span>try
<p class=MsoNormal><span><span>            {
<p class=MsoNormal><span><span>                <span>Configuration mConfigurationFile = <span>ConfigurationManager.OpenExeConfiguration(<span>"D:\Projects\EmailSolution\Email\App.config");
<p class=MsoNormal><span><span>                <span>MailSettingsSectionGroup mMailSettings = mConfigurationFile.GetSectionGroup(<span>"system.net/mailSettings") <span>as <span>MailSettingsSectionGroup;
<p class=MsoNormal><span> 
<p class=MsoNormal><span><span>              <span>  <span>string mHost = <span>string.Empty;
<p class=MsoNormal><span> 
<p class=MsoNormal><span><span>                <span>if (mMailSettings != <span>null)
<p class=MsoNormal><span><span>                {
<p class=MsoNormal><span><span>                    <span>//int mPort = mMailSettings.Smtp.Network.Port;
<p class=MsoNormal><span><span>                    mHost = mMailSettings.Smtp.Network.Host;
<p class=MsoNormal><span><span>                    <span>//string mPassword = mMailSettings.Smtp.Network.Password;
<p class=MsoNormal><span><span>                    <span>//string mUsername = mMailSettings.Smtp.Network.UserName;
<p class=MsoNormal><span><span>                }
<p class=MsoNormal><span><span>                <span>//Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).
<p class=MsoNormal><span><span>                <span>SmtpClient mailClient = <span>new <span>SmtpClient(mHost);
<p class=MsoNormal><span> 
<p class=MsoNormal><span><span>                <span>//Sends an e-mail message to an SMTP server for delivery.
<p class=MsoNormal><span><span>                mailClient.Send(<span>EmailMessage.CreateEmailMessage(sender, recipient, subject, body));
<p class=MsoNormal><span><span>               
<p class=MsoNormal><span><span>                <span>return <span>true;
<p class=MsoNormal><span><span>            }

View the full article
 
Back
Top