Create and send HTML Formatted Emails in ASP.Net using C#

Create File name as Test.htm

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <img src="/Logo.png" /><br />
    <br />
    <div style="border-top: 3px solid #22BCE5">
        &nbsp;</div>
    <span style="font-family: Arial; font-size: 10pt">Hello <b>{UserName}</b>,<br />
        <br />
        A new article has been published on ASPSnippets.<br />
        <br />
        <a style="color: #22BCE5" href="{Url}">{Title}</a><br />
        {Description}
        <br />
        <br />
        Thanks<br />
        Raghu.N </span>
</body>
</html>

Note :- While Creating Mail Format Body….Follow this logic or else do your own Logic Style.

private string PopulateBody(string userName, string title, string url, string description)
    {
        string body = string.Empty;
        using (StreamReader reader = new StreamReader(Server.MapPath("~/test.htm")))
        {
            body = reader.ReadToEnd();
        }
        body = body.Replace("{UserName}", userName);
        body = body.Replace("{Title}", title);
        body = body.Replace("{Url}", url);
        body = body.Replace("{Description}", description);
        return body;
    }


Note :- Below Method used is to send mail with crenditial.

public static int SendMail(string mailTo, string mailCC, string mailBCC, string mailSubject, string mailBody)
    {

        int mailStatus = 1;
        MailMessage mail = new MailMessage();
        mail.IsBodyHtml = true;
        string[] splitedEmails;
        if (mailTo != "")
        {
            splitedEmails = mailTo.Split(';');
            foreach (string s in splitedEmails)
            {
                mail.To.Add(s);
            }
        }
        if (mailCC != "")
        {
            splitedEmails = mailCC.Split(';');
            foreach (string s in splitedEmails)
            {
                mail.CC.Add(s);
            }
        }
        if (mailBCC != "")
        {
            splitedEmails = mailBCC.Split(';');
            foreach (string s in splitedEmails)
            {
                mail.Bcc.Add(s);
            }
        }
        mail.Subject = mailSubject;
        mail.Body = mailBody;
        mail.From = new MailAddress(GiveYourMailID);
        mail.Sender = new MailAddress(GiveYouMailID);
        mail.IsBodyHtml = true;
        try
        {
            SmtpClient smtp = new SmtpClient(“smtp.gmail.com”);
            smtp.Credentials = new System.Net.NetworkCredential("raghu.nmca@gmail.com","GiveYourPassword");
            smtp.EnableSsl = true; // If you use this, while sending mail through gmail or(https:\\sites)  OR Enable False.
            smtp.Send(mail);
        }
        catch (Exception Ex)
        {
            mailStatus = 0;
            Mails.SendMail(ConfigurationManager.AppSettings["EmailDomain"].ToString(), ConfigurationManager.AppSettings["EmailFrom"].ToString(), ConfigurationManager.AppSettings["EmailSender"].ToString(), ConfigurationManager.AppSettings["ErrorMailTo"].ToString(), "", "", "DMS Mail Sending Exception", "DMS Mail Sending Exception  " + Ex.ToString() + " At:  " + DateTime.Now);
        }
        return (mailStatus);
    }

Comments

Popular posts from this blog

SQL Query :- Create Tables,Primary key,Foreign key,Merge Statment

AngularJS

Check folder and Delete All Files from Existing Folder in C#