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:
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.
good stuff!
ReplyDeletelixonraphael@gmail.com
ReplyDeleteGood stuff.
ReplyDeletePlease send me the source files at
lion_king032000@yahoo.co.in
you should close() the stream after getting the file content
ReplyDeletePls send source code to
ReplyDeletearun1103@yahoo.com
salman350@gmail.com
ReplyDeletegood code..it made me go easy for sending emails
ReplyDeletessrikanth6@gmail.com
Hi
ReplyDeletedef-f@hotmail.fr
maane707@hotmail.com
ReplyDeletenice one
ReplyDeletegauravjoshi1818@gmail.com
ReplyDeletenitha_rj@yahoo.co.in
ReplyDeletegood one ...please send me the code.
ReplyDeletejyoti_kumar_haz@yahoo.co.in
Good article...
ReplyDeletesenthilnathanr27@gmail.com
very good!!
ReplyDeletePlease send to me: larry_kamonda@hotmail.com
very nice ..
ReplyDeletekmurali.krishna53@gmail.com
Good
ReplyDeleteThis comment has been removed by the author.
ReplyDeletejasmine.gibson@live.com
ReplyDeletePlease send the source files to
ReplyDeleteziemer-2010 AT hotmail DOT com
Please Send Me the Source Code..
ReplyDeletechintan.v.p@gmail.com
pls send me code
ReplyDeletejasbir.jnmd001@gmail.com
Please Send Me the Source Code..
ReplyDeleteits seem is very useful jmwageni@gmail.com or jmwageni@ids.co.tz
Thanks for this example - any chance i can get a copy at this email address: sneid@sneid.com
ReplyDelete