Attachment クラス
アセンブリ: System (system.dll 内)
構文
解説
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 プロパティによって表されます。
電子メール メッセージにファイルを添付するコード例を次に示します。
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 e-mail 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 e-mail 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; client.Send(message); // 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(); }
System.Net.Mail.AttachmentBase
System.Net.Mail.Attachment
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Attachment コンストラクタ (String)
アセンブリ: System (system.dll 内)
構文
例外
使用例
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 e-mail message. Attachment data = new Attachment(textMessage); // Send textMessage as part of the e-mail 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; client.Send(message); data.Dispose(); }
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Attachment コンストラクタ (Stream, String, String)
アセンブリ: System (system.dll 内)
構文
Dim contentStream As Stream Dim name As String Dim mediaType As String Dim instance As New Attachment(contentStream, name, mediaType)
mediaType が null 参照 (Visual Basic では Nothing) でないか、String.Empty ("") と等しい場合は、この添付データに関連付けられた ContentType クラスの構築に使用されます。
mediaType と name の両方に Name 情報が格納されている場合、name で指定された値が使用されます。TransferEncoding プロパティが Base64 に設定されています。
ストリームの CanSeek プロパティが false の場合、添付データとそのデータを格納している MailMessage は再利用できません。添付データを再利用するには、検索できるストリームを指定する必要があります。
// The following example sends a summary of a log file as the message // and the log as an e-mail 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 e-mail 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; client.Send(message); data.Dispose(); // Close the log file. fs.Close(); }
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Attachment コンストラクタ (Stream, String)
アセンブリ: System (system.dll 内)
構文
解説
name が null 参照 (Visual Basic では Nothing) でないか、String.Empty ("") と等しい場合、この添付データの ContentType は name に設定されている Name プロパティを使用して生成されます。TransferEncoding プロパティが Base64 に設定されています。
ストリームの CanSeek プロパティが false の場合、添付データとそのデータを格納している MailMessage は再利用できません。添付データを再利用するには、検索できるストリームを指定する必要があります。
// The following example sends a summary of a log file as the message // and the log as an e-mail 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 e-mail 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; client.Send(message); data.Dispose(); // Close the log file. fs.Close(); return; }
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Attachment コンストラクタ (String, String)
アセンブリ: System (system.dll 内)
構文
mediaType が null 参照 (Visual Basic では Nothing) か、または String.Empty ("") と等しい場合、この添付データの MediaType プロパティは Plain に設定されます。mediaType が null 参照 (Visual Basic では Nothing) でなく、長さが 0 の文字列でもない場合は、この添付データに関連付けられた ContentType の生成に使用されます。
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 e-mail message. Attachment data = new Attachment(textMessage, MediaTypeNames.Text.Plain); // Send textMessage as part of the e-mail 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; client.Send(message); data.Dispose(); }
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Attachment コンストラクタ (Stream, ContentType)
アセンブリ: System (system.dll 内)
構文
Public Sub New ( _ contentStream As Stream, _ contentType As ContentType _ )
Dim contentStream As Stream Dim contentType As ContentType Dim instance As New Attachment(contentStream, contentType)
public Attachment ( Stream contentStream, ContentType contentType )
public: Attachment ( Stream^ contentStream, ContentType^ contentType )
public Attachment ( Stream contentStream, ContentType contentType )
public function Attachment ( contentStream : Stream, contentType : ContentType )
例外の種類 | 条件 |
---|---|
ArgumentNullException | contentType が null 参照 (Visual Basic では Nothing) です。 または contentStream が null 参照 (Visual Basic では Nothing) です。 |
TransferEncoding プロパティが Base64 に設定されています。
ストリームの CanSeek プロパティが false の場合、添付データとそのデータを格納している MailMessage は再利用できません。添付データを再利用するには、検索できるストリームを指定する必要があります。
// The following example sends a summary of a log file as the message // and the log as an e-mail 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 e-mail 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 e-mail 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; client.Send(message); data.Dispose(); // Close the log file. fs.Close(); }
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Attachment コンストラクタ (String, ContentType)
アセンブリ: System (system.dll 内)
構文
Public Sub New ( _ fileName As String, _ contentType As ContentType _ )
Dim fileName As String Dim contentType As ContentType Dim instance As New Attachment(fileName, contentType)
public Attachment ( string fileName, ContentType contentType )
public: Attachment ( String^ fileName, ContentType^ contentType )
public Attachment ( String fileName, ContentType contentType )
public function Attachment ( fileName : String, contentType : ContentType )
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
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 プロパティ
パブリック プロパティ
名前 | 説明 | |
---|---|---|
ContentDisposition | この添付データの MIME コンテンツ配置を取得します。 | |
ContentId | この添付データの MIME コンテンツ ID を取得または設定します。 ( AttachmentBase から継承されます。) | |
ContentStream | この添付データのコンテンツ ストリームを取得します。 ( AttachmentBase から継承されます。) | |
ContentType | この添付データのコンテンツ タイプを取得します。 ( AttachmentBase から継承されます。) | |
Name | この添付データに関連付けられているコンテンツ タイプの MIME コンテンツ タイプ名の値を取得または設定します。 | |
NameEncoding | Attachment Name のエンコーディングを指定します。 | |
TransferEncoding | この添付データのエンコーディングを取得または設定します。 ( AttachmentBase から継承されます。) |
Attachment メソッド
パブリック メソッド
名前 | 説明 | |
---|---|---|
CreateAttachmentFromString | オーバーロードされます。 文字列から添付データを作成します。 | |
Dispose | オーバーロードされます。 AttachmentBase によって使用されているリソースを解放します。 ( AttachmentBase から継承されます。) | |
Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) | |
GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) | |
GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) | |
ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) | |
ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |
名前 | 説明 | |
---|---|---|
Dispose | オーバーロードされます。 AttachmentBase によって使用されているリソースを解放します。 ( AttachmentBase から継承されます。) | |
Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) | |
MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
Attachment メンバ
Attachment データ型で公開されるメンバを以下の表に示します。
パブリック コンストラクタ
パブリック プロパティ
名前 | 説明 | |
---|---|---|
ContentDisposition | この添付データの MIME コンテンツ配置を取得します。 | |
ContentId | この添付データの MIME コンテンツ ID を取得または設定します。(AttachmentBase から継承されます。) | |
ContentStream | この添付データのコンテンツ ストリームを取得します。(AttachmentBase から継承されます。) | |
ContentType | この添付データのコンテンツ タイプを取得します。(AttachmentBase から継承されます。) | |
Name | この添付データに関連付けられているコンテンツ タイプの MIME コンテンツ タイプ名の値を取得または設定します。 | |
NameEncoding | Attachment Name のエンコーディングを指定します。 | |
TransferEncoding | この添付データのエンコーディングを取得または設定します。(AttachmentBase から継承されます。) |
名前 | 説明 | |
---|---|---|
CreateAttachmentFromString | オーバーロードされます。 文字列から添付データを作成します。 | |
Dispose | オーバーロードされます。 AttachmentBase によって使用されているリソースを解放します。 (AttachmentBase から継承されます。) | |
Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) | |
GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) | |
GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) | |
ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) | |
ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |
名前 | 説明 | |
---|---|---|
Dispose | オーバーロードされます。 AttachmentBase によって使用されているリソースを解放します。 (AttachmentBase から継承されます。) | |
Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) | |
MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
Weblioに収録されているすべての辞書からattachmentを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からattachmentを検索
- attachmentのページへのリンク