ContentType コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
ContentType クラスの新しいインスタンスを初期化します。
オーバーロード
ContentType() |
ContentType クラスの新しい既定のインスタンスを初期化します。 |
ContentType(String) |
指定した文字列を使用して、ContentType クラスの新しいインスタンスを初期化します。 |
ContentType()
- ソース:
- ContentType.cs
- ソース:
- ContentType.cs
- ソース:
- ContentType.cs
ContentType クラスの新しい既定のインスタンスを初期化します。
public:
ContentType();
public ContentType ();
Public Sub New ()
例
次のコード例では、このコンストラクターを呼び出す方法を示します。
// The following example sends a summary of a log file as the message
// and the log as an email attachment.
static void SendNamedErrorLog( String^ server, String^ recipientList )
{
// Create a message from logMailer@contoso.com to recipientList.
MailMessage^ message = gcnew MailMessage( L"logMailer@contoso.com",recipientList );
message->Subject = L"Error Log report";
String^ fileName = L"log.txt";
// Get the file stream for the error log.
// Requires the System.IO namespace.
FileStream^ fs = gcnew FileStream( fileName,FileMode::Open,FileAccess::Read );
StreamReader^ s = gcnew StreamReader( fs );
int errors = 0;
while ( s->ReadLine() != nullptr )
{
// Process each line from the log file here.
errors++;
}
message->Body = String::Format( L"{0} errors in log as of {1}", errors, DateTime::Now );
// Close the stream reader. This also closes the file.
s->Close();
// Re-open the file at the beginning to make the attachment.
fs = gcnew FileStream( fileName,FileMode::Open,FileAccess::Read );
// Make a ContentType indicating that the log data
// that is attached is plain text and is named.
ContentType^ ct = gcnew ContentType;
ct->MediaType = MediaTypeNames::Text::Plain;
ct->Name = String::Format( L"log{0}.txt", DateTime::Now );
// Create the attachment.
Attachment^ data = gcnew Attachment( fs,ct );
// Add the attachment to the message.
message->Attachments->Add( data );
// Send the message.
// Include credentials if the server requires them.
SmtpClient^ client = gcnew SmtpClient( server );
client->Credentials = CredentialCache::DefaultNetworkCredentials;
client->Send( message );
data->~Attachment();
client->~SmtpClient();
// Close the log file.
fs->Close();
return;
}
// The following example sends a summary of a log file as the message
// and the log as an email attachment.
public static void SendNamedErrorLog(string server, string recipientList)
{
// Create a message from logMailer@contoso.com to recipientList.
MailMessage message = new MailMessage(
"logMailer@contoso.com", recipientList);
message.Subject = "Error Log report";
string fileName = "log.txt";
// Get the file stream for the error log.
// Requires the System.IO namespace.
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader s = new StreamReader(fs);
int errors = 0;
while (s.ReadLine() != null)
{
// Process each line from the log file here.
errors++;
}
// The email message summarizes the data found in the log.
message.Body = String.Format("{0} errors in log as of {1}",
errors, DateTime.Now);
// Close the stream reader. This also closes the file.
s.Close();
// Re-open the file at the beginning to make the attachment.
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
// Make a ContentType indicating that the log data
// that is attached is plain text and is named.
ContentType ct = new ContentType();
ct.MediaType = MediaTypeNames.Text.Plain;
ct.Name = "log" + DateTime.Now.ToString() + ".txt";
// Create the attachment.
Attachment data = new Attachment(fs, ct);
// Add the attachment to the message.
message.Attachments.Add(data);
// Send the message.
// Include credentials if the server requires them.
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in SendNamedErrorLog: {0}",
ex.ToString());
}
data.Dispose();
// Close the log file.
fs.Close();
return;
}
注釈
このコンストラクターは、MediaType プロパティを "application/octet-stream"
に初期化します。
適用対象
ContentType(String)
- ソース:
- ContentType.cs
- ソース:
- ContentType.cs
- ソース:
- ContentType.cs
指定した文字列を使用して、ContentType クラスの新しいインスタンスを初期化します。
public:
ContentType(System::String ^ contentType);
public ContentType (string contentType);
new System.Net.Mime.ContentType : string -> System.Net.Mime.ContentType
Public Sub New (contentType As String)
パラメーター
- contentType
- String
MIME のメディア タイプ、サブタイプ、および省略可能なパラメーターを格納している String (例: "text/plain; charset=us-ascii"
)。
例外
contentType
が null
です。
contentType
が Empty ("") です。
contentType
が、解析できない形式です。
例
次のコード例では、このコンストラクターを呼び出す方法を示します。
// The following example sends a summary of a log file as the message
// and the log as an email attachment.
static void SendNamedErrorLog( String^ server, String^ recipientList )
{
// Create a message from logMailer@contoso.com to recipientList.
MailMessage^ message = gcnew MailMessage( L"logMailer@contoso.com",recipientList );
message->Subject = L"Error Log report";
String^ fileName = L"log.txt";
// Get the file stream for the error log.
// Requires the System.IO namespace.
FileStream^ fs = gcnew FileStream( fileName,FileMode::Open,FileAccess::Read );
StreamReader^ s = gcnew StreamReader( fs );
int errors = 0;
while ( s->ReadLine() != nullptr )
{
// Process each line from the log file here.
errors++;
}
message->Body = String::Format( L"{0} errors in log as of {1}", errors, DateTime::Now );
// Close the stream reader. This also closes the file.
s->Close();
// Re-open the file at the beginning to make the attachment.
fs = gcnew FileStream( fileName,FileMode::Open,FileAccess::Read );
// Make a ContentType indicating that the log data
// that is attached is plain text and is named.
ContentType^ ct = gcnew ContentType;
ct->MediaType = MediaTypeNames::Text::Plain;
ct->Name = String::Format( L"log{0}.txt", DateTime::Now );
// Create the attachment.
Attachment^ data = gcnew Attachment( fs,ct );
// Add the attachment to the message.
message->Attachments->Add( data );
// Send the message.
// Include credentials if the server requires them.
SmtpClient^ client = gcnew SmtpClient( server );
client->Credentials = CredentialCache::DefaultNetworkCredentials;
client->Send( message );
data->~Attachment();
client->~SmtpClient();
// Close the log file.
fs->Close();
return;
}
// The following example sends a summary of a log file as the message
// and the log as an email attachment.
public static void SendNamedErrorLog(string server, string recipientList)
{
// Create a message from logMailer@contoso.com to recipientList.
MailMessage message = new MailMessage(
"logMailer@contoso.com", recipientList);
message.Subject = "Error Log report";
string fileName = "log.txt";
// Get the file stream for the error log.
// Requires the System.IO namespace.
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader s = new StreamReader(fs);
int errors = 0;
while (s.ReadLine() != null)
{
// Process each line from the log file here.
errors++;
}
// The email message summarizes the data found in the log.
message.Body = String.Format("{0} errors in log as of {1}",
errors, DateTime.Now);
// Close the stream reader. This also closes the file.
s.Close();
// Re-open the file at the beginning to make the attachment.
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
// Make a ContentType indicating that the log data
// that is attached is plain text and is named.
ContentType ct = new ContentType();
ct.MediaType = MediaTypeNames.Text.Plain;
ct.Name = "log" + DateTime.Now.ToString() + ".txt";
// Create the attachment.
Attachment data = new Attachment(fs, ct);
// Add the attachment to the message.
message.Attachments.Add(data);
// Send the message.
// Include credentials if the server requires them.
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in SendNamedErrorLog: {0}",
ex.ToString());
}
data.Dispose();
// Close the log file.
fs.Close();
return;
}
注釈
文字列の contentType
構文については、RFC 2045 セクション 5.1 で https://www.ietf.org説明されています。
適用対象
.NET