简体   繁体   English

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

[英]Set Content-Transfer-Encoding to Quoted-printable with .NET MailMessage

When i send mails with .NET SmtpClient, i have noticed the content transfer encoding is set to base64 when i check the source code of the mail. 当我用.NET SmtpClient发送邮件时,我注意到当我检查邮件的源代码时,内容传输编码设置为base64。

I need to set it to Quoted-printable. 我需要将其设置为Quoted-printable。 How can i achieve this ? 我怎样才能实现这一目标? Thanks in advance 提前致谢

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

Solution: 解:

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

Because you are setting your message.BodyEncoding to System.Text.Encoding.UTF8 , the transfer encoding is automatically set to Base64. 因为您正在设置message.BodyEncodingSystem.Text.Encoding.UTF8 ,传输编码会自动设置为Base64。 ( source in remarks section ) 来自备注部分

Depending on the reason why you need to have Quoted-printable transfer encoding, you will need to adjust your MailMessage object accordingly to have the transfer encoding set to Quoted-printable. 根据您需要使用Quoted-printable传输编码的原因,您需要相应地调整MailMessage对象,以将传输编码设置为Quoted-printable。

More reading can be done here 可以在这里阅读更多内容

When using .NET 4.5 or better, it should be possible to use 使用.NET 4.5或更高版本时,应该可以使用

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM