attachmentとは? わかりやすく解説

Attachment クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

電子メール添付するデータ表します

名前空間: System.Net.Mail
アセンブリ: System (system.dll 内)
構文構文

Public Class Attachment
    Inherits AttachmentBase
public class Attachment : AttachmentBase
public ref class Attachment : public
 AttachmentBase
public class Attachment extends AttachmentBase
public class Attachment extends
 AttachmentBase
解説解説

Attachment クラスは、MailMessage クラスと共に使用されます。すべてのメッセージには、メッセージ内容格納している Body含まれます。本文加えて追加ファイル送信することもできます。これらは添付データとして送信されAttachmentインスタンスとして表されます。添付データ電子メール メッセージ追加するには、そのファイルを MailMessage.Attachments コレクション追加します

添付データ内容には、StringStream、またはファイル名指定できます添付データ内容は、いずれかの Attachment コンストラクタ使用して指定できます

添付データMIMEContent-Type ヘッダー情報は、ContentType プロパティによって表されます。Content-Type ヘッダーでは、メディア タイプサブタイプ、および関連付けられたパラメータ指定します添付データ関連付けられているインスタンス取得するには、ContentType使用します

MIME の Content-Disposition ヘッダーは、ContentDisposition プロパティによって表されます。Content-Disposition ヘッダーでは、添付データ表示方法ファイルタイム スタンプ指定します。Content-Disposition ヘッダーは、添付データファイル場合にだけ送信されます。添付データ関連付けられているインスタンス取得するには、ContentDisposition プロパティ使用します

MIMEContent-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.Object
   System.Net.Mail.AttachmentBase
    System.Net.Mail.Attachment
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Attachment メンバ
System.Net.Mail 名前空間

Attachment コンストラクタ (String)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

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

名前空間: System.Net.Mail
アセンブリ: System (system.dll 内)
構文構文

例外例外
例外種類条件

ArgumentNullException

fileNamenull 参照 (Visual Basic では Nothing) です。

ArgumentException

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 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();
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Attachment クラス
Attachment メンバ
System.Net.Mail 名前空間

Attachment コンストラクタ (Stream, String, String)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

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

名前空間: System.Net.Mail
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    contentStream As Stream, _
    name As String, _
    mediaType As String _
)
Dim contentStream As Stream
Dim name As String
Dim mediaType As String

Dim instance As New Attachment(contentStream,
 name, mediaType)
public Attachment (
    Stream contentStream,
    string name,
    string mediaType
)
public:
Attachment (
    Stream^ contentStream, 
    String^ name, 
    String^ mediaType
)
public Attachment (
    Stream contentStream, 
    String name, 
    String mediaType
)
public function Attachment (
    contentStream : Stream, 
    name : String, 
    mediaType : String
)

パラメータ

contentStream

この添付内容格納している読み取り可能な Stream

name

この添付データ関連付けられている ContentTypeName プロパティの値を格納している String。この値は、null 参照 (Visual Basic では Nothing) の場合あります

mediaType

この添付データMIME コンテンツ ヘッダー情報格納している String。この値は、null 参照 (Visual Basic では Nothing) の場合あります

例外例外
例外種類条件

ArgumentNullException

streamnull 参照 (Visual Basic では Nothing) です。

FormatException

mediaType形式正しくありません。

解説解説

mediaTypenull 参照 (Visual Basic では Nothing) でないか、String.Empty ("") と等し場合は、この添付データ関連付けられた ContentType クラス構築使用されます。

mediaTypename両方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();
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Attachment クラス
Attachment メンバ
System.Net.Mail 名前空間

Attachment コンストラクタ (Stream, String)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

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

名前空間: System.Net.Mail
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    contentStream As Stream, _
    name As String _
)
Dim contentStream As Stream
Dim name As String

Dim instance As New Attachment(contentStream,
 name)
public Attachment (
    Stream contentStream,
    string name
)
public:
Attachment (
    Stream^ contentStream, 
    String^ name
)
public Attachment (
    Stream contentStream, 
    String name
)
public function Attachment (
    contentStream : Stream, 
    name : String
)

パラメータ

contentStream

この添付内容格納している読み取り可能な Stream

name

この添付データ関連付けられている ContentTypeName プロパティの値を格納している String。この値は、null 参照 (Visual Basic では Nothing) の場合あります

例外例外
例外種類条件

ArgumentNullException

contentStreamnull 参照 (Visual Basic では Nothing) です。

解説解説

namenull 参照 (Visual Basic では Nothing) でないか、String.Empty ("") と等し場合、この添付データContentTypename設定されている 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;
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Attachment クラス
Attachment メンバ
System.Net.Mail 名前空間

Attachment コンストラクタ (String, String)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

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

名前空間: System.Net.Mail
アセンブリ: System (system.dll 内)
構文構文

例外例外
例外種類条件

ArgumentNullException

fileNamenull 参照 (Visual Basic では Nothing) です。

FormatException

mediaType形式正しくありません。

解説解説

mediaTypenull 参照 (Visual Basic では Nothing) か、または String.Empty ("") と等し場合、この添付データMediaType プロパティPlain設定されます。mediaTypenull 参照 (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();
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Attachment クラス
Attachment メンバ
System.Net.Mail 名前空間

Attachment コンストラクタ (Stream, ContentType)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

ストリームコンテンツ タイプ指定して、Attachment クラス新しインスタンス初期化します。

名前空間: System.Net.Mail
アセンブリ: 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
)

パラメータ

contentStream

この添付内容格納している読み取り可能な Stream

contentType

stream 内のデータ説明する ContentType

例外例外
例外種類条件

ArgumentNullException

contentTypenull 参照 (Visual Basic では Nothing) です。

または

contentStreamnull 参照 (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();
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Attachment クラス
Attachment メンバ
System.Net.Mail 名前空間

Attachment コンストラクタ (String, ContentType)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

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

名前空間: System.Net.Mail
アセンブリ: System (system.dll 内)
構文構文

例外例外
例外種類条件

ArgumentNullException

fileNamenull 参照 (Visual Basic では Nothing) です。

ArgumentException

mediaType形式正しくありません。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Attachment クラス
Attachment メンバ
System.Net.Mail 名前空間

Attachment コンストラクタ


Attachment プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ ContentDisposition この添付データMIME コンテンツ配置取得します
パブリック プロパティ ContentId  この添付データMIME コンテンツ ID取得または設定します。 ( AttachmentBase から継承されます。)
パブリック プロパティ ContentStream  この添付データコンテンツ ストリーム取得します。 ( AttachmentBase から継承されます。)
パブリック プロパティ ContentType  この添付データコンテンツ タイプ取得します。 ( AttachmentBase から継承されます。)
パブリック プロパティ Name この添付データ関連付けられているコンテンツ タイプMIME コンテンツ タイプ名の値を取得または設定します
パブリック プロパティ NameEncoding Attachment Nameエンコーディング指定します
パブリック プロパティ TransferEncoding  この添付データエンコーディング取得または設定します。 ( AttachmentBase から継承されます。)
参照参照

関連項目

Attachment クラス
System.Net.Mail 名前空間

Attachment メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

Attachment クラス
System.Net.Mail 名前空間

Attachment メンバ

電子メール添付するデータ表します

Attachment データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

Attachment クラス
System.Net.Mail 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からattachmentを検索した結果を表示しています。
Weblioに収録されているすべての辞書からattachmentを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からattachmentを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「attachment」の関連用語

attachmentのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



attachmentのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS