Bug in ASP.NET 2.0: AlternateViews MIME Email

W

warren

To add a text and body part of an email MSDN help has an example:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref10/html/P_System_Net_Mail_MailMessage_AlternateViews.htm

Which lists the code I have below (doesn't work). I get an illegal
characters in path error. This appears to be a bug or a typo in the
code example?

public static void CreateMessageWithMultipleViews(string server, string
recipients)
{
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"(e-mail address removed)",
recipients,
"This e-mail message has multiple views.",
"This is some plain text.");

// Construct the alternate body as HTML.
string body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0
Transitional//EN\">";
body += "<HTML><HEAD><META http-equiv=Content-Type
content=\"text/html; charset=iso-8859-1\">";
body += "</HEAD><BODY><DIV><FONT face=Arial color=#ff0000
size=2>this is some HTML text";
body += "</FONT></DIV></BODY></HTML>";

// Add the alternate body to the message.
AlternateView alternate = new AlternateView(body,
MediaTypeNames.Text.Html);
message.AlternateViews.Add(alternate);

// Send the message.
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
// Display the values in the ContentType for the attachment.
ContentType c = alternate.ContentType;
Console.WriteLine("Content type");
Console.WriteLine(c.ToString());
Console.WriteLine("Boundary {0}", c.Boundary);
Console.WriteLine("CharSet {0}", c.CharSet);
Console.WriteLine("MediaType {0}", c.MediaType);
Console.WriteLine("Name {0}", c.Name);
Console.WriteLine("Parameters: {0}", c.Parameters.Count);
foreach (DictionaryEntry d in c.Parameters)
{
Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
Console.WriteLine();
alternate.Dispose();
}
 
M

Mark Walker

This is a really old thread but I just had the same problem. Wasn't hard to
work out but in case someone else has this problem...

The overload that takes text as the first parameter is expecting a file path
for an attachment. Instead in this case you would want to convert to a
stream:

// Add the alternate body to the message.
//Convert text to stream
Stream stream = new MemoryStream(Encoding.ASCII.GetBytes(body));
AlternateView alternate = new AlternateView(stream, MediaTypeNames.Text.Html)
;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top