Below article is use full for sent a mail functionality from template, In real scenario in web application we have to send multiple mails to different operation from system and they all have different contents.
This article contains below things.
Sent mail
Mail content pick from template
Add Dynamic parameter in mail contents.
So I have implemented common class with that class use few required parameter and based on that we can sent a mail as per contents provided in temperate.
For simple and common method, we are storing html email template file in text file in any specific folder in application directory
Add Dynamic Value in Template:
Mail template never be static contents there is always a dynamic data like stack holder name or data from database.
So that in template we are storing that contents as {0},{1}……. And original value we are passed in list collection in that class and inside the class that is handle this part.
Below is the class for Sent mail.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.IO;
using System.Text;
namespace Library
{
public class SentMail
{
public SentMail()
{
}
private string _SMTPAddress;
public string SMTPAddress
{
get
{
if(string.IsNullOrEmpty(_SMTPAddress))
return System.Configuration.ConfigurationSettings.AppSettings["SMTPSERVER"].ToString();
else
return _SMTPAddress;
}
set
{
_SMTPAddress = value;
}
}
private string _FromDisplayName;
public string FromDisplayName
{
get
{
if (string.IsNullOrEmpty(_SMTPAddress))
return System.Configuration.ConfigurationSettings.AppSettings["FROMDISPLAY"].ToString();
else
return _FromDisplayName;
}
set
{
_FromDisplayName = value;
}
}
private string _FromAddress;
public string FromAddress
{
get
{
if (string.IsNullOrEmpty(_SMTPAddress))
return System.Configuration.ConfigurationSettings.AppSettings["FROMADDRESS"].ToString();
else
return _FromAddress;
}
set
{
_FromAddress = value;
}
}
///
/// To Address if multiple then add ; in between each address
///
public string ToDisplayName { get; set; }
public string ToAddress { get; set; }
///
/// CC Address if multiple then add ; in between each address
///
public string CCAddress { get; set; }
///
/// BCC Address if multiple then add ; in between each address
///
public string BCCAddress { get; set; }
public string Body { get; set; }
public string Subject { get; set; }
public List SubjectParameter { get; set; }
public List Attachment { get; set; }
public bool HighPriority { get; set; }
public List DynamicParameter { get; set; }
public string TemplateFile { get; set; }
public bool Sent()
{
bool bReturn = true;
try
{
SmtpClient oSMTLClient = new SmtpClient();
oSMTLClient.Host = this.SMTPAddress;
MailMessage message = new MailMessage();
message.From = new MailAddress(this.FromAddress, this.FromDisplayName);
if (!string.IsNullOrEmpty(this.ToAddress))
{
string[] ToAddress = this.ToAddress.Trim().Split(";".ToCharArray());
foreach (string itemTo in ToAddress)
{
message.To.Add(itemTo);
}
}
// CC address
if (!string.IsNullOrEmpty(this.CCAddress))
{
string[] CCAddress = this.CCAddress.Trim().Split(";".ToCharArray());
foreach (string itemCC in CCAddress)
{
message.CC.Add(itemCC);
}
}
// BCC address
if (!string.IsNullOrEmpty(this.BCCAddress))
{
string[] BCCAddress = this.BCCAddress.Trim().Split(";".ToCharArray());
foreach (string itemBCC in BCCAddress)
{
message.Bcc.Add(itemBCC);
}
}
message.Body = string.Format(this.MailBodystring(), this.DynamicParameter.ToArray());
message.IsBodyHtml=true;
if (this.HighPriority)
{
message.Priority = MailPriority.High;
}
if (this.Attachment != null && this.Attachment.Count > 0)
{
// Attachment code
}
message.Subject = string.Format(this.Subject, this.SubjectParameter.ToArray());
oSMTLClient.Send(message);
bReturn = true;
}
catch (Exception ex)
{
bReturn = false;
}
return bReturn;
//message.CC = this.CCAddress
}
private string MailBodystring()
{
string MailBody = "";
if (File.Exists(this.TemplateFile))
{
StreamReader SR = new StreamReader(this.TemplateFile);
MailBody = SR.ReadToEnd();
SR.Close();
SR.Dispose();
}
return MailBody;
}
}
}
Below is the code for how to use this class.
SentMail oMail = new SentMail();
oMail.SMTPAddress = "mail.myserver.com";// if not provide that consider value from web.config
oMail.FromAddress = "amit.patel@myserver.com";// if not provide that consider value from web.config
oMail.DynamicParameter = new List { "Amit Patel", "5P Admin" };
oMail.SubjectParameter = new List { "Amit Patel" };
oMail.FromDisplayName = "Amit Patel";// if not provide that consider value from web.config
oMail.HighPriority=false;
oMail.ToAddress = "amit.patel@myserver.com";
oMail.Subject = "Leave Application for : {0}";
oMail.TemplateFile = Server.MapPath("HTMLPage.txt");
oMail.Sent();
Thanks,
Amit Patel
Happy Programming