Monday, March 31, 2008

Simple mail sending code and mail body format with style sheet

we can put clsaa name in TDs becz we took the style.css path

public bool SendMailInvitation(string To, string Subject,string nameOfRecipient)
{

bool status = true;
string path = ApplicationPath.getApplicationPath();
try
{
string strMailStyle = "[link rel='stylesheet' type='text/css' href=" + path + "/css/style.css]";
String strMessage;

strMessage = "[table width='600px' border='0' align='center' cellpadding='0' cellspacing='0']";
strMessage += "[tr][td] [/td][/tr]";
strMessage += "[tr][td][table width='600px' border='0' cellspacing='2' cellpadding='4']";
strMessage += " [tr][td width='8'][/td]";
strMessage += "[td][/td]";
strMessage += "[td width='8'][/td][/tr]";
strMessage += " [tr][td][/td]";
strMessage += "[td][table width='100%' border='0' align='center' cellpadding='0' cellspacing='0'][tr] [td

height='10px'][/td][/tr]";
strMessage += " [tr][td ] Hi " + nameOfRecipient + " :[/td] [/tr][tr] [td height='9px'][/td][/tr]";
strMessage += "[tr] [td height='6px'][/td][/tr]";
strMessage += " [tr][td ] Congratulations–you have been promoted to an Editor on *****.[/td] [/tr]";
strMessage += "[tr] [td height='6px'][/td][/tr]";
//
strMessage += " [tr][td ] When you sign in, you’re now able to create places and edit events on the site. [/td]

[/tr]";
strMessage += "[tr] [td height='6px'][/td][/tr]";
strMessage += " [tr][td ] Get started now by following the link below, or copy and paste it into your browser:

[/td] [/tr]";
strMessage += "[tr] [td height='6px'][/td][/tr]";

strMessage += "[tr][td ][a href='http://www.8**********.com']http://www.********l.com[/a][/td][/tr]";
strMessage += "[tr] [td height='6px'][/td][/tr]";

strMessage += " [tr][td ] Wield this immense power wisely.[/td] [/tr]";
strMessage += "[tr] [td height='6px'][/td][/tr]";




strMessage += "[tr] [td height='10px'][/td][/tr]";
strMessage += "[tr] [td ] -the *****Team [/td][/tr]";
strMessage += "[tr] [td height='1px' ] [/td][/tr]";
strMessage += " [tr][td ] **********.com, ****** 11201,[br/] USA –

www.********.com[/td][/tr]";
//Questions? Concerns? Acclaim? Contact us at feedback@**********.com
strMessage += " [tr][td ] Questions? Concerns? Acclaim? Contact us at [a

href='mailto:feedback@*****.com'] feedback@*******.com [/a] [/td][/tr]";

strMessage += "[/table][/td]";
strMessage += "[td][/td]";
strMessage += "[/tr][tr][td][/td]";
strMessage += " [td][/td]";
strMessage += "[td][/td]";
strMessage += "[/tr][/table][/td][/tr][/table]";

string message = strMessage + strMailStyle;
MailMessage Mail_Message = new MailMessage();
Mail_Message.From = new MailAddress("service@******.com");
Mail_Message.To.Add(new MailAddress(To));
Mail_Message.Subject = Subject; // "This is my subject";
Mail_Message.Body = message; // "This is the content";
Mail_Message.IsBodyHtml = true;
SmtpClient Smtp_Client = new SmtpClient();

Smtp_Client.Send(Mail_Message);

}
catch (Exception e)
{

status = false;
string str = e.Message;
}
return status;
}



This function retrive the server path where application is run and strugn string path

public static string getApplicationPath()
{
string Port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
if (Port == null || Port == "80" || Port == "443")
Port = "";
else
Port = ":" + Port;
string Protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
if (Protocol == null || Protocol == "0")
Protocol = "http://";
else
Protocol = "https://";
string p = Protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + Port +

System.Web.HttpContext.Current.Request.ApplicationPath;
p = p.Trim();
if (!p.EndsWith("/"))
p += "/";
return p;
}


2) set the folowing mail.net setting in web config

[system.net]
[mailSettings]
[smtp]
[network host="mail.*******.com" port="25" userName="mahesh.sharma@*********.com"

password="******"/]
[/smtp]
[/mailSettings]
[/system.net]
[/configuration]

No comments: