Attachment コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
Attachment クラスの新しいインスタンスを初期化します。
オーバーロード
| 名前 | 説明 |
|---|---|
| Attachment(String) |
指定したコンテンツ文字列を使用して、 Attachment クラスの新しいインスタンスを初期化します。 |
| Attachment(Stream, ContentType) |
指定したストリームとコンテンツ タイプを使用して、 Attachment クラスの新しいインスタンスを初期化します。 |
| Attachment(Stream, String) |
指定したストリームと名前を使用して、 Attachment クラスの新しいインスタンスを初期化します。 |
| Attachment(String, ContentType) |
指定したコンテンツ文字列とContentTypeを使用して、Attachment クラスの新しいインスタンスを初期化します。 |
| Attachment(String, String) |
指定したコンテンツ文字列と MIME の種類の情報を使用して、 Attachment クラスの新しいインスタンスを初期化します。 |
| Attachment(Stream, String, String) |
指定したストリーム、名前、および MIME の種類の情報を使用して、 Attachment クラスの新しいインスタンスを初期化します。 |
Attachment(String)
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
指定したコンテンツ文字列を使用して、 Attachment クラスの新しいインスタンスを初期化します。
public:
Attachment(System::String ^ fileName);
public Attachment(string fileName);
new System.Net.Mail.Attachment : string -> System.Net.Mail.Attachment
Public Sub New (fileName As String)
パラメーター
例外
fileName は nullです。
fileName が空です。
例
次のコード例は、このコンストラクターを呼び出す方法を示しています。
public static void CreateMessageInlineAttachment2(string server, string
textMessage)
{
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"jane@contoso.com",
"ben@contoso.com",
"A text message for you.",
"Message: ");
// Attach the message string to this email message.
Attachment data = new Attachment(textMessage);
// Send textMessage as part of the email body.
message.Attachments.Add(data);
ContentType content = data.ContentType;
content.MediaType = MediaTypeNames.Text.Plain;
//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 CreateMessageInlineAttachment2: {0}",
ex.ToString());
}
data.Dispose();
}
注釈
プロパティは次のように設定されます。
| 財産 | 価値 |
|---|---|
| MediaType | Plain。 |
| TransferEncoding | QuotedPrintable。 |
適用対象
Attachment(Stream, ContentType)
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
指定したストリームとコンテンツ タイプを使用して、 Attachment クラスの新しいインスタンスを初期化します。
public:
Attachment(System::IO::Stream ^ contentStream, System::Net::Mime::ContentType ^ contentType);
public Attachment(System.IO.Stream contentStream, System.Net.Mime.ContentType contentType);
new System.Net.Mail.Attachment : System.IO.Stream * System.Net.Mime.ContentType -> System.Net.Mail.Attachment
Public Sub New (contentStream As Stream, contentType As ContentType)
パラメーター
- contentType
- ContentType
contentStream内のデータを記述するContentType。
例外
例
次のコード例は、このコンストラクターを呼び出す方法を示しています。
// The following example sends a summary of a log file as the message
// and the log as an email attachment.
public static void SendErrorLog(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.
ContentType ct = new ContentType(MediaTypeNames.Text.Plain);
// Attach the log file stream to the email message.
Attachment data = new Attachment(fs, ct);
ContentDisposition disposition = data.ContentDisposition;
// Suggest a file name for the attachment.
disposition.FileName = "log" + DateTime.Now.ToString() + ".txt";
// 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 SendErrorLog: {0}",
ex.ToString());
}
data.Dispose();
// Close the log file.
fs.Close();
}
注釈
TransferEncoding プロパティが Base64 に設定されている。
ストリームの CanSeek プロパティが falseされている場合、添付ファイルとそれを含む MailMessage は再利用できません。 添付ファイルを再利用するには、検索できるストリームを指定する必要があります。
適用対象
Attachment(Stream, String)
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
指定したストリームと名前を使用して、 Attachment クラスの新しいインスタンスを初期化します。
public:
Attachment(System::IO::Stream ^ contentStream, System::String ^ name);
public Attachment(System.IO.Stream contentStream, string? name);
public Attachment(System.IO.Stream contentStream, string name);
new System.Net.Mail.Attachment : System.IO.Stream * string -> System.Net.Mail.Attachment
Public Sub New (contentStream As Stream, name As String)
パラメーター
- name
- String
この添付ファイルに関連付けられているContentTypeのName プロパティの値を格納しているString。 この値は、null の場合もあります。
例外
contentStream は nullです。
例
次のコード例は、このコンストラクターを呼び出す方法を示しています。
// 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;
}
注釈
nameがString.Empty ("") とnullまたは等しくない場合、この添付ファイルのContentTypeは、Name プロパティを name に設定して構築されます。
TransferEncoding プロパティが Base64 に設定されている。
ストリームの CanSeek プロパティが falseされている場合、添付ファイルとそれを含む MailMessage は再利用できません。 添付ファイルを再利用するには、検索できるストリームを指定する必要があります。
適用対象
Attachment(String, ContentType)
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
指定したコンテンツ文字列とContentTypeを使用して、Attachment クラスの新しいインスタンスを初期化します。
public:
Attachment(System::String ^ fileName, System::Net::Mime::ContentType ^ contentType);
public Attachment(string fileName, System.Net.Mime.ContentType contentType);
new System.Net.Mail.Attachment : string * System.Net.Mime.ContentType -> System.Net.Mail.Attachment
Public Sub New (fileName As String, contentType As ContentType)
パラメーター
- contentType
- ContentType
fileName内のデータを記述するContentType。
例外
fileName は nullです。
contentType が正しい形式ではありません。
適用対象
Attachment(String, String)
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
指定したコンテンツ文字列と MIME の種類の情報を使用して、 Attachment クラスの新しいインスタンスを初期化します。
public:
Attachment(System::String ^ fileName, System::String ^ mediaType);
public Attachment(string fileName, string? mediaType);
public Attachment(string fileName, string mediaType);
new System.Net.Mail.Attachment : string * string -> System.Net.Mail.Attachment
Public Sub New (fileName As String, mediaType As String)
パラメーター
例外
fileName は nullです。
mediaType が正しい形式ではありません。
例
次のコード例は、このコンストラクターを呼び出す方法を示しています。
public static void CreateMessageInlineAttachment(string server, string
textMessage)
{
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"jane@contoso.com",
"ben@contoso.com",
"An inline text message for you.",
"Message: ");
// Attach the message string to this email message.
Attachment data = new Attachment(textMessage, MediaTypeNames.Text.Plain);
// Send textMessage as part of the email body.
message.Attachments.Add(data);
ContentDisposition disposition = data.ContentDisposition;
disposition.Inline = true;
//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 CreateMessageInlineAttachment: {0}",
ex.ToString());
}
data.Dispose();
}
注釈
mediaTypeがnullまたはString.Empty ("") と等しい場合、この添付ファイルのMediaTypeプロパティはPlainに設定されます。
mediaTypeがnullではなく、長さ 0 の文字列でない場合は、この添付ファイルに関連付けられているContentTypeを構築するために使用されます。
適用対象
Attachment(Stream, String, String)
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
- ソース:
- Attachment.cs
指定したストリーム、名前、および MIME の種類の情報を使用して、 Attachment クラスの新しいインスタンスを初期化します。
public:
Attachment(System::IO::Stream ^ contentStream, System::String ^ name, System::String ^ mediaType);
public Attachment(System.IO.Stream contentStream, string? name, string? mediaType);
public Attachment(System.IO.Stream contentStream, string name, string mediaType);
new System.Net.Mail.Attachment : System.IO.Stream * string * string -> System.Net.Mail.Attachment
Public Sub New (contentStream As Stream, name As String, mediaType As String)
パラメーター
- name
- String
この添付ファイルに関連付けられているContentTypeのName プロパティの値を格納しているString。 この値は、null の場合もあります。
例外
contentStream は nullです。
mediaType が正しい形式ではありません。
例
次のコード例は、このコンストラクターを呼び出す方法を示しています。
// The following example sends a summary of a log file as the message
// and the log as an email attachment.
public static void SendNamedAndTypedErrorLog(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);
// Create a name for the log data file.
string name = "log" + DateTime.Now.ToString() + ".txt";
// Create the attachment, name it, and specify the MIME type.
Attachment data = new Attachment(fs, name, MediaTypeNames.Text.Plain);
// 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 SendNamedAndTypedErrorLog: {0}",
ex.ToString());
}
data.Dispose();
// Close the log file.
fs.Close();
}
注釈
mediaTypeがString.Empty ("" とnullまたは等しくない場合)、この添付ファイルに関連付けられているContentType クラスを構築するために使用されます。
mediaTypeとnameの両方にName情報が含まれている場合は、nameで指定された値が使用されます。
TransferEncoding プロパティが Base64 に設定されている。
ストリームの CanSeek プロパティが falseされている場合、添付ファイルとそれを含む MailMessage は再利用できません。 添付ファイルを再利用するには、検索できるストリームを指定する必要があります。