tags:

views:

826

answers:

4

I have an ASP.Net/MVC application and I'm trying to send HTML emails. I'm doing this by reading in an HTML file with tokens, then replacing the tokens. That part is fine and generates HTML that is exactly what I want, but when I send the email, what I'm receiving looks like -

<style type=3D"text/css">=
=0D=0A.styleTitles=0D=0A{=0D=0Afont-weight:=bold;=0D=0A}=0D=0A 
.style1=0D=0A        {=0D=0A

and should look like

    <style type="text/css">
    .styleTitles
    {
        font-weight: bold;
    }
    .style1
    {
        height: 15px;
    }

I've looked on the web and I can't seem to find the correct syntax to send the message. I've seen some solutions, but none seem to work.

My current test code is -

SmtpClient smtpclient = new SmtpClient();
MailMessage message = new MailMessage();

MailAddress SendFrom = new MailAddress("[email protected]");
MailAddress SendTo = new MailAddress("[email protected]");
MailMessage MyMessage = new MailMessage(SendFrom, SendTo);

var plainView = AlternateView.CreateAlternateViewFromString(msgBody,null,"text/html");
plainView.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
MyMessage.AlternateViews.Add(plainView);
MyMessage.IsBodyHtml = true;
MyMessage.Subject = subjectLine;
MyMessage.Body = msgBody;
smtpclient.Send(MyMessage);

Any Suggestions?

+2  A: 

Try this change:

plainView.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
Nissan Fan
Did not seem to have any effect
photo_tom
A: 

This might not be the answer you need, but have you considered using XSLT for the translation of your email messages? I'm busy with a project that sends emails, and its pretty nice to use XSLT as part of the solution. Also means in future the template can easily be customized in an industry standardized way, maybe you should consider making the change?

JL
You do have a point, but the code is already working far better than the client wanted. So a re-write is not really in order.However, I will keep this in mind for my next project
photo_tom
+1  A: 

Maybe something like this:

var plainView = AlternateView.CreateAlternateViewFromString(msgBody, new ContentType("text/plain; charset=UTF-8"));

MyMessage.AlternateViews.Add(plainView);
MyMessage.BodyEncoding = Encoding.UTF8;
MyMessage.IsBodyHtml = true;
MyMessage.Subject = subjectLine;
MyMessage.Body = msgBody;
ParmesanCodice
That results in the message being sent in base64 encoding and it seems to work. thanksI've tracked the real problem down to the fact that by default the "Content-Transfer-Encoding: quoted-printable" setting causes all the problem. The quoted-printable is designed to replace \n with =0D, \r with =0A and = as =3D. I have not found a good way to change the transfer encoding so that it is 8bit which would be simpler. Anyway. Thanks.
photo_tom
+1  A: 

To set the transfer encoding to 8bit, taken from here , you have to :

message.Body = null;
using (AlternateView body =
AlternateView.CreateAlternateViewFromString(
    "Some Message Body",
    message.BodyEncoding,
    message.IsBodyHtml ? "text/html" : null))
{
body.TransferEncoding = 
    TransferEncoding.SevenBit;
message.AlternateViews.Add(body);
}
Gary W