Send E-mail with Dynamic Body
This Article is helpful to understand this:-
1)basic part of E-mail.
2)how to make dynamic body of email.
3)send mail with SMTP server.
I explain clearly to create dynamic Body part of E-mail Content.
Parts of an email message
Headers
The message headers contain information concerning the sender and recipients. The exact content of mail headers can vary depending on the email system that generated the message. Generally, headers contain the following information:Subject.
Sender (From).
Date and time received (On).
Reply-to.
Recipient (To:).
Recipient email address. Body
The body of a message contains text that is the actual content, such as "Employees who are eligible for the new health care program should contact their supervisors by next Friday if they want to switch." The message body also may include signatures or automatically generated text that is inserted by the sender's email system.Attachments
Attachments are optional and include any separate files that may be part of the message.
In the SMTP standard, the body is the full email message. The header here is only information servers need to deliver the message.............
To implement this concept first create new web application >> Right click on your application >> Select Add New item >> Select HTML Page and click OK
Then after add what you want to show in body part of email code as per it into your HTML Page.
here i want to show 4 paramaters,
Client Name:@@Client_Name@@
Event Name:@@Event_Name@@
Event_Id:@@Event_Id@@
Activation_Code:@@Activation_Code@@
for get value of dynamically of this four parameters,we have to use this methos.
protected void getDataforemail()
{
try
{
EventManagerDataContext db = new EventManagerDataContext();
{
var q = (from a in db.EMR_EVENTs
join b in db.EMR_CLIENTs on a.ClientID equals b.ClientID
select new
{
EventID = a.EventID,
Activation_Code=b.activation_Code
Client_Name = b.Name,
Event_Name = a.Name,
}).ToString();
catch (Exception)
{
throw;
}
}
now you have to call this function only.and you will get value of this parameters dynamically from database.
now put this code into your aspx.cs file.
protected void getDynamicbodyforemail()
{
StreamReader reader = new StreamReader(Server.MapPath("~/HTMLPage1.htm"));
string readFile = reader.ReadToEnd();
string myString = "";
myString = readFile;
myString = myString.Replace("$$Client_Name$$", Client_Name);
myString = myString.Replace("$$Event_Name$$", Event_Name);
myString = myString.Replace("$$Event_Id$$", EventID);
myString = myString.Replace("$$Activation_Code$$", "http://www.Italiyainternational.blogpost.com");
MailMessage Msg = new MailMessage();
MailAddress fromMail = new MailAddress("ketanitaliya16@gmail.com");
// Sender e-mail address.
Msg.From = fromMail;
// Recipient e-mail address.
Msg.To.Add(clientEmail(getClientId(lblclientname.Text)));
// Subject of e-mail
Msg.Subject = "Send Mail with HTML File";
Msg.Body = myString.ToString();
Msg.IsBodyHtml = true;
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromMail.ToString(), "your password");
smtp.Timeout = 20000;
}
smtp.Send(Msg);
reader.Dispose();
}
}
}
Thanks,
ketan