Attachment クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
電子メールの添付ファイルを表します。
public ref class Attachment : System::Net::Mail::AttachmentBase
public class Attachment : System.Net.Mail.AttachmentBase
type Attachment = class
inherit AttachmentBase
Public Class Attachment
Inherits AttachmentBase
- 継承
例
次のコード例は、電子メール メッセージにファイルを添付する方法を示しています。
public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
string file = "data.xls";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"jane@contoso.com",
"ben@contoso.com",
"Quarterly data report.",
"See the attached spreadsheet.");
// Create the file attachment for this email message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this email message.
message.Attachments.Add(data);
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
ex.ToString());
}
// Display the values in the ContentDisposition for the attachment.
ContentDisposition cd = data.ContentDisposition;
Console.WriteLine("Content disposition");
Console.WriteLine(cd.ToString());
Console.WriteLine("File {0}", cd.FileName);
Console.WriteLine("Size {0}", cd.Size);
Console.WriteLine("Creation {0}", cd.CreationDate);
Console.WriteLine("Modification {0}", cd.ModificationDate);
Console.WriteLine("Read {0}", cd.ReadDate);
Console.WriteLine("Inline {0}", cd.Inline);
Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
foreach (DictionaryEntry d in cd.Parameters)
{
Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
data.Dispose();
}
注釈
Attachment クラスは、MailMessage クラスと共に使用されます。 すべてのメッセージには、メッセージの内容を含む Bodyが含まれます。 本文に加えて、追加のファイルを送信することもできます。 これらは添付ファイルとして送信され、 Attachment インスタンスとして表されます。 メール メッセージに添付ファイルを追加するには、 MailMessage.Attachments コレクションに追加します。
添付ファイルのコンテンツには、 String、 Stream、またはファイル名を指定できます。 Attachmentコンストラクターのいずれかを使用して、添付ファイル内のコンテンツを指定できます。
添付ファイルの MIME Content-Type ヘッダー情報は、 ContentType プロパティによって表されます。 Content-Type ヘッダーは、メディアの種類とサブタイプ、および関連するパラメーターを指定します。 ContentTypeを使用して、添付ファイルに関連付けられているインスタンスを取得します。
MIME Content-Disposition ヘッダーは、 ContentDisposition プロパティによって表されます。 Content-Disposition ヘッダーは、添付ファイルのプレゼンテーションとファイルのタイムスタンプを指定します。 Content-Disposition ヘッダーは、添付ファイルがファイルである場合にのみ送信されます。 ContentDisposition プロパティを使用して、添付ファイルに関連付けられているインスタンスを取得します。
MIME Content-Transfer-Encoding ヘッダーは、 TransferEncoding プロパティによって表されます。
コンストラクター
| 名前 | 説明 |
|---|---|
| Attachment(Stream, ContentType) |
指定したストリームとコンテンツ タイプを使用して、 Attachment クラスの新しいインスタンスを初期化します。 |
| Attachment(Stream, String, String) |
指定したストリーム、名前、および MIME の種類の情報を使用して、 Attachment クラスの新しいインスタンスを初期化します。 |
| Attachment(Stream, String) |
指定したストリームと名前を使用して、 Attachment クラスの新しいインスタンスを初期化します。 |
| Attachment(String, ContentType) |
指定したコンテンツ文字列とContentTypeを使用して、Attachment クラスの新しいインスタンスを初期化します。 |
| Attachment(String, String) |
指定したコンテンツ文字列と MIME の種類の情報を使用して、 Attachment クラスの新しいインスタンスを初期化します。 |
| Attachment(String) |
指定したコンテンツ文字列を使用して、 Attachment クラスの新しいインスタンスを初期化します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| ContentDisposition |
この添付ファイルの MIME コンテンツの処理を取得します。 |
| ContentId |
この添付ファイルの MIME コンテンツ ID を取得または設定します。 (継承元 AttachmentBase) |
| ContentStream |
この添付ファイルのコンテンツ ストリームを取得します。 (継承元 AttachmentBase) |
| ContentType |
この添付ファイルのコンテンツ タイプを取得します。 (継承元 AttachmentBase) |
| Name |
この添付ファイルに関連付けられているコンテンツ タイプの MIME コンテンツ タイプ名の値を取得または設定します。 |
| NameEncoding |
Attachment Nameのエンコードを指定します。 |
| TransferEncoding |
この添付ファイルのエンコードを取得または設定します。 (継承元 AttachmentBase) |
メソッド
| 名前 | 説明 |
|---|---|
| CreateAttachmentFromString(String, ContentType) |
指定した文字列の内容と指定した ContentTypeを使用して、メール添付ファイルを作成します。 |
| CreateAttachmentFromString(String, String, Encoding, String) |
指定した文字列のコンテンツ、指定した MIME コンテンツ タイプ名、文字エンコード、および添付ファイルの MIME ヘッダー情報を使用して、メール添付ファイルを作成します。 |
| CreateAttachmentFromString(String, String) |
指定した文字列のコンテンツと、指定した MIME コンテンツ タイプ名を使用して、メール添付ファイルを作成します。 |
| Dispose() |
AttachmentBaseによって使用されるリソースを解放します。 (継承元 AttachmentBase) |
| Dispose(Boolean) |
AttachmentBaseによって使用されるアンマネージ リソースを解放し、必要に応じてマネージド リソースを解放します。 (継承元 AttachmentBase) |
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |