Attachment コンストラクタとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Attachment コンストラクタの意味・解説 

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 コンストラクタ (Stream, String, String)

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

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

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

例外例外
例外種類条件

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 コンストラクタ (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
)

パラメータ

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 コンストラクタ (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 コンストラクタ (Stream, ContentType)

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

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

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

例外例外
例外種類条件

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 コンストラクタ (String, ContentType)

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

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

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

例外例外
例外種類条件

ArgumentNullException

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

ArgumentException

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

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Attachment コンストラクタ




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

辞書ショートカット

すべての辞書の索引

「Attachment コンストラクタ」の関連用語

Attachment コンストラクタのお隣キーワード
検索ランキング

   

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



Attachment コンストラクタのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS