HTML Mail using SmtpClient
Hi,
I'm using .NET 2.0 (Beta2) and I want to send an email where the body is html.
I'm using the System.Net.Mail.SmtpClient and System.Net.Mail.MailMessage classes. Before sending the MailMessage I add an alternteView using
message.AlternateViews.Add(
AlternateView.CreateAlternateViewFromString(<html string>,null,"text/html"));but when I receive the sent mail it is blank. If I set the MailMessage.Body property to the Html string the it arrives as raw HTML, again in plain text.
MailMessage no longer has a BodyFormat property as in the .NET 1.1 framework!!
Can anyone help me?
Regards
Graham
[833 byte] By [
Gravy] at [2008-2-8]
A little more info for you...
The above code works it I send the mail synchronously using SmtpClient.Send. But if I use SendAsync it doesn't work!!
I have noticed too that the mail doesn't actually get sent until my app is closed!! Is there something I should do to flush the smtpClient after calling Send or SendAsync?
Thanks
Graham
Thanks for the reply Mike. I can't test this until Tuesday next week but I will let you know if your sample code works. However, the code I was using looks like:
smtpClient =
new SmtpClient();
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtpClient.Host = <hostname here>;
using (MailMessage message = new MailMessage())
{
message.From =
new MailAddress(systemEmail); message.To.Add(user.Email);
message.Subject = "A Message";
message.AlternateViews.Add(
AlternateView.CreateAlternateViewFromString("<H1>Hello</H1>", null, "text/html")); smtpClient.Send(message);
}