使用 .NET MailMessage 将 Content-Transfer-Encoding 设置为 Quoted-printable
Set Content-Transfer-Encoding to Quoted-printable with .NET MailMessage
当我使用 .NET SmtpClient 发送邮件时,当我检查邮件的源代码时,我注意到内容传输编码设置为 base64。
我需要将其设置为可引用打印。
我怎样才能做到这一点?
提前致谢
public virtual MailMessage GetMailMessage(string body, string from, string [] user_emails, string [] back_office_emails, string subject)
{
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(from);
foreach (string email in user_emails)
{
message.To.Add(new MailAddress(email));
}
if (back_office_emails != null)
{
foreach (string email in back_office_emails)
{
message.Bcc.Add(new MailAddress(email));
}
}
message.Subject = subject;
message.Body = body;
message.BodyEncoding = System.Text.Encoding.UTF8;
return message;
}
protected virtual void SendEmailTemplate(string body, string from, string[] user_emails, string[] back_office_emails, string subject)
{
MailMessage message = GetMailMessage(body, from, user_emails, back_office_emails, subject);
SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["SmtpClient"]);
client.Send(message);
}
因为您将 message.BodyEncoding
设置为 System.Text.Encoding.UTF8
,传输编码会自动设置为 Base64。 (source in remarks section)
根据您需要 Quoted-printable 传输编码的原因,您需要相应地调整 MailMessage
对象以将传输编码设置为 Quoted-printable。
可以多读书here
解决方案:
AlternateView plainTextView = AlternateView.CreateAlternateViewFromString(body.Trim(), new ContentType("text/html; charset=UTF-8"));
plainTextView.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
message.AlternateViews.Add(plainTextView);
使用 .NET 4.5 或更高版本时,应该可以使用
message.BodyTransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
当我使用 .NET SmtpClient 发送邮件时,当我检查邮件的源代码时,我注意到内容传输编码设置为 base64。
我需要将其设置为可引用打印。 我怎样才能做到这一点? 提前致谢
public virtual MailMessage GetMailMessage(string body, string from, string [] user_emails, string [] back_office_emails, string subject)
{
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(from);
foreach (string email in user_emails)
{
message.To.Add(new MailAddress(email));
}
if (back_office_emails != null)
{
foreach (string email in back_office_emails)
{
message.Bcc.Add(new MailAddress(email));
}
}
message.Subject = subject;
message.Body = body;
message.BodyEncoding = System.Text.Encoding.UTF8;
return message;
}
protected virtual void SendEmailTemplate(string body, string from, string[] user_emails, string[] back_office_emails, string subject)
{
MailMessage message = GetMailMessage(body, from, user_emails, back_office_emails, subject);
SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["SmtpClient"]);
client.Send(message);
}
因为您将 message.BodyEncoding
设置为 System.Text.Encoding.UTF8
,传输编码会自动设置为 Base64。 (source in remarks section)
根据您需要 Quoted-printable 传输编码的原因,您需要相应地调整 MailMessage
对象以将传输编码设置为 Quoted-printable。
可以多读书here
解决方案:
AlternateView plainTextView = AlternateView.CreateAlternateViewFromString(body.Trim(), new ContentType("text/html; charset=UTF-8"));
plainTextView.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
message.AlternateViews.Add(plainTextView);
使用 .NET 4.5 或更高版本时,应该可以使用
message.BodyTransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;