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]
# 1
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

Gravy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2

Can you post some sample code that you are using to see this problem? What build of the framework are you using? Your code should look something similar to the following (possibly without the linked resources)



//CREATE A MAIL CLIENT
SmtpClient MailClient = new SmtpClient("localhost");

//SET FROM, TO
MailMessage Message = new MailMessage("user@test.com", "user@test.com");
//SET SUBJECT
Message.Subject = "System.Net.Mail is easy";

//LINKED RESOURCES
LinkedResource expresssql = new LinkedResource("expresssql.jpg", "image/jpeg");
expresssql.ContentLink = new Uri("http://lab.msdn.microsoft.com/vs2005/art/expresssql.jpg");

//ADD Linked Resources
AlternateView htmlBody = new AlternateView("VS2005.htm", "text/html");
htmlBody.LinkedResources.Add(expresssql);

//Add it to the Message
Message.AlternateViews.Add(htmlBody);
//SEND IT!!!
MailClient.Send(Message);



MikeFlasko at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3
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);

}

Gravy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 4
You can do the following

Message.Body = "....";
Message.IsBodyHTML = true;

I believe we added this property in Beta2. It should work without you having to use alternate views

DurgaprasadGorti at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 5
D'oh. the simplest things are most often the ones overlooked. Thanks.
Gravy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified