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(@"sender@mydomain.com");
var to = new MailAddress(@"receiver@mydomain.com");
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: sender@mydomain.com To: receiver@mydomain.com
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-printableHello World!
—-boundary_2_be88a42a-4d48-4096-b4e0-71fb7857809f Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding:
quoted-printableHello 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(@"sender@mydomain.com");
var to = new MailAddress(@"receiver@mydomain.com");
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: sender@mydomain.com To: receiver@mydomain.com
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-printableHello World!
—-boundary_1_2d888597-e405-40cb-8bd4-1bfcba04fb44 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding:
quoted-printableHello 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(@"sender@mydomain.com");
var to = new MailAddress(@"receiver@mydomain.com");
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: sender@mydomain.com To: receiver@mydomain.com
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-printableHello World!
—-boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding:
quoted-printableHello World!
—-boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2–
Edit: can anyone help in having html tags not interpreted in outputs?
Best Answer
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
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;