Introduction:
Here I will explain how to Sending Email from Asp.net With Dynamic Content using asp.net in c#.
Here I will explain how to Sending Email from Asp.net With Dynamic Content using asp.net in c#.
In Previous Post I was Explained about the How To- Search records or data in gridview using jQuery , Sqldatasource Filterexpression example in asp.net, Asp.net GridView Sorting example: How to sort GridView Data, How To Export gridview data to Excel/PDF , CSV Formates in asp.net C#
Mailcontant.html page
This is your actual data here you can apply CSS styles also .
<table border="1" style="font-family:Arial; border-color:Black; font- size:11px;"cellspacing="0" cellpadding="2">
<tr>
<td width="120">ID :td>
<td width="190">#id#td>
tr>
<tr>
<td>Request Date :td>
<td>#enquirydate#td>
tr>
<tr>
<td>Customer Name :td>
<td>#customername#td>
tr>
<tr>
<td>Customer Address1 :td>
<td>#custaddress1#td>
tr>
<tr>
<td>Customer Address2 :td>
<td>#custaddress2#td>
tr>
<tr>
<td>State :td>
<td>#state#td>
tr>
<tr>
<td>City :td>
<td>#city#td>
tr>
<tr>
<td>Remarks :td>
<td>#remarks#td>
tr>
table>
public static void sendmail(string ID,string mailbody)
{
string mailbodytext = "";
string mailID = string.Empty;
StreamReader sr = newStreamReader(HttpContext.Current.Server.MapPath("Mailcontant.html"));
sr = File.OpenText(HttpContext.Current.Server.MapPath("Mailcontant.html "));
mailbodytext = sr.ReadToEnd();
sr.Close();
string squery = string.Empty;
squery = "select * from Detail where ID='" + ID + "'";
DataTable dr = DataAccess.getDataTable(squery);
// Replace your Variables to actual data
if (dr.Rows.Count > 0)
{
mailbodytext = mailbodytext.Replace("#id#", ID);
mailbodytext = mailbodytext.Replace("#enquirydate#", Convert.ToDateTime(dr.Rows[0]["Date"]).ToString("dd/MM/yyyy"));
mailbodytext = mailbodytext.Replace("#customername#", dr.Rows[0]["Cust_Name"].ToString());
mailbodytext = mailbodytext.Replace("#custaddress1#", dr.Rows[0]["Cust_Address1"].ToString());
mailbodytext = mailbodytext.Replace("#custaddress2#", dr.Rows[0]["Cust_Address2"].ToString());
mailbodytext = mailbodytext.Replace("#state#", dr.Rows[0]["State"].ToString());
mailbodytext = mailbodytext.Replace("#city#",
dr.Rows[0]["City"].ToString());
mailbodytext = mailbodytext.Replace("#remarks#", dr.Rows[0]["Remarks"].ToString());
}
SmtpClient smtp = new SmtpClient();
MailMessage msg = new MailMessage();
smtp.EnableSsl = true;
msg.IsBodyHtml = true;
msg.To.Add(new MailAddress(ConfigurationManager.AppSettings["EmailTo"].ToString()));
msg.Subject = "Request - " + FeasibilityID;
msg.Body = mailbody + "
" + mailbodytext;
" + mailbodytext;
smtp.EnableSsl = true;
object state = msg;
smtp.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtp.Send(msg);
}
private static void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MailMessage mail = e.UserState as MailMessage;
if (!e.Cancelled && e.Error != null)
{
message.Text = "Mail sent successfully";
}
else
{
message.Text = "Error occured";
}
}
Add smtp server setting in web.config file
<system.net>
<mailSettings>
<smtp from="work@gmail.com" deliveryMethod="Network">
<network host="smtp.gmail.com" port="587"
userName="work@gmail.com" password="12345"/>
smtp>
mailSettings>
system.net>
Comments
Post a Comment