MailMessage.AlternateViews Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar den samling med bifogade filer som används för att lagra alternativa former av meddelandetexten.
public:
property System::Net::Mail::AlternateViewCollection ^ AlternateViews { System::Net::Mail::AlternateViewCollection ^ get(); };
public System.Net.Mail.AlternateViewCollection AlternateViews { get; }
member this.AlternateViews : System.Net.Mail.AlternateViewCollection
Public ReadOnly Property AlternateViews As AlternateViewCollection
Egenskapsvärde
En skrivbar AlternateViewCollection.
Exempel
I följande kodexempel visas hur du skapar och skickar ett e-postmeddelande med oformaterad text och en alternativ HTML-vy.
public static void CreateMessageWithMultipleViews(string server, string recipients)
{
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"jane@contoso.com",
recipients,
"This email 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>";
ContentType mimeType = new System.Net.Mime.ContentType("text/html");
// Add the alternate body to the message.
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
message.AlternateViews.Add(alternate);
// Send the message.
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateMessageWithMultipleViews(): {0}",
ex.ToString());
}
// 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();
}
Public Shared Sub CreateMessageWithMultipleViews(ByVal server As String, ByVal recipients As String)
' Create a message and set up the recipients.
Dim message As MailMessage = New MailMessage(
"jane@contoso.com",
recipients,
"This email message has multiple views.",
"This is some plain text.")
' Construct the alternate body as HTML.
Dim body As String = "<!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>"
Dim mimeType As ContentType = New System.Net.Mime.ContentType("text/html")
' Add the alternate body to the message.
Dim alternate As AlternateView = AlternateView.CreateAlternateViewFromString(body, mimeType)
message.AlternateViews.Add(alternate)
' Send the message.
Dim client As SmtpClient = New SmtpClient(server)
client.Credentials = CredentialCache.DefaultNetworkCredentials
Try
client.Send(message)
Catch ex As Exception
Console.WriteLine("Exception caught in CreateMessageWithMultipleViews(): {0}", ex.ToString())
End Try
' Display the values in the ContentType for the attachment.
Dim c As ContentType = 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)
For Each d As DictionaryEntry In c.Parameters
Console.WriteLine("{0} = {1}", d.Key, d.Value)
Next
Console.WriteLine()
alternate.Dispose()
End Sub
Kommentarer
Använd egenskapen AlternateViews för att ange kopior av ett e-postmeddelande i olika format. Om du till exempel skickar ett meddelande i HTML kanske du också vill ange en oformaterad textversion om vissa mottagare använder e-postläsare som inte kan visa HTML-innehåll.
Om du vill lägga till en alternativ vy i ett MailMessage objekt skapar du en Attachment för vyn och lägger sedan till den i samlingen som returneras av AlternateViews. Använd egenskapen Body för att ange textversionen och använda AlternateViews samlingen för att ange vyer med andra MIME-typer. MediaTypeNames Använd klassmedlemmarna för att ange MIME-typen för den alternativa vyn.