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-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(@"[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-printableHello World! ----boundary_3_fa7662d6-404a-4c86-a775-373355aa41e2--
Edit: can anyone help in having html tags not interpreted in outputs?