1

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

ItalyPaleAle
  • 7,185
  • 6
  • 42
  • 69
user2109254
  • 1,709
  • 2
  • 30
  • 49

1 Answers1

0

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#

Community
  • 1
  • 1
noamtcohen
  • 4,432
  • 2
  • 20
  • 14
  • Thanks for responding mate. That got it to work, even with the 3D's inserted after ever = sign. The thing that threw me was I was looking at a Telerik HTML email I got, viewing the source, and they had styles in a style element in the header, and their Content-Transfer-Encoding was 7BIT, and they didn't have the infamous 3D inserted everywhere. But when I looked more closely I found that they were not actually using the styles in the header, and they did have inline styles. Thanks again ;-) – user2109254 Aug 26 '13 at 23:27
  • This doesn't make a difference, as "quoted-printable" encoding breaks inline styles too. The question is, how to *prevent* QP encoding, but switching to inline styles has no effect on QP encoding. If there is a way to prevent this, I would really like to know, because my entire Gmail inbox is QP encoded as of a few days ago--no idea why! https://en.wikipedia.org/wiki/Quoted-printable – PJ Brunet Jan 31 '17 at 20:28