Sending emails in asp.net

How to Send emails in asp.net ? 



First we Need A Hmtl page
Firstly you will need to build an HTML Template of the body which will have some placeholders which will be replaced with the actual content. Advantage of creating templates instead of building HTML using String Builder in code is that you can easily change the HTML of the template without changing the code.
 
 
<head>
<title></title>
</head>
<body>
<img src = "images/Blue/Logo.png" /><br /><br />
<div style = "border-top:3px solid #22BCE5">&nbsp;</div>
<span style = "font-family:Arial;font-size:10pt">
Hello <b>{UserName}</b>,<br /><br />
Test Email Webblog.<br /><br />
<a style = "color:#22BCE5" href = "{Url}">{Title}</a><br />
{Description}
<br /><br />
Thanks<br /> 
asp-net-learning.blogspot.com 
 </span>
</body>
</html>
 
 
 
On the click event handler of a button I will first read the HTML Template file and then replace the placeholders with their values. 


private string PopulateBody(string userName, string title, string url, string description)
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userName);
body = body.Replace("{Title}", title);
body = body.Replace("{Url}", url);
body = body.Replace("{Description}", description);
return body;
}
 
 
Email Sending Method
 
 
private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recepientEmail));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
smtp.Send(mailMessage);
}
}  
 
And below are the web.config setting for gmail:
 
 
<appSettings>
   <addkey="Host"value="smtp.gmail.com"/>
   <addkey="EnableSsl"value="true"/>
   <addkey="UserName"value="sender@gmail.com"/>
   <addkey="Password"value="xxxxx"/>
   <addkey="Port"value="587"/>
</appSettings>
 
 
On the click of the send button I am sending the formatted HTML emails.
 
protected void SendEmail(object sender, EventArgs e)
{
string body = this.PopulateBody("John",
"Test Email from ASP.NET website",
"Email from my website" +
"Complete ASP.NET",
"with C#"
+ "Check this email");
this.SendHtmlFormattedEmail("recipient@gmail.com", "Test Email Sent" , body);
}
 

No comments:

Post a Comment