使用.NET MailMessage将Content-Transfer-Encoding设置为Quoted-printable

时间:2015-01-19 12:44:10

标签: c# .net smtpclient

当我使用.NET SmtpClient发送邮件时,我注意到当我检查邮件的源代码时,内容传输编码设置为base64。

我需要将其设置为Quoted-printable。 我怎样才能实现这一目标? 提前致谢

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

3 个答案:

答案 0 :(得分:6)

解决方案:

AlternateView plainTextView = AlternateView.CreateAlternateViewFromString(body.Trim(), new ContentType("text/html; charset=UTF-8"));
plainTextView.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
message.AlternateViews.Add(plainTextView);

答案 1 :(得分:1)

由于您要将message.BodyEncoding设置为System.Text.Encoding.UTF8,因此传输编码会自动设置为Base64。 (source in remarks section

根据您需要使用引号可打印传输编码的原因,您需要相应地调整MailMessage对象,以便将传输编码设置为Quoted-printable。

可以进行更多阅读here

答案 2 :(得分:1)

使用.NET 4.5或更高版本时,应该可以使用

message.BodyTransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;

请参见https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.bodytransferencoding?view=netframework-4.5