Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

How do I get the SMTP email address from web.config?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Thielen - 18 Aug 2007 06:10 GMT
I've tried:
MailSettingsSectionGroup mailConfig =
WebConfigurationManager.GetSection("system.net/mailSettings") as
MailSettingsSectionGroup;

and:
    Configuration configurationFile =
WebConfigurationManager.OpenWebConfiguration("/Web.Config");
    MailSettingsSectionGroup mailConfig;
    if (configurationFile != null)
        mailConfig = configurationFile.GetSectionGroup("system.net/mailSettings")
as MailSettingsSectionGroup;

The first method returns null and the second returns an object but it does
not have the host or from properties set.

Signature

thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

Manish Bafna - 18 Aug 2007 11:08 GMT
Hi,
First write below code in web.config  </system.web> and before
</configuration>:
<system.net >
   <mailSettings >
   <smtp from="who@myhost.com">
     <network host="mail.myhost.net" userName="xxxxx" password="xxxx"/>
   </smtp>
 </mailSettings>
   </system.net >

Then try this code:
Configuration configurationFile =
WebConfigurationManager.OpenWebConfiguration("~\\Web.config");
       MailSettingsSectionGroup mailSettings =
configurationFile.GetSectionGroup("system.net/mailSettings") as
MailSettingsSectionGroup;
       if (mailSettings != null)
       {    
           int port = mailSettings.Smtp.Network.Port;    
           string host = mailSettings.Smtp.Network.Host;  
           string password = mailSettings.Smtp.Network.Password;    
           string username = mailSettings.Smtp.Network.UserName;

           lblHost.Text = host;
           lblUserName.Text = username;
           lblPassword.Text = password;
       }
Also include following namespaces:
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;
Signature

Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

> I've tried:
> MailSettingsSectionGroup mailConfig =
[quoted text clipped - 11 lines]
> The first method returns null and the second returns an object but it does
> not have the host or from properties set.
David Thielen - 19 Aug 2007 17:32 GMT
worked great - thanks

Signature

thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

> Hi,
> First write below code in web.config  </system.web> and before
[quoted text clipped - 44 lines]
> > The first method returns null and the second returns an object but it does
> > not have the host or from properties set.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.