ContentType.Parameters 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取包含由此实例表示的 Content-Type 标头中包含的参数的字典。
public:
property System::Collections::Specialized::StringDictionary ^ Parameters { System::Collections::Specialized::StringDictionary ^ get(); };
public System.Collections.Specialized.StringDictionary Parameters { get; }
member this.Parameters : System.Collections.Specialized.StringDictionary
Public ReadOnly Property Parameters As StringDictionary
属性值
可写的 StringDictionary,包含名称和值。
示例
下面的代码示例显示此属性返回的字典中的值。
static void CreateMessageWithMultipleViews( String^ server, String^ recipients )
{
// Create a message and set up the recipients.
MailMessage^ message = gcnew MailMessage( L"jane@contoso.com",recipients,L"This email message has multiple views.",L"This is some plain text." );
// Construct the alternate body as HTML.
String^ body = L"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
body = String::Concat( body, L"<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">" );
body = String::Concat( body, L"</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text" );
body = String::Concat( body, L"</FONT></DIV></BODY></HTML>" );
// Add the alternate body to the message.
AlternateView^ alternate = AlternateView::CreateAlternateViewFromString(body);
message->AlternateViews->Add(alternate);
// Send the message.
SmtpClient^ client = gcnew SmtpClient( server );
client->Credentials = CredentialCache::DefaultNetworkCredentials;
client->Send( message );
// Display the values in the ContentType for the attachment.
ContentType^ c = alternate->ContentType;
Console::WriteLine( L"Content type" );
Console::WriteLine( c );
Console::WriteLine( L"Boundary {0}", c->Boundary );
Console::WriteLine( L"CharSet {0}", c->CharSet );
Console::WriteLine( L"MediaType {0}", c->MediaType );
Console::WriteLine( L"Name {0}", c->Name );
Console::WriteLine( L"Parameters: {0}", c->Parameters->Count );
IEnumerator^ myEnum = c->Parameters->GetEnumerator();
while ( myEnum->MoveNext() )
{
DictionaryEntry^ d = safe_cast<DictionaryEntry^>(myEnum->Current);
Console::WriteLine( L"{0} = {1}", d->Key, d->Value );
}
Console::WriteLine();
alternate->~AlternateView();
}
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();
}
注解
可以通过在构造ContentType对象时指定整个 Content-Header 值来设置参数,也可以向 属性返回的 Parameters 添加参数StringDictionary。
将参数项添加到字典时,参数的名称是条目的键,参数的值是条目的值。
RFC 2045 第 5.1 节介绍了详细说明内容类型标头语法的语法。 RFC 2046 提供有关 MIME 媒体类型及其关联参数的详细信息。 这些 RFC 在 中可用 https://www.ietf.org。