Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I need to set it to 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);
}
like image 666
FieryA Avatar asked Dec 24 '22 23:12

FieryA


1 Answers

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);
like image 128
FieryA Avatar answered Jan 01 '23 03:01

FieryA