如何在用户邮箱中发送带有HTML格式的邮件正文的邮件 [英] How do I send mail with mail body being HTML formatted in users mail box

查看:108
本文介绍了如何在用户邮箱中发送带有HTML格式的邮件正文的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML格式的电子邮件正文内容来自数据库表。发送邮件时,将按照格式化并保存在db中的方式格式化并在用户邮箱中呈现正文。 (HTML格式化)。



数据库中的代码为:

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 :

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;
     }







在用户邮箱中,当他们收到邮件时,他们会收到邮件方式而不是格式化。如何使用HTML格式在用户邮箱中呈现页面而不是在邮箱中显示所有标签和内容?



我尝试过:



我创建了一个方法PopulateBody(mailMessage,viewName:UserGroupDiagnostic);使用HTML格式格式化正文并发送此正文。但这种方式也不能按要求工作。代码是:



公共虚拟MailMessage SendMailNotification(UserInfo项目,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;

}





- 查看生成HTML内容的开始

@ {

string mailBody =( string)ViewBag.MailBody;

string projName = Kats.DAL .Connection.ProjectName();

}





@mailBody



}

- 生成HTML内容的视图结束




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

推荐答案

mailMessage = new MailMessage(" xxxx @ gmail.com",

" yyyy@gmail.com",来自PSSP系统的消息,

"这封电子邮件由PSSP系统发送< br />" +

"< b>这是粗体文字!< / b>");



mailMessage.IsBodyHtml = true ;
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;


检查MailMessage对象是否具有.HTMLBody属性。我使用具有该属性的Outlook对象,不确定通用对象。



oMessage只是我用来收集邮件消息的所有位的类。



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.

// 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();


我的身体
string body = diagnosticTestMailContent.Mail_Body;



中有carryinh html标签。它直接被渲染。我将主体添加到stringbuilder然后它开始工作。


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


这篇关于如何在用户邮箱中发送带有HTML格式的邮件正文的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