且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何发送HTML嵌入式电子邮件

更新时间:2021-11-16 22:31:36

这只是具有适当的内容类型:text / html,当然还有HTML上下文本身。你的邮件正文不是HTML,但我希望你知道怎么写。



(更准确地说,在 MailMessage 术语,表示为 ContentType.MediaType 属性;请参阅下面的最后一个链接。)



这很简单:属性 System.Net.Mail.MailMessage.IsBodyHtml 可以解决问题:

https://msdn.microsoft.com/en-us/library/system.net。 mail.mailmessage%28v = vs.110%29.aspx [ ^ ]。



如果你想只做一部分你的邮件HTML,它会更棘手。您可以创建多部分邮件消息。部件扮演具有不同内容处置值的附件角色,例如在呈现时嵌入在邮件文本中,或者可下载的附件,或替代视图或链接资源。然后每个部分都可以有自己的内容类型;一个可以是HTML另一个是位图。有关详细信息,另请参阅:

https://msdn.microsoft.com/en-us/library/system.net.mail.attachment%28v=vs.110%29.aspx [ ^ ] ,

https ://msdn.microsoft.com/en-us/library/system.net.mail.attachmentbase%28v=vs.110%29.aspx [ ^ ],

https:// msdn .microsoft.com / zh-cn / library / system.net.mail.attachmentbase.contenttype%28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com /en-us/library/system.net.mime.contenttype.mediatype%28v=vs.110%29.aspx [ ^ ]。


-SA
This just a matter of having appropriate Content-type: "text/html", and of course HTML context itself. Your mail body is not HTML, but I hope you know how to write it.

(More exactly, in MailMessage terminology, this is expressed as ContentType.MediaType property; please see the last link below.)

This is easy: the property System.Net.Mail.MailMessage.IsBodyHtml does the trick:
https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage%28v=vs.110%29.aspx[^].

If you want to make only part of your mail HTML, it would be more tricky. You can create multi-part mail message. Parts plays the roles of attachments with different content-disposition values, such as embedded in the mail text when rendered, or downloadable attachments, or alternative views or linked resource. Then each part could have its own content type; one can be HTML another one a bitmap. For further information, see also:
https://msdn.microsoft.com/en-us/library/system.net.mail.attachment%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.net.mail.attachmentbase%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.net.mail.attachmentbase.contenttype%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.net.mime.contenttype.mediatype%28v=vs.110%29.aspx[^].

Good luck,
—SA


int ExampleYourProductImage = 0;

StringBuilder strEmailBody = new StringBuilder();

strEmailBody.Append("< html>");

strEmailBody.Append("< head>" ;);

strEmailBody.Append("< / head>");

strEmailBody.Append("< body>");



strEmailBody.Append(" table width = \" 400px \" style = \" border:2px black solid;填充:10px; \">");



//使用表行和表列的一行将是Genearte。

strEmailBody.Append("< tr>");



strEmailBody.Append("< td style = \" width:15 %; padding-left:15px; padding:0; margin:0; \">");

strEmailBody.Append("< img alt = \" Logo \" src = \"" + ExampleYourProductImage +" Newimages / logo.jpg \" height = \" 60px \" width = \" 150px \" /&gt ;");

strEmailBody.Append("< td>");



strEmailBody.Append("< ; td style = \" width:15%; text-align:right; padding-right:15px; \">");

strEmailBody.Append("< ; h3>收据< / h3>");

strEmailBody.Append("< / td>");



strEmailBody.Append("< / tr>");



strEmailBody.Append("< / table>" );

//相同你可以使用Div,表格行和表格列设计HTML邮件。



strEmailBody.Append(" < / body>< / html>");



string Message = strEmailBody.ToString();

string strEmailSub = 示例HTML邮件;





MailMessage mail = new System.Net.Mail.MailMessage();

mail.To.Add(&ToMail");

mail.From = new MailAddress(" FromMail");

mail.Subject ="主题;

mail.Body =消息;

mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();

smtp.Host =" Host";

smtp.Port = 000;

smtp.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationSettings.AppSettings [" YourMail"]。ToString(),System.Configuration.ConfigurationSettings .AppSettings [" YourPassword"]。ToString());



smtp.EnableSsl = true;

smtp.Send(mail) ;



Vikas Joshi

高级软件开发人员
int ExampleYourProductImage = 0;
StringBuilder strEmailBody = new StringBuilder();
strEmailBody.Append("<html>");
strEmailBody.Append("<head>");
strEmailBody.Append("</head>");
strEmailBody.Append("<body>");

strEmailBody.Append("<table width=\"400px\" style=\"border: 2px black solid; padding: 10px;\">");

// One Row Will Be Genearte Using Table Row And Table Columns.
strEmailBody.Append("<tr>");

strEmailBody.Append("<td style=\"width: 15%; padding-left: 15px; padding: 0; margin: 0;\">");
strEmailBody.Append("<img alt=\"Logo\" src=\"" + ExampleYourProductImage + "Newimages/logo.jpg \" height=\"60px\" width=\"150px\" />");
strEmailBody.Append("<td>");

strEmailBody.Append("<td style=\"width: 15%; text-align: right; padding-right: 15px;\">");
strEmailBody.Append("<h3>Receipt</h3>");
strEmailBody.Append("</td>");

strEmailBody.Append("</tr>");

strEmailBody.Append("</table>");
// Same You Can Design Your HTML Mail Using Div, Table Row And Table Columns.

strEmailBody.Append("</body></html>");

string Message = strEmailBody.ToString();
string strEmailSub = "Example HTML Mail";


MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add("ToMail");
mail.From = new MailAddress("FromMail");
mail.Subject = "Subject";
mail.Body = Message;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "Host";
smtp.Port = 000;
smtp.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationSettings.AppSettings["YourMail"].ToString(), System.Configuration.ConfigurationSettings.AppSettings["YourPassword"].ToString());

smtp.EnableSsl = true;
smtp.Send(mail);

Vikas Joshi
Senior Software Developer