语言

Attachment 构造函数

定义

初始化 Attachment 类的新实例。

重载

名称 说明
Attachment(String)

使用指定的内容字符串初始化类的新实例 Attachment 。

Attachment(Stream, ContentType)

使用指定的流和内容类型初始化类的新实例 Attachment 。

Attachment(Stream, String)

使用指定的流和名称初始化类的新实例 Attachment 。

Attachment(String, ContentType)

Attachment使用指定的内容字符串和 ContentType.

Attachment(String, String)

使用指定的内容字符串和 MIME 类型信息初始化类的新实例 Attachment 。

Attachment(Stream, String, String)

使用指定的流、名称和 MIME 类型信息初始化类的新实例 Attachment 。

Attachment(String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

使用指定的内容字符串初始化类的新实例 Attachment 。

public:
 Attachment(System::String ^ fileName);
public Attachment(string fileName);
new System.Net.Mail.Attachment : string -> System.Net.Mail.Attachment
Public Sub New (fileName As String)

参数

fileName
String

包含用于创建此附件的文件路径的 A String 。

例外

fileName 是 null。

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 email message.
    Attachment data = new Attachment(textMessage);
    // Send textMessage as part of the email 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;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageInlineAttachment2: {0}",
            ex.ToString());
    }
    data.Dispose();
}

注解

这些属性按如下方式设置:

财产 价值
MediaType Plain。
TransferEncoding QuotedPrintable。

适用于

Attachment(Stream, ContentType)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

使用指定的流和内容类型初始化类的新实例 Attachment 。

public:
 Attachment(System::IO::Stream ^ contentStream, System::Net::Mime::ContentType ^ contentType);
public Attachment(System.IO.Stream contentStream, System.Net.Mime.ContentType contentType);
new System.Net.Mail.Attachment : System.IO.Stream * System.Net.Mime.ContentType -> System.Net.Mail.Attachment
Public Sub New (contentStream As Stream, contentType As ContentType)

参数

contentStream
Stream

包含此附件内容的可读属性 Stream 。

contentType
ContentType

描述中ContentType数据的 AcontentStream。

例外

contentType 是 null。

-或-

contentStream 是 null。

示例

下面的代码示例演示如何调用此构造函数。

// The following example sends a summary of a log file as the message
// and the log as an email 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 email 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 email 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;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in SendErrorLog: {0}",
            ex.ToString());
    }
    data.Dispose();
    // Close the log file.
    fs.Close();
}

注解

TransferEncoding 属性设置为 Base64。

如果流的 CanSeek 属性为 false,则附件及其 MailMessage 包含该属性的附件不可重用。 必须提供可以搜索以重复使用附件的流。

适用于

Attachment(Stream, String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

使用指定的流和名称初始化类的新实例 Attachment 。

public:
 Attachment(System::IO::Stream ^ contentStream, System::String ^ name);
public Attachment(System.IO.Stream contentStream, string? name);
public Attachment(System.IO.Stream contentStream, string name);
new System.Net.Mail.Attachment : System.IO.Stream * string -> System.Net.Mail.Attachment
Public Sub New (contentStream As Stream, name As String)

参数

contentStream
Stream

包含此附件内容的可读属性 Stream 。

name
String

一个String包含与此附件关联的属性Name的值ContentType。 此值可为 null。

例外

contentStream 是 null。

示例

下面的代码示例演示如何调用此构造函数。

// The following example sends a summary of a log file as the message
// and the log as an email 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 email 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;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in SendNamedErrorLog: {0}",
            ex.ToString());
    }
    data.Dispose();
    // Close the log file.
    fs.Close();
    return;
}

注解

如果name不是或等于 null (“),String.Empty则使用此附件的属性设置为 ContentType 构造Namename。 TransferEncoding 属性设置为 Base64。

如果流的 CanSeek 属性为 false,则附件及其 MailMessage 包含该属性的附件不可重用。 必须提供可以搜索的流才能重复使用附件。

适用于

Attachment(String, ContentType)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Attachment使用指定的内容字符串和 ContentType.

public:
 Attachment(System::String ^ fileName, System::Net::Mime::ContentType ^ contentType);
public Attachment(string fileName, System.Net.Mime.ContentType contentType);
new System.Net.Mail.Attachment : string * System.Net.Mime.ContentType -> System.Net.Mail.Attachment
Public Sub New (fileName As String, contentType As ContentType)

参数

fileName
String

包含用于创建此附件的文件路径的 A String 。

contentType
ContentType

描述中ContentType数据的 AfileName。

例外

fileName 是 null。

contentType 格式不正确。

适用于

Attachment(String, String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

使用指定的内容字符串和 MIME 类型信息初始化类的新实例 Attachment 。

public:
 Attachment(System::String ^ fileName, System::String ^ mediaType);
public Attachment(string fileName, string? mediaType);
public Attachment(string fileName, string mediaType);
new System.Net.Mail.Attachment : string * string -> System.Net.Mail.Attachment
Public Sub New (fileName As String, mediaType As String)

参数

fileName
String

包含此附件的内容的 A String 。

mediaType
String

String包含此附件的 MIME 内容标头信息。 此值可为 null。

例外

fileName 是 null。

mediaType 格式不正确。

示例

下面的代码示例演示如何调用此构造函数。

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 email message.
    Attachment data = new Attachment(textMessage, MediaTypeNames.Text.Plain);
    // Send textMessage as part of the email 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;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageInlineAttachment: {0}",
            ex.ToString());
    }
    data.Dispose();
}

注解

如果 mediaType 为 null 或等于 String.Empty (“),则 MediaType 此附件的属性设置为 Plain。 如果 mediaType 不是 null 且不是零长度字符串,则用于构造 ContentType 与此附件关联的字符串。

适用于

Attachment(Stream, String, String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

使用指定的流、名称和 MIME 类型信息初始化类的新实例 Attachment 。

public:
 Attachment(System::IO::Stream ^ contentStream, System::String ^ name, System::String ^ mediaType);
public Attachment(System.IO.Stream contentStream, string? name, string? mediaType);
public Attachment(System.IO.Stream contentStream, string name, string mediaType);
new System.Net.Mail.Attachment : System.IO.Stream * string * string -> System.Net.Mail.Attachment
Public Sub New (contentStream As Stream, name As String, mediaType As String)

参数

contentStream
Stream

包含此附件内容的可读属性 Stream 。

name
String

一个String包含与此附件关联的属性Name的值ContentType。 此值可为 null。

mediaType
String

String包含此附件的 MIME 内容标头信息。 此值可为 null。

例外

contentStream 是 null。

mediaType 格式不正确。

示例

下面的代码示例演示如何调用此构造函数。

// The following example sends a summary of a log file as the message
// and the log as an email 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 email 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;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in SendNamedAndTypedErrorLog: {0}",
            ex.ToString());
    }
    data.Dispose();
    // Close the log file.
    fs.Close();
}

注解

如果 mediaType 不是 null 或等于 String.Empty (“),则用于构造 ContentType 与此附件关联的类。

如果 mediaType 同时 name 包含 Name 信息,则使用中指定的 name 值。 TransferEncoding 属性设置为 Base64。

如果流的 CanSeek 属性为 false,则附件及其 MailMessage 包含该属性的附件不可重用。 必须提供可以搜索的流才能重复使用附件。

适用于