Thursday, January 8, 2009

Sending Email using Templates Asp.net

Have you ever tried to send the emails using generic email templates.
Here is the simple method which I use to follow to send email in HTML format.

1. Create a NewAccountTemplate.htm page in "EmailTemplates" folder.
2. Enter your html contents in the htm page.
Eg:










Welcome to My Website!



Hello, ##UserName##. You are receiving this email because you recently created a new account at my
site. Before you can login, however, you need to first visit the following link:



<%VerifyUrl%>



After visiting the above link you can log into the site!



If you have any problems verifying your account, please reply to this email to
get assistance.



Thanks!











3. Create a class FileClass


public class FileClass
{
public FileClass()
{
}

public static string ReadFile(string FileName)
{
try
{
String FILENAME = System.Web.HttpContext.Current.Server.MapPath(FileName);
StreamReader objStreamReader = File.OpenText(FILENAME);
String contents = objStreamReader.ReadToEnd();
return contents;
}
catch (Exception ex)
{

}
return "";
}


public static void SendMail(string From, string To,string CC,string AttachmentFilePath ,string Subject, string Body,ref int intResult,ref string MailResult)
{

try
{
SmtpClient mailClient = new SmtpClient();
NetworkCredential basicAuthenticationInfo = new NetworkCredential();
basicAuthenticationInfo.UserName = ConfigurationManager.AppSettings["userName"].ToString();
basicAuthenticationInfo.Password = ConfigurationManager.AppSettings["password"].ToString();
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;
mailClient.Host = ConfigurationManager.AppSettings["host"].ToString();
mailClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["port"].ToString());
// mailClient.EnableSsl = true;
System.Net.Mail.MailMessage mailmessage = new System.Net.Mail.MailMessage(From, To, Subject, Body);
if(CC.Trim() !="")
mailmessage.CC.Add(CC);
System.Net.Mail.Attachment Attach =null;
if(AttachmentFilePath.Trim() !="")
Attach = new System.Net.Mail.Attachment(AttachmentFilePath);
if(Attach !=null)
mailmessage.Attachments.Add(Attach);
mailmessage.IsBodyHtml = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.EnableSsl = true;
mailmessage.SubjectEncoding = System.Text.Encoding.UTF8;
mailmessage.BodyEncoding = System.Text.Encoding.UTF8;



mailClient.Send(mailmessage);
MailResult = "Mail(s) sent successfully.";
intResult = 0;
}
catch(Exception ex)
{
MailResult = "Mail Unsuccessful : "+ex;
intResult = -1;
//if( ex.ToString() = "System.UnauthorizedAccessException")
//{
// MailResult +="Please give the ASP Net permission to the file before attachment.";
//}

}
}


}



4. call this function from your aspx page.
public string GetRegisterTemplate()
{
return FileClass.ReadFile("EmailTemplates/NewAccountTemplate.htm");
}


5.Code to send Email :

public void SendEmailVerificationToUser(string strUsername)
{
// getting the content of the Email template.
string body = GetRegisterTemplate();
// replace your username with dynamic name and URL and other based on your
requirements
body = body.Replace("##UserName##", userinfo.UserName);
body = body.Replace("##VerifyUrl##", verifyURL);
int intResult = 0;
string strMailResult = "";
FileClass.SendMail("xyz@gmail.com", userinfo.Email, "", "", "Organisation Welcomes: Activation mail...", body, ref intResult, ref strMailResult);
}


For download of the source code plz leave your email.

Happy coding.

24 comments:

  1. Good stuff.
    Please send me the source files at

    lion_king032000@yahoo.co.in

    ReplyDelete
  2. you should close() the stream after getting the file content

    ReplyDelete
  3. Pls send source code to
    arun1103@yahoo.com

    ReplyDelete
  4. good code..it made me go easy for sending emails
    ssrikanth6@gmail.com

    ReplyDelete
  5. Hi
    def-f@hotmail.fr

    ReplyDelete
  6. maane707@hotmail.com

    ReplyDelete
  7. good one ...please send me the code.
    jyoti_kumar_haz@yahoo.co.in

    ReplyDelete
  8. Good article...
    senthilnathanr27@gmail.com

    ReplyDelete
  9. very good!!
    Please send to me: larry_kamonda@hotmail.com

    ReplyDelete
  10. very nice ..
    kmurali.krishna53@gmail.com

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. jasmine.gibson@live.com

    ReplyDelete
  13. Please send the source files to
    ziemer-2010 AT hotmail DOT com

    ReplyDelete
  14. Please Send Me the Source Code..
    chintan.v.p@gmail.com

    ReplyDelete
  15. pls send me code
    jasbir.jnmd001@gmail.com

    ReplyDelete
  16. Please Send Me the Source Code..
    its seem is very useful jmwageni@gmail.com or jmwageni@ids.co.tz

    ReplyDelete
  17. Thanks for this example - any chance i can get a copy at this email address: sneid@sneid.com

    ReplyDelete