Click here to Skip to main content
14,979,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a HTML formatted email body content that comes from database table. When the mail is send, the body is to be formatted and rendered in users mail box in the way it is formatted and saved in db. (HTML formatting).

The code in database is :
<code></code><p>
    This is body</p>
<p style="text-align: center;">
   Formatted
<h2>
    Body</h2>
<p style="margin-removed 40px;">
    NEW BODY</p>


My code to send mail is :

C#
public virtual MailMessage SendMailNotification(UserInfo item, DiagnosticTestMailContent diagnosticTestMailContent)
     {
         string body = diagnosticTestMailContent.Mail_Body;
         var mailMessage = new MailMessage { Subject = diagnosticTestMailContent.Mail_Header + Kats.DAL.Connection.ProjectName().ToUpper() , Body = body };
         mailMessage.From = new MailAddress(Kats.Helpers.FileSettings.GetAdminMail());
         mailMessage.To.Add(item.Email);
         ViewBag.MailBody = diagnosticTestMailContent.Mail_Body;
        return mailMessage;
     }



In users mail box, when they receive mail, they receive it this way instead of being formatted. How can I render a page in user mail box using HTML formatting instead of displaying all tags and content in mail box??

What I have tried:

I had created a method PopulateBody(mailMessage, viewName: "UserGroupDiagnostic"); to format the body using HTML formatting and send this body. But this way also it not working as required. The code for this is ;

public virtual MailMessage SendMailNotification(UserInfo item, DiagnosticTestMailContent diagnosticTestMailContent)
{
string body = diagnosticTestMailContent.Mail_Body;
var mailMessage = new MailMessage { Subject = diagnosticTestMailContent.Mail_Header + Kats.DAL.Connection.ProjectName().ToUpper() , Body = body };
mailMessage.From = new MailAddress(Kats.Helpers.FileSettings.GetAdminMail());
mailMessage.To.Add(item.Email);
ViewBag.MailBody = diagnosticTestMailContent.Mail_Body;
PopulateBody(mailMessage, viewName: "UserGroupDiagnostic");
return mailMessage;
}


--View to generate HTML content starts
@{
string mailBody = (string)ViewBag.MailBody;
string projName = Kats.DAL.Connection.ProjectName();
}


@mailBody

}
--View to generate HTML content ends
Posted
Updated 3-Oct-16 2:31am

mailMessage = new MailMessage("xxxx@gmail.com",
"yyyy@gmail.com", "Message from PSSP System",
"This email sent by the PSSP system<br />" +
"<b>this is bold text!</b>");

mailMessage.IsBodyHtml = true;
   
Comments
Codes DeCodes 8-Sep-16 7:51am
   
mailMessage.IsBodyHtml = true;
I tried this but it is not working in my case..
Beginner Luck 8-Sep-16 8:49am
   
change your var mailMessage = new MailMessage to MailMessage mailMessage = new MailMessage();
Check if the MailMessage object has a .HTMLBody property. I use the Outlook object that has the property, not sure about the generic object.

oMessage is just a class I use to collect all the bits use for the mail message.

C#
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = oMessage.Body;
oMsg.Subject = oMessage.Subject;
oMsg.To = oMessage.MailTo;
oMsg.CC = oMessage.CC;
oMsg.Display();
   
My body
C#
string body = diagnosticTestMailContent.Mail_Body;

was carryinh html tags in it. It was directly being rendered. I added the body to stringbuilder and then it started working.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900