我在邮件中将文本作为html标记发送到邮件时遇到了问题。首先,我将rtf转换为html
string GetHtmlContent(string someRTFtext)
{
RichEditDocumentServer server = new RichEditDocumentServer();
server.RtfText = someRTFtext;
server.Options.Export.Html.CssPropertiesExportType = DevExpress.XtraRichEdit.Export.Html.CssPropertiesExportType.Inline;
server.Options.Export.Html.DefaultCharacterPropertiesExportToCss = true;
server.Options.Export.Html.EmbedImages = true;
server.Options.Export.Html.ExportRootTag = DevExpress.XtraRichEdit.Export.Html.ExportRootTag.Body;
return server.HtmlText;
}
这个方法让我回复
<body>
<style type="text/css">
.cs2E86D3A6{text-align:center;text-indent:0pt;margin:0pt 0pt 0pt 0pt}
.cs88F66593{color:#800080;background-color:transparent;font-family:Arial;font-size:8pt;font-weight:bold;font-style:normal;}
</style>
<p class="cs2E86D3A6"><span class="cs88F66593">Форматированый текст</span></p></body>
但帖子来了简单的文字,没有任何选择,字体等。 这是一种发送方法
try
{
string login = ConfigurationManager.AppSettings["EmailLogin"];
string password = ConfigurationManager.AppSettings["EmailPassword"];
MailMessage mail = new MailMessage();
mail.From = new MailAddress(login);
if (lbStudents.Items.Count == 0)
MessageBox.Show("Error.", "Sending massage", MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach (SmallStudent student in lbStudents.Items)
{
mail.To.Add(new MailAddress(student.Email));
}
if (txtSubject.Text.Trim() == String.Empty)
{
MessageBox.Show("Error.", "Sending massage",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
mail.Subject = txtSubject.Text.Trim();
mail.Body = GetHtmlContent(rtfEditor.DocumentRtf);
mail.IsBodyHtml = true;
mail.BodyEncoding = Encoding.UTF8;
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential(login.Split('@')[0], password);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(mail);
mail.Dispose();
}
catch (Exception exception)
{
MessageBox.Show("Exception: " + exception.Message,
"Sending massage",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
答案 0 :(得分:2)
多个电子邮件客户端(包括Gmail网络邮件)不支持<style>
元素;见CSS Support Guide for Email Clients。
通过style="..."
属性内联您的样式,您将获得更好的兼容性:
<body>
<p style="text-align:center;text-indent:0pt;margin:0pt 0pt 0pt 0pt">
<span style="color:#800080;background-color:transparent;font-family:Arial;font-size:8pt;font-weight:bold;font-style:normal;">Форматированый текст</span>
</p>
</body>
这可能会引入大量重复,因此如果您有许多样式元素,请考虑使用自动流程来内联样式。