Friday, February 8, 2013

send mail using by template in c#

public static void Mail(object obj, string Offers)



{
Customer ObjCustomer = (Customer)obj;

string[] Arr = new string[5];



Arr[0] = ObjCustomer.CompanyName;

Arr[1] = ObjCustomer.Email;

Arr[2] = ObjCustomer.Password;

Arr[3] = Offers;
Arr[4] = ConfigurationManager.AppSettings["SiteName"];

string bodyMatter = GetHtml("mail.html", Arr);//for reading template



SendMail(MailFrom, MailFrom, ObjCustomer.Email, Registration_MailSubject, bodyMatter);

 

}



========================================================
public static bool SendMail(string fromAddress, string displayName, string toAddress, string subjectMatter, string bodyMatter)


{
 
       

bool returnVal=false;

        SmtpClient smtpClient = new SmtpClient();

        MailMessage message = new MailMessage();

        string ccMailId = ConfigurationManager.AppSettings["CMailId"].ToString();

        try

        {

            MailAddress fromAddres = new MailAddress(fromAddress, displayName);




 
 

            message.From = fromAddres;

            message.To.Add(toAddress);

            message.Subject = subjectMatter;

            message.CC.Add("gautamsingh64@gmail.com");

            message.CC.Add(ccMailId);

            message.IsBodyHtml = true;      

            message.Body = bodyMatter;     

            smtpClient.Send(message);

            returnVal= true;

        }

        catch (Exception ex)

        {

            returnVal= false;

        }

return returnVal;



}

==========================
public static string GetHtml(string argTemplateDocument, string[] Arr)



{
 
int i;

StreamReader sr;

string Data;

string _path = HttpContext.Current.Server.MapPath("~/MailTempletes/") + argTemplateDocument;

sr = File.OpenText(_path);



Data = sr.ReadToEnd();
 
if ((Arr == null))



{
 
return Data;



}
 
else



{
 
for (i = 0; i < Arr.Length; i++)



{
 
Data = Data.ToString().Replace("@V" + i + "@", Arr[i]);



}
 
return Data;



}

sr.Close();
 
sr = null;



}
============================
============
template

===========
<!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=iso-8859-1" />

<link type="text/css" rel="Stylesheet" href="@V4@Css/screen.css"/>

<title>Email Template</title>

<style>

*{padding:0; margin:0; border:0;}

body {

font-family: Arial, Helvetica, sans-serif;

font-size: 12px;

color: #111B07;



}
 
.txt16-green{ font-size:16px; color:#000000; font-weight:bold;}

.txt12-green{ font-size:12px; color:#000000; font-weight:bold;}

.txt12-black{ font-size:12px; font-weight:bold;}

.txt12-str{font-size:12px; font-weight:bold; text-decoration:line-through;}

.sec-title{

font-size:20px;

color:#fff;

background:url(@V4@images/menubg.jpg) repeat-x;

height:34px;

vertical-align:middle;

padding:0 0 0 14px;



}
 
</style>

</head>

<body>

<table width="700px" align="left" cellpadding="0" cellspacing="0" style="border: solid 1px #000;">

<tr>

<td style="padding: 14px;">

<img src="@V4@images/logo.jpg" border="0" />

</td>

</tr>

<tr>

<td>

<table width="100%" cellpadding="0" cellspacing="0" border="0">

<tr>

<td style="padding: 14px">

<table width="100%" cellpadding="0" cellspacing="0" border="0">

<tr>

<td class="txt12-green">

Dear @V0@</td>

</tr>

<tr>

<td>

&nbsp;</td>

</tr>

<tr>

<td >

Thank you for registering with us at <a style="color:#807303" href="@V4@">sitename</a>




 
</td>

</tr>

<tr>

<td>

&nbsp;</td>

</tr>

<tr>

<td>

<b>You have successfully registered!</b>

</td>

</tr>

<tr>

<td>

                                        </td>

</tr>

<tr>

<td>

Our administrator will check the your account details and will approve your account.<br /><br />

Once approved/unapproved you will be notified via email.<br /><br />

If approved then you can browse any part of the website.<br /><br />

</td>

</tr>

<tr>

<td>

&nbsp;</td>

</tr>

<tr>

<td class="txt12-black">

<table>

<tr>

<td>



Login
 
</td>

<td>

:</td>

<td>



@V1@
 
</td>

</tr>

<tr>

<td>



Password
 
</td>

<td>

:</td>

<td>



@V2@
 
</td>

</tr>

</table>

</td>

</tr>

<tr>

<td>

&nbsp;</td>

</tr>

<tr>

<td class="txt12-green">

Thanks & Regards</td>

</tr>

<tr>

<td>

&nbsp;</td>

</tr>

<tr>

<td >




 
<p>

&nbsp;</p>

<strong>Phone no</strong>. 54545455454

<p>

<strong>Email</strong>:sales@kkkkk.com</p>

</td>

</tr>

<tr>

<td>

&nbsp;</td>




 
</tr>

<tr>

<td>

<strong>Please do not reply to this email. It was sent from an unattended mailbox.</strong>

</td>

</tr>




 
</table>

</td>

</tr>

</table>

</td>

</tr>

</table>

</body>

</html>

No comments:

Post a Comment