Step 1:- web.config
<system.net>
<mailSettings>
<smtp>
<network host="192.168.1.53" port="28" userName="alert@gmail.com" password="password"/>
</smtp>
</mailSettings>
</system.net>
Step 2:- Read mail settings programmatically
Configuration configuration = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup mailSettingsSectionGroup
= configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettingsSectionGroup
!= null)
{
string host
= mailSettingsSectionGroup.Smtp.Network.Host;
int port
= mailSettingsSectionGroup.Smtp.Network.Port;
string username
= mailSettingsSectionGroup.Smtp.Network.UserName;
string password
= mailSettingsSectionGroup.Smtp.Network.Password;
}
OR
Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~/web.config");
MailSettingsSectionGroup mailSettingsSectionGroup
= configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettingsSectionGroup
!= null)
{
string host
= mailSettingsSectionGroup.Smtp.Network.Host;
int port
= mailSettingsSectionGroup.Smtp.Network.Port;
string username
= mailSettingsSectionGroup.Smtp.Network.UserName;
string password
= mailSettingsSectionGroup.Smtp.Network.Password;
}
OR
SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
if (smtpSection
!= null)
{
string host
= smtpSection.Network.Host;
int port
= smtpSection.Network.Port;
string username
= smtpSection.Network.UserName;
string password
= smtpSection.Network.Password;
No comments:
Post a Comment