4
votes

Can anyone explain why this code is not working:

async Task Main()
{
    using (var smtpClient = new SmtpClient(@"127.0.0.1", 25))
    {
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        var from = new MailAddress(@"[email protected]");
        var to = new MailAddress(@"[email protected]");
        using (var message = new MailMessage())
        {
            message.Subject = "Email Subject";
            message.Body = "<html><head></head><body><h1>Hello World!</h1></body></html>";
            message.IsBodyHtml = true;
            message.From = from;
            message.To.Add(to);
            message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Hello World!", null, MediaTypeNames.Text.Plain));
            await smtpClient.SendMailAsync(message);
        }
    }
}

Output message is:

MIME-Version: 1.0 From: [email protected] To: [email protected] Date: 12 May 2016 14:51:30 +0200 Subject: Email Subject Content-Type: multipart/alternative; boundary=--boundary_2_be88a42a-4d48-4096-b4e0-71fb7857809f

----boundary_2_be88a42a-4d48-4096-b4e0-71fb7857809f Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable

Hello World!

----boundary_2_be88a42a-4d48-4096-b4e0-71fb7857809f Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable

Hello World! ----boundary_2_be88a42a-4d48-4096-b4e0-71fb7857809f--

This code is working:

async Task Main()
{
    using (var smtpClient = new SmtpClient(@"127.0.0.1", 25))
    {
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        var from = new MailAddress(@"[email protected]");
        var to = new MailAddress(@"[email protected]");
        using (var message = new MailMessage())
        {
            message.Subject = "Email Subject";
            message.Body = "Hello World!";
            message.IsBodyHtml = false;
            message.From = from;
            message.To.Add(to);
            message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<html><head></head><body><h1>Hello World!</h1></body></html>", null, MediaTypeNames.Text.Html));
            await smtpClient.SendMailAsync(message);
        }
    }
}

Output is fine

MIME-Version: 1.0 From: [email protected] To: [email protected] Date: 12 May 2016 14:46:52 +0200 Subject: Email Subject Content-Type: multipart/alternative; boundary=--boundary_1_2d888597-e405-40cb-8bd4-1bfcba04fb44

----boundary_1_2d888597-e405-40cb-8bd4-1bfcba04fb44 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable

Hello World! ----boundary_1_2d888597-e405-40cb-8bd4-1bfcba04fb44 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: quoted-printable

Hello World!

----boundary_1_2d888597-e405

-40cb-8bd4-1bfcba04fb44--

And this code is working:

async Task Main()
{
    using (var smtpClient = new SmtpClient(@"127.0.0.1", 25))
    {
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        var from = new MailAddress(@"[email protected]");
        var to = new MailAddress(@"[email protected]");
        using (var message = new MailMessage())
        {
            message.Subject = "Email Subject";
            message.From = from;
            message.To.Add(to);
            message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<html><head></head><body><h1>Hello World!</h1></body></html>", null, MediaTypeNames.Text.Html));
            message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Hello World!", null, MediaTypeNames.Text.Plain));
            await smtpClient.SendMailAsync(message);
        }
    }
}

Output is fine also:

MIME-Version: 1.0 From: [email protected] To: [email protected] Date: 12 May 2016 15:07:24 +0200 Subject: Email Subject Content-Type: multipart/alternative; boundary=--boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2

----boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: quoted-printable

Hello World!

----boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable

Hello World! ----boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2--

Edit: can anyone help in having html tags not interpreted in outputs?

1
change MediaTypeNames.Text.Plain to MediaTypeNames.Text.HtmlMer Hardik
Have you had a look here? msdn.microsoft.com/en-us/library/…JaggenSWE
@merhardik why that? If content is plain text you set it to plain text.JuChom
@JaggenSWE this code is the same as the 2nd codeJuChom

1 Answers

5
votes

I faced the exact same issue yesterday; Here's what I understood thanks to the MSDN reading pointed out in the comments.

When you send a message, the Body property is dedicated to text content of the email as stated here link

Use the Body property to specify the text version and use the AlternateViews collection to specify views with other MIME types

For whatever reason it seems that if you add any AlternateView after you set your Body content, the Body would get reset to Plain/Text.

To me, your last example seems to be the way to go and make things much more dynamic;

message.Subject = "Email Subject";
message.From = from;
message.To.Add(to);
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<html><head></head><body><h1>Hello World!</h1></body></html>", null, MediaTypeNames.Text.Html));
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Hello World!", null, MediaTypeNames.Text.Plain));
await smtpClient.SendMailAsync(message);