Attachment クラス

定義

電子メールの添付ファイルを表します。

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
継承
Attachment

次のコード例は、電子メール メッセージにファイルを添付する方法を示しています。

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 = L"data.xls";
   
   // Create a message and set up the recipients.
   MailMessage^ message = gcnew MailMessage( L"jane@contoso.com",L"ben@contoso.com",L"Quarterly data report.",L"See the attached spreadsheet." );
   
   // Create  the file attachment for this email message.
   Attachment^ data = gcnew 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 = gcnew SmtpClient( server );
   
   // Add credentials if the SMTP server requires them.
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   client->Send( message );
   
   // Display the values in the ContentDisposition for the attachment.
   ContentDisposition^ cd = data->ContentDisposition;
   Console::WriteLine( L"Content disposition" );
   Console::WriteLine( cd );
   Console::WriteLine( L"File {0}", cd->FileName );
   Console::WriteLine( L"Size {0}", cd->Size );
   Console::WriteLine( L"Creation {0}", cd->CreationDate );
   Console::WriteLine( L"Modification {0}", cd->ModificationDate );
   Console::WriteLine( L"Read {0}", cd->ReadDate );
   Console::WriteLine( L"Inline {0}", cd->Inline );
   Console::WriteLine( L"Parameters: {0}", cd->Parameters->Count );
   IEnumerator^ myEnum1 = cd->Parameters->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      DictionaryEntry^ d = safe_cast<DictionaryEntry^>(myEnum1->Current);
      Console::WriteLine( L"{0} = {1}", d->Key, d->Value );
   }

   data->~Attachment();
   client->~SmtpClient();
}
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 追加します。

添付ファイルの内容には、ファイル名 、またはファイルStream名をString指定できます。 任意のコンストラクターを使用して、添付ファイル内のコンテンツを 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)

ストリームと名前を指定して、Attachment クラスの新しいインスタンスを初期化します。

Attachment(Stream, String, String)

ストリーム、名前、MIME タイプの情報を指定して、Attachment クラスの新しいインスタンスを初期化します。

Attachment(String)

コンテンツの文字列を指定して、Attachment クラスの新しいインスタンスを初期化します。

Attachment(String, ContentType)

コンテンツの文字列と ContentType を指定して、Attachment クラスの新しいインスタンスを初期化します。

Attachment(String, String)

コンテンツの文字列と MIME タイプの情報を指定して、Attachment クラスの新しいインスタンスを初期化します。

プロパティ

ContentDisposition

この添付ファイルの MIME コンテンツ配置を取得します。

ContentId

この添付ファイルの MIME コンテンツ ID を取得または設定します。

(継承元 AttachmentBase)
ContentStream

この添付ファイルのコンテンツ ストリームを取得します。

(継承元 AttachmentBase)
ContentType

この添付ファイルのコンテンツ タイプを取得します。

(継承元 AttachmentBase)
Name

この添付ファイルに関連付けられているコンテンツ タイプの MIME コンテンツ タイプ名の値を取得または設定します。

NameEncoding

AttachmentName のエンコーディングを指定します。

TransferEncoding

この添付ファイルのエンコーディングを取得または設定します。

(継承元 AttachmentBase)

メソッド

CreateAttachmentFromString(String, ContentType)

指定した文字列のコンテンツと指定した ContentType を使用して、電子メールの添付ファイルを作成します。

CreateAttachmentFromString(String, String)

指定した文字列のコンテンツと指定した MIME コンテンツ タイプ名を使用して、電子メールの添付ファイルを作成します。

CreateAttachmentFromString(String, String, Encoding, String)

指定した文字列のコンテンツ、指定した MIME コンテンツ タイプ名、文字エンコーディング、添付ファイルの MIME ヘッダー情報を使用して、電子メールの添付ファイルを作成します。

Dispose()

AttachmentBase で使用したリソースを解放します。

(継承元 AttachmentBase)
Dispose(Boolean)

AttachmentBase によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。

(継承元 AttachmentBase)
Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象