How to prevent HTML Email from having quoted printable when sending with C# using System.Net.Mail.MaiMessage

StackOverflow https://stackoverflow.com/questions/18438666

Question

I am attempting to send an email to a gmail address using C# VS2012 .NET 4.5 System.Net.Mail.MailMessage.

The email get's sent but always has:

Content-Transfer-Encoding: quoted-printable

when I view the source of the email in the mail client (gmail web mail client).

I have tried every different combination I can think of with BodyEncoding, and BodyTransferEnconding and DeliveryFormat etc... but nothing can get rid of this quoted-printable.

The problem is that the CSS is not working in the email. The HTML renders ok, but not CSS is applied, and when I view the email source, I see that it has this 3D inserted after all the = signs and this is preventing my CSS from working.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir=3D"ltr" xml:lang=3D"en" xmlns=3D"http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv=3D"Content-Type" content=3D"Type=3Dtext/html; charset=3Dut=f-8" />
<style type=3D"text/css">
    ...

The code I am using to send the email is as follows:

using (MailMessage _MailMessage = new MailMessage("[SENDER_EMAIL_REMOVED]", "[RECIPIENT_EMAIL_REMOVED]"))
{
    SmtpClient _SmtpClient = new SmtpClient("smpt.gmail.com", 587);
    _SmtpClient.EnableSsl = true;
    _SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    _SmtpClient.UseDefaultCredentials = false;
    _SmtpClient.Credentials = new System.Net.NetworkCredential("[USERNAME_REMOVED]","[PASSWORD_REMOVED]");
    _MailMessage.Subject = ""Test Email";
    _MailMessage.BodyEncoding = System.Text.Encoding.UTF8;
    _MailMessage.BodyTransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
    _SmtpClient.DeliveryFormat = SmtpDeliveryFormat.SevenBit;
    _MailMessage.Body = _MessageBody;
}

The HTML I am sending is loaded from a string that was serialized into the web.config and is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="Type=text/html; charset=utf-8" />
<title>Page Title</title>
<style type="text/css">
 .style1 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; }
 .style2 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight:bold; }
 .tblRowHdrCol {font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight:bold; background-color:#f0f0f0; width:180px; }
 .tblRowValCol {font-family: Arial, Helvetica, sans-serif; font-size: 12px; padding-left:10px; width:100%; }
</style>
</head>
<body>
<p class="style1"><img src="http://www.domain.comn/image.jpg" alt="Some Image" /></p>
<p class="style1"> 
Some text here...

</p>

    <table width="100%">
        <tr>
            <td class="tblRowHdrCol">Field One:</td>
            <td class="tblRowValCol">[FIELD_ONE]</td>
        </tr>
        <tr>
            <td class="tblRowHdrCol">Field Two:</td>
            <td class="tblRowValCol">[FIELD_TWO]</td>
        </tr>
        <tr>
            <td class="tblRowHdrCol">Field Three:</td>
            <td class="tblRowValCol">[FIELD_THREE]</td>
        </tr>
        <tr>
            <td class="tblRowHdrCol">Field Four:</td>
            <td class="tblRowValCol">[FIELD_FOUR]</td>
        </tr>
        <tr>
            <td class="tblRowHdrCol" colspan="2">Exception Details:</td>
        </tr>
        <tr>
            <td class="tblRowValCol" colspan="2" style="padding-left:1px">[EXCEPTION_DETAILS]</td>
        </tr>
    </table>
</body>
</html>

So I load this in and do a find and replace on the [] place holders.

How can I resolve this issue?

I even tried adding a header to set it to 7BIT:

_MailMessage.Headers.Add("Content-Transfer-Encoding", "7BIT");

It added this but still had the quoted-printable above it when I viewed the source of the email in gmail.

Regards,

Scott

Was it helpful?

Solution

From what I understand, you can't get the css to work. I think you have to convert the html so that the style are taken out of the style tag and placed inline to the html tags. There are many tool that do this, if you want to do this dynamically in C# take a look at this: Inlining CSS in C#

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top