photo_tom
photo_tom

Reputation: 7342

Problem sending email with SmtpClient in C#

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?

Upvotes: 4

Views: 12838

Answers (6)

Muhammad Hussain
Muhammad Hussain

Reputation: 24

string emailMessage="a skjdhak kdkand"; 
MailMessage mail = new MailMessage();
                    mail.To.Add(obj_Artist.EmailAddress);
                    mail.From = new MailAddress(EmailList[0].FromEmail, "Sentric Music - Rights Management");
                       mail.Subject = (EmailList[0].Subject);

                    if (EmailList[0].BCC1 != null && EmailList[0].BCC1 != string.Empty)
                    {
                        mail.Bcc.Add(EmailList[0].BCC1);
                    }
                    if (EmailList[0].BCC2 != null && EmailList[0].BCC2 != string.Empty)
                    {
                        mail.Bcc.Add(EmailList[0].BCC2);
                    }
                    if (EmailList[0].CC1 != null && EmailList[0].CC1 != string.Empty)
                    {
                        mail.CC.Add(EmailList[0].CC1);
                    }
                    if (EmailList[0].CC2 != null && EmailList[0].CC2 != string.Empty)
                    {
                        mail.CC.Add(EmailList[0].CC2);`enter code here`
                    }


                    string Body = emailMessage;


                    mail.Body = Body;
                    mail.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
                    mail.IsBodyHtml = true;
                    AlternateView plainView = AlternateView.CreateAlternateViewFromString
                    (System.Text.RegularExpressions.Regex.Replace(Body, @"<(.|\n)*?>", string.Empty), null, "text/plain");
                    System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
                    mail.AlternateViews.Add(plainView);
                    mail.AlternateViews.Add(htmlView);
                    SmtpClient smtp = new SmtpClient();
                    smtp.EnableSsl = true;
                    smtp.Send(mail);

Upvotes: -1

Vetal
Vetal

Reputation: 29

It's strange, but more simple code is work for me:

var message = new MailMessage(Email, mailTo);
message.IsBodyHtml = true;
message.SubjectEncoding = message.BodyEncoding = Encoding.UTF8;
message.Subject = "Subject";
message.Body = msgBody;
smtpclient.Send(message);

Upvotes: 0

Gary W
Gary W

Reputation: 1904

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

Upvotes: 3

ParmesanCodice
ParmesanCodice

Reputation: 5035

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;

Upvotes: 6

JL.
JL.

Reputation: 81342

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?

Upvotes: 0

Keith Adler
Keith Adler

Reputation: 21178

Try this change:

plainView.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

Upvotes: 3

Related Questions