Attachment 클래스

정의

이메일의 첨부 파일을 나타냅니다.

public ref class Attachment : System::Net::Mail::AttachmentBase
public class Attachment : System.Net.Mail.AttachmentBase
type Attachment = class
    inherit AttachmentBase
Public Class Attachment
Inherits AttachmentBase
상속
Attachment

예제

다음 코드 예제에서는 전자 메일 메시지에 파일을 첨부하는 방법을 보여 줍니다.

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 = L"data.xls";
   
   // Create a message and set up the recipients.
   MailMessage^ message = gcnew MailMessage( L"jane@contoso.com",L"ben@contoso.com",L"Quarterly data report.",L"See the attached spreadsheet." );
   
   // Create  the file attachment for this email message.
   Attachment^ data = gcnew 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 email message.
   message->Attachments->Add( data );
   
   //Send the message.
   SmtpClient^ client = gcnew 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( L"Content disposition" );
   Console::WriteLine( cd );
   Console::WriteLine( L"File {0}", cd->FileName );
   Console::WriteLine( L"Size {0}", cd->Size );
   Console::WriteLine( L"Creation {0}", cd->CreationDate );
   Console::WriteLine( L"Modification {0}", cd->ModificationDate );
   Console::WriteLine( L"Read {0}", cd->ReadDate );
   Console::WriteLine( L"Inline {0}", cd->Inline );
   Console::WriteLine( L"Parameters: {0}", cd->Parameters->Count );
   IEnumerator^ myEnum1 = cd->Parameters->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      DictionaryEntry^ d = safe_cast<DictionaryEntry^>(myEnum1->Current);
      Console::WriteLine( L"{0} = {1}", d->Key, d->Value );
   }

   data->~Attachment();
   client->~SmtpClient();
}
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 email 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 email 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;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
            ex.ToString());
    }
    // 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();
}

설명

클래스는 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 표시됩니다.

생성자

Attachment(Stream, ContentType)

지정된 스트림 및 콘텐츠 형식을 사용하여 Attachment 클래스의 새 인스턴스를 초기화합니다.

Attachment(Stream, String)

지정된 스트림과 이름을 사용하여 Attachment 클래스의 새 인스턴스를 초기화합니다.

Attachment(Stream, String, String)

지정된 스트림, 이름 및 MIME 형식 정보를 사용하여 Attachment 클래스의 새 인스턴스를 초기화합니다.

Attachment(String)

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

Attachment(String, ContentType)

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

Attachment(String, String)

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

속성

ContentDisposition

이 첨부 파일의 MIME 콘텐츠 처리 정보를 가져옵니다.

ContentId

이 첨부 파일의 MIME 콘텐츠 ID를 가져오거나 설정합니다.

(다음에서 상속됨 AttachmentBase)
ContentStream

이 첨부 파일의 콘텐츠 스트림을 가져옵니다.

(다음에서 상속됨 AttachmentBase)
ContentType

이 첨부 파일의 콘텐츠 형식을 가져옵니다.

(다음에서 상속됨 AttachmentBase)
Name

이 첨부 파일과 연결된 콘텐츠 형식의 MIME 콘텐츠 형식 이름 값을 가져오거나 설정합니다.

NameEncoding

AttachmentName에 대한 인코딩을 지정합니다.

TransferEncoding

이 첨부 파일의 인코딩을 가져오거나 설정합니다.

(다음에서 상속됨 AttachmentBase)

메서드

CreateAttachmentFromString(String, ContentType)

지정된 문자열의 내용 및 지정된 ContentType을 사용하여 전자 메일 첨부 파일을 만듭니다.

CreateAttachmentFromString(String, String)

지정된 문자열의 내용 및 지정된 MIME 콘텐츠 형식 이름을 사용하여 전자 메일 첨부 파일을 만듭니다.

CreateAttachmentFromString(String, String, Encoding, String)

지정된 문자열의 내용과 첨부 파일에 대해 지정된 MIME 콘텐츠 형식 이름, 문자 인코딩 및 MIME 헤더 정보를 사용하여 전자 메일 첨부 파일을 만듭니다.

Dispose()

AttachmentBase에서 사용하는 리소스를 해제합니다.

(다음에서 상속됨 AttachmentBase)
Dispose(Boolean)

AttachmentBase에서 사용하는 관리되지 않는 리소스를 해제하고, 관리되는 리소스를 선택적으로 해제할 수 있습니다.

(다음에서 상속됨 AttachmentBase)
Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상