SMTP客户端未翻译内联HTML
本文关键字:HTML 翻译 客户端 SMTP | 更新日期: 2023-09-27 17:49:36
我使用SMTP/.NET邮件将内联HTML发送到电子邮件时遇到问题。我尝试过标准类和MailDefinitionion类,但我一直在内联原始代码,而不是翻译后的页面。
我知道这可能是我缺少的一些小东西,下面是我的代码。感谢
public static void SendMail(string toAddress, string subject, string messageBody)
{
MailDefinition message = new MailDefinition();
message.CC = MailCCAddress;
message.From = "orders@test.com";
message.Subject = subject;
message.IsBodyHtml = true;
ListDictionary replacements = new ListDictionary();
System.Net.Mail.MailMessage fileMsg;
fileMsg = message.CreateMailMessage(toAddress, replacements, messageBody, new System.Web.UI.Control());
string _hostName = HostName;
SmtpClient client = new SmtpClient(_hostName);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
//client.Send(message);
client.Send(fileMsg);
}
如果仍有问题,您可能还希望设置MailMessage
Body Encoding。例如,
//Nick's code
MailMessage mail = new MailMessage("orders@test.com", toAddress, subject, messageBody);
mail.IsBodyHtml = true;
//set encoding
mail.BodyEncoding = Encoding.UTF8; //or SevenBit, etc, whatever is appropriate.
//send
SmtpClient client = new SmtpClient(_hostName);
client.Send(mail);
您还可以使用AlternateViews集合,它可以更好地控制内容的MIME类型(因此您可以将其指定为text/html等(。您还可以使用它来选择性地包括纯文本版本和HTML版本,如以下所示:
//create the mail message
MailMessage mail = new MailMessage("orders@test.com", toAddress) { Subject = subject };
//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
//then we create the Html part
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by those mail clients that support html</b>", null, "text/html");
//add both views
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
//send the message as before
MailMessage mail = new MailMessage("orders@test.com", toAddress,
subject, messageBody);
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient(_hostName);
client.Send(mail);
如果这不起作用,那么可能是你的messageBody有问题。您可以尝试调试代码,看看messageBody是否是正确的html,或者它可能有一些字符转义。