About Me

My photo
a Dynamic and Energetic guy.....

Friday, December 18, 2009

Sending Email Using Web.Config Entries in .NET

// Web.Config sample

<configuration>

<system.net>

<mailSettings>

<smtp from="website@website.com">

<network host="mail.website.com" password="" userName=""/>

</smtp>

</mailSettings>

</system.net>

</configuration>




// Code sample

using System.Net.Mail;

MailSettingsSectionGroup mailSettings = WebConfigurationManager.GetSection("system.net/

mailSettings") as MailSettingsSectionGroup;


protected void SendingEmail()

{

// Sending E-Mail

MailMessage message = new MailMessage(mailSettings.Smtp.From, WebConfigurationManager.AppSettings["RecipientList"], "Sending E-mail using web.config", "");

message.Priority = MailPriority.High;

SmtpClient smtp = new SmtpClient(mailSettings.Smtp.Network.Host);

smtp.Send(message);

}

No comments:

My Masters