In this post I will explain how to send the tutorial in ASP.NET. I have used GMail SMTP to send the mail.
First of all add the keys host and port to web.config file
<appSettings>
<add key="host" value="smtp.gmail.com" />
<add key="port" value="587" />
</appSettings>
Now create a class with name Helper and add common method to send the mail so that you can access this to any page in your application. First method is to specify the values to send the mail like From, To, Subject, Body etc. Second method is for getting the HTML template to create mail body.
public int SendMail(string From, string To, string CC, string AttachmentFilePath, string Subject, string Body, string Password)
{
int Result;
try
{
SmtpClient mailClient = new SmtpClient();
NetworkCredential basicAuthenticationInfo = new NetworkCredential();
basicAuthenticationInfo.UserName = From;
basicAuthenticationInfo.Password = Password;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;
mailClient.Host = ConfigurationManager.AppSettings["host"].ToString();
mailClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["port"].ToString());
System.Net.Mail.MailMessage mailmessage = new System.Net.Mail.MailMessage(From, To,
Subject, Body);
if (CC.Trim() != "")
mailmessage.CC.Add(CC);
System.Net.Mail.Attachment Attach = null;
if (AttachmentFilePath.Trim() != "")
Attach = new System.Net.Mail.Attachment(AttachmentFilePath);
if (Attach != null)
mailmessage.Attachments.Add(Attach);
mailmessage.IsBodyHtml = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.EnableSsl = true;
mailmessage.SubjectEncoding = System.Text.Encoding.UTF8;
mailmessage.BodyEncoding = System.Text.Encoding.UTF8;
mailClient.Send(mailmessage);
Result = 0;
return Result;
}
catch (Exception)
{
Result = -1;
return Result;
}
}
{
WebClient client = new WebClient();
string content = client.DownloadString(path);
return content;
}
Now create an HTML template for the Email. Here I have provided the sample.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
Welcome ##UserName##
<h2>This is the welcome email.</h2>
</body>
</html>
Now add the method to in code behind page to call the method.
public int SendTheMail(string From, string To, string CC, string Body, string Password, string Attachment)
{
Helper obj = new Helper();
int result;
string body, subject;
// getting the content of the Email template.
body = obj .GetTemplate(Server.MapPath("../EmailTemplate/ConfirmationMail.htm"));
subject = "Confirmation Mail";
string Username = "Ankit";
// replace your content with dynamic content
body = body.Replace("##UserName##", Username);
result = obj.SendMail(From, To, CC, Attachment, subject, body, Password);
return result;
}
Now you can call this method(SendTheMail) on Button click or wherever you want
If you have any doubt please put comment...
No comments:
Post a Comment