MailMessage クラス

定義

SmtpClient クラスを使用して送信できる電子メール メッセージを表します。

public ref class MailMessage : IDisposable
public class MailMessage : IDisposable
type MailMessage = class
    interface IDisposable
Public Class MailMessage
Implements IDisposable
継承
MailMessage
実装

次のコード例では、添付ファイルを含む電子メール メッセージを作成して送信する方法を示します。

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();
}
Public Shared Sub CreateMessageWithAttachment(ByVal server As String)
    ' Specify the file to be attached And sent.
    ' This example assumes that a file named Data.xls exists in the
    ' current working directory.
    Dim file As String = "data.xls"
    ' Create a message and set up the recipients.
    Dim message As MailMessage = New MailMessage(
        "jane@contoso.com",
        "ben@contoso.com",
        "Quarterly data report.",
        "See the attached spreadsheet.")

    ' Create  the file attachment for this email message.
    Dim data As Attachment = New Attachment(file, MediaTypeNames.Application.Octet)
    ' Add time stamp information for the file.
    Dim disposition As ContentDisposition = 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
    Dim client As SmtpClient = New SmtpClient(server)
    ' Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials

    Try
        client.Send(message)
    Catch ex As Exception
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString())
    End Try

    ' Display the values in the ContentDisposition for the attachment.
    Dim cd As ContentDisposition = 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)

    For Each d As DictionaryEntry In cd.Parameters
        Console.WriteLine("{0} = {1}", d.Key, d.Value)
    Next

    data.Dispose()
End Sub

注釈

クラスの MailMessage インスタンスは、そのクラスを使用して配信するために SMTP サーバーに送信される電子メール メッセージを SmtpClient 作成するために使用されます。

メール メッセージの送信者、受信者、件名、本文は、オブジェクトの初期化MailMessageに使用する場合MailMessageにパラメーターとして指定できます。 これらのパラメーターは、オブジェクトの MailMessage プロパティを使用して設定またはアクセスすることもできます。

メッセージのプライマリ メール メッセージ ヘッダーと要素は、クラスの次の MailMessage プロパティを使用して設定できます。

メール ヘッダーまたはパーツ プロパティ
[Attachments] Attachments
ブラインド カーボン コピー (BCC) Bcc
カーボン コピー (CC) CC
Content-Type BodyEncoding
カスタム ヘッダーのエンコード HeadersEncoding
メッセージ本文 Body
優先度 Priority
Recipient To
[返信先] ReplyToList
送信者 From
サブジェクト Subject

この MailMessage クラスを使用すると、アプリケーションはプロパティを使用してメッセージのヘッダー コレクションに Headers アクセスすることもできます。 このコレクションは読み取り専用ですが (新しいコレクションは設定できません)、このコレクションに対してカスタム ヘッダーを追加したり、このコレクションから削除したりすることができます。 追加されたカスタム ヘッダーは、インスタンスの送信時に MailMessage 含まれます。 メッセージが送信される前に、プロパティ内 Headers のこのコレクションに特別に追加されたヘッダーのみがコレクションに含まれます。 インスタンスがMailMessage送信されると、プロパティには、オブジェクトのHeaders初期化MailMessageに使用されるときにMailMessage渡されるクラスまたはパラメーターのMailMessage関連付けられたプロパティを使用して設定されたヘッダーも含まれます。

一部のメール ヘッダーの形式が正しくない場合は、メール メッセージが破損する可能性があります。 そのため、クラスのプロパティMailMessageを使用して設定できるヘッダー コレクション内のメール ヘッダーは、クラス プロパティをMailMessage使用するか、オブジェクトを初期化MailMessageするときにMailMessage渡されるパラメーターとしてのみ設定する必要があります。 次のメール ヘッダーの一覧は、プロパティを Headers 使用して追加しないでください。このプロパティを使用して Headers これらのヘッダーに設定された値は、メッセージの送信時に破棄または上書きされます。

  • [Bcc]

  • [Cc]

  • Content-ID

  • Content-Location

  • Content-Transfer-Encoding

  • Content-Type

  • Date

  • ソース

  • 重要度

  • MIME-Version

  • 優先度

  • [返信先]

  • 送信者

  • 終了

  • X 優先度

