다음을 통해 공유


Attachment 생성자

정의

Attachment 클래스의 새 인스턴스를 초기화합니다.

오버로드

Name Description
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(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 입니다.

예외

fileNamenull입니다.

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

의 데이터를 contentStream설명하는 A ContentType 입니다.

예외

contentTypenull입니다.

-또는-

contentStreamnull입니다.

예제

다음 코드 예제에서는이 생성자를 호출 하는 방법을 보여 줍니다.

// 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

이 첨부 파일과 연결된 속성의 NameContentType 값을 포함하는 A String 입니다. 이 값은 null일 수 있습니다.

예외

contentStreamnull입니다.

예제

다음 코드 예제에서는이 생성자를 호출 하는 방법을 보여 줍니다.

// 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;
}

설명

("")ContentType가 아니 null 거나 같 String.Empty 으면 name 이 첨부 파일에 대한 속성이 로 설정된 name상태에서 Name 생성됩니다. TransferEncoding 속성은 Base64로 설정됩니다.

스트림의 CanSeek 속성이 false면 첨부 파일 및 MailMessage 해당 파일이 포함된 파일을 다시 사용할 수 없습니다. 첨부 파일을 다시 사용하려면 검색할 수 있는 스트림을 제공해야 합니다.

적용 대상

Attachment(String, ContentType)

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

지정된 콘텐츠 문자열 ContentType및 .를 사용하여 클래스의 Attachment 새 인스턴스를 초기화합니다.

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

의 데이터를 fileName설명하는 A ContentType 입니다.

예외

fileNamenull입니다.

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

이 첨부 파일에 대한 MIME Content-Header 정보가 들어 있는 A String 입니다. 이 값은 null일 수 있습니다.

예외

fileNamenull입니다.

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 이 첨부 파일의 MediaType 속성이 .로 Plain설정 String.Empty 됩니다.null mediaType 길이가 0이 아니 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

이 첨부 파일과 연결된 속성의 NameContentType 값을 포함하는 A String 입니다. 이 값은 null일 수 있습니다.

mediaType
String

이 첨부 파일에 대한 MIME Content-Header 정보가 들어 있는 A String 입니다. 이 값은 null일 수 있습니다.

예외

contentStreamnull입니다.

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();
}

설명

("")가 아니 null 거나 같 String.Empty 으면 mediaType 이 첨부 파일과 연결된 클래스를 ContentType 생성하는 데 사용됩니다.

name 둘 다 정보가 포함된 Name 경우 mediaType 지정된 name 값이 사용됩니다. TransferEncoding 속성은 Base64로 설정됩니다.

스트림의 CanSeek 속성이 false면 첨부 파일 및 MailMessage 해당 파일이 포함된 파일을 다시 사용할 수 없습니다. 첨부 파일을 다시 사용하려면 검색할 수 있는 스트림을 제공해야 합니다.

적용 대상