アプリケーションでプロパティを使用して X-Sender ヘッダーを Headers 指定しない場合、 MailMessage メッセージの送信時にクラスによって作成されます。

このプロパティを AlternateViews 使用して、電子メール メッセージのコピーをさまざまな形式で指定します。 たとえば、HTML でメッセージを送信する場合、一部の受信者が HTML コンテンツを表示できない電子メール リーダーを使用する場合に備えて、プレーン テキスト バージョンを指定することもできます。 代替ビューを使用してメッセージを作成する例については、次を参照してください AlternateViews

このプロパティを Attachments 使用して、電子メール メッセージに添付ファイルを追加します。 添付ファイルを含むメッセージを作成する例については、次を参照してください Attachments。 MailMessage で Dispose を呼び出すと、参照されている各添付ファイルに対して Dispose も呼び出されます。

電子メール メッセージを組み立てた後、またはSendAsyncメソッドをSend使用して送信できます。

コンストラクター

MailMessage()

MailMessage クラスの空のインスタンスを初期化します。

MailMessage(MailAddress, MailAddress)

MailMessage クラスのオブジェクトを指定して、MailAddress クラスの新しいインスタンスを初期化します。

MailMessage(String, String)

MailMessage クラスのオブジェクトを指定して、String クラスの新しいインスタンスを初期化します。

MailMessage(String, String, String, String)

MailMessage クラスの新しいインスタンスを初期化します。

プロパティ

AlternateViews

メッセージ本文の別形式の表示内容を格納するために使用する添付データのコレクションを取得します。

Attachments

この電子メール メッセージに添付されるデータを格納するために使用する添付データのコレクションを取得します。

Bcc

この電子メール メッセージの BCC (blind carbon copy) 受信者を格納するアドレスのコレクションを取得します。

Body

メッセージ本文を取得または設定します。

BodyEncoding

メッセージ本文をエンコードするために使用されるエンコーディングを取得または設定します。

BodyTransferEncoding

メッセージ本文をエンコードするために使用される転送エンコーディングを取得または設定します。

CC

この電子メール メッセージの CC (carbon copy) 受信者を格納するアドレスのコレクションを取得します。

DeliveryNotificationOptions

この電子メールの配信通知を取得または設定します。

From

この電子メールの差出人アドレスを取得または設定します。

Headers

この電子メール メッセージで送信される電子メール ヘッダーを取得します。

HeadersEncoding

この電子メール メッセージのユーザー定義のカスタム ヘッダーに使用されるエンコーディングを取得または設定します。

IsBodyHtml

電子メール メッセージの本文が HTML 形式かどうかを示す値を取得または設定します。

Priority

この電子メールの優先順位を取得または設定します。

ReplyTo
互換性のために残されています。
互換性のために残されています。
互換性のために残されています。
互換性のために残されています。

電子メール メッセージの ReplyTo アドレスを取得または設定します。

ReplyToList

電子メール メッセージの返信先のアドレスの一覧を取得します。

Sender

この電子メール メッセージの差出人のアドレスを取得または設定します。

Subject

この電子メールの件名を取得または設定します。

SubjectEncoding

この電子メール メッセージの件名の内容に使用されるエンコーディングを取得または設定します。

To

この電子メール メッセージの受信者を格納するアドレスのコレクションを取得します。

メソッド

Dispose()

MailMessage によって使用されているすべてのリソースを解放します。

Dispose(Boolean)

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

Equals(Object)

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

(継承元 Object)
GetHashCode()

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

(継承元 Object)
GetType()

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

(継承元 Object)
MemberwiseClone()

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

(継承元 Object)
ToString()

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

(継承元 Object)

適用対象