다음을 통해 공유


MailMessage 생성자

정의

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

오버로드

Name Description
MailMessage()

클래스의 빈 인스턴스를 초기화합니다 MailMessage .

MailMessage(MailAddress, MailAddress)

지정된 클래스 개체를 사용하여 클래스의 MailMessage 새 인스턴스를 MailAddress 초기화합니다.

MailMessage(String, String)

지정된 클래스 개체를 사용하여 클래스의 MailMessage 새 인스턴스를 String 초기화합니다.

MailMessage(String, String, String, String)

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

MailMessage()

Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs

클래스의 빈 인스턴스를 초기화합니다 MailMessage .

public:
 MailMessage();
public MailMessage();
Public Sub New ()

설명

from은 mailSettings<smtp> 요소(네트워크 설정)에 대한 네트워크 요소의 값(있는 경우)으로 설정됩니다.

적용 대상

MailMessage(MailAddress, MailAddress)

Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs

지정된 클래스 개체를 사용하여 클래스의 MailMessage 새 인스턴스를 MailAddress 초기화합니다.

public:
 MailMessage(System::Net::Mail::MailAddress ^ from, System::Net::Mail::MailAddress ^ to);
public MailMessage(System.Net.Mail.MailAddress from, System.Net.Mail.MailAddress to);
new System.Net.Mail.MailMessage : System.Net.Mail.MailAddress * System.Net.Mail.MailAddress -> System.Net.Mail.MailMessage
Public Sub New (from As MailAddress, to As MailAddress)

매개 변수

from
MailAddress

전자 메일 메시지 보낸 사람의 주소를 포함하는 A MailAddress 입니다.

to
MailAddress

MailAddress 전자 메일 메시지 받는 사람의 주소가 들어 있는 A입니다.

예외

fromnull입니다.

-또는-

tonull입니다.

from 또는 to 형식이 잘못되었습니다.

예제

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

public static void CreateTestMessage3()
{
    MailAddress to = new MailAddress("jane@contoso.com");
    MailAddress from = new MailAddress("ben@contoso.com");
    MailMessage message = new MailMessage(from, to);
    message.Subject = "Using the new SMTP client.";
    message.Body = @"Using this new feature, you can send an email message from an application very easily.";
    // Use the application or machine configuration to get the
    // host, port, and credentials.
    SmtpClient client = new SmtpClient();
    Console.WriteLine("Sending an email message to {0} at {1} by using the SMTP host={2}.",
        to.User, to.Host, client.Host);
    client.Send(message);
}
Public Shared Sub CreateTestMessage3()
    Dim [to] As MailAddress = New MailAddress("jane@contoso.com")
    Dim from As MailAddress = New MailAddress("ben@contoso.com")
    Dim message As MailMessage = New MailMessage(from, [to])
    message.Subject = "Using the new SMTP client."
    message.Body = "Using this new feature, you can send an email message from an application very easily."
    'Use the application or machine configuration to get the
    ' host, port, And credentials.
    Dim client As SmtpClient = New SmtpClient()
    Console.WriteLine("Sending an email message to {0} at {1} by using the SMTP host={2}.", [to].User, [to].Host, client.Host)
    client.Send(message)
End Sub

설명

속성을 From 사용 하 여 from 초기화 하 고 To 속성을 사용 하 여 to초기화됩니다.

적용 대상

MailMessage(String, String)

Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs

지정된 클래스 개체를 사용하여 클래스의 MailMessage 새 인스턴스를 String 초기화합니다.

public:
 MailMessage(System::String ^ from, System::String ^ to);
public MailMessage(string from, string to);
new System.Net.Mail.MailMessage : string * string -> System.Net.Mail.MailMessage
Public Sub New (from As String, to As String)

매개 변수

from
String

전자 메일 메시지 보낸 사람의 주소를 포함하는 A String 입니다.

to
String

String 전자 메일 메시지의 받는 사람의 주소가 들어 있는 A입니다. 여러 전자 메일 주소는 쉼표 문자(",")로 구분해야 합니다.

예외

fromnull입니다.

-또는-

tonull입니다.

from 는 ("")입니다 Empty .

-또는-

to 는 ("")입니다 Empty .

from 또는 to 형식이 잘못되었습니다.

예제

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

public static void CreateTestMessage2(string server)
{
    string to = "jane@contoso.com";
    string from = "ben@contoso.com";
    MailMessage message = new MailMessage(from, to);
    message.Subject = "Using the new SMTP client.";
    message.Body = @"Using this new feature, you can send an email message from an application very easily.";
    SmtpClient client = new SmtpClient(server);
    // Credentials are necessary if the server requires the client
    // to authenticate before it will send email on the client's behalf.
    client.UseDefaultCredentials = true;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
            ex.ToString());
    }
}
Public Shared Sub CreateTestMessage2(ByVal server As String)
    Dim [to] As String = "jane@contoso.com"
    Dim from As String = "ben@contoso.com"
    Dim message As MailMessage = New MailMessage(from, [to])
    message.Subject = "Using the new SMTP client."
    message.Body = "Using this new feature, you can send an email message from an application very easily."
    Dim client As SmtpClient = New SmtpClient(server)
    ' Credentials are necessary if the server requires the client
    ' to authenticate before it will send email on the client's behalf.
    client.UseDefaultCredentials = True

    Try
        client.Send(message)
    Catch ex As Exception
        Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString())
    End Try
End Sub

설명

속성을 From 사용 하 여 from 초기화 하 고 To 속성을 사용 하 여 to초기화됩니다.

적용 대상

MailMessage(String, String, String, String)

Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs
Source:
MailMessage.cs

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

public:
 MailMessage(System::String ^ from, System::String ^ to, System::String ^ subject, System::String ^ body);
public MailMessage(string from, string to, string? subject, string? body);
public MailMessage(string from, string to, string subject, string body);
new System.Net.Mail.MailMessage : string * string * string * string -> System.Net.Mail.MailMessage
Public Sub New (from As String, to As String, subject As String, body As String)

매개 변수

from
String

전자 메일 메시지 보낸 사람의 주소를 포함하는 A String 입니다.

to
String

String 전자 메일 메시지의 받는 사람의 주소가 들어 있는 A입니다. 여러 전자 메일 주소는 쉼표 문자(",")로 구분해야 합니다.

subject
String

제목 텍스트가 포함된 A String 입니다.

body
String

메시지 본문을 포함하는 A String 입니다.

예외

fromnull입니다.

-또는-

tonull입니다.

from 는 ("")입니다 Empty .

-또는-

to 는 ("")입니다 Empty .

from 또는 to 형식이 잘못되었습니다.

예제

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

public static void CreateTimeoutTestMessage(string server)
{
    string to = "jane@contoso.com";
    string from = "ben@contoso.com";
    string subject = "Using the new SMTP client.";
    string body = @"Using this new feature, you can send an email message from an application very easily.";
    MailMessage message = new MailMessage(from, to, subject, body);
    SmtpClient client = new SmtpClient(server);
    Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
    client.Timeout = 100;
    // Credentials are necessary if the server requires the client
    // to authenticate before it will send email on the client's behalf.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    client.Send(message);
}
Public Shared Sub CreateTimeoutTestMessage(ByVal server As String)
    Dim [to] As String = "jane@contoso.com"
    Dim from As String = "ben@contoso.com"
    Dim subject As String = "Using the new SMTP client."
    Dim body As String = "Using this new feature, you can send an email message from an application very easily."
    Dim message As MailMessage = New MailMessage(from, [to], subject, body)
    Dim client As SmtpClient = New SmtpClient(server)
    Console.WriteLine("Changing time out from {0} to 100.", client.Timeout)
    client.Timeout = 100
    ' Credentials are necessary if the server requires the client
    ' to authenticate before it will send email on the client's behalf.
    client.Credentials = CredentialCache.DefaultNetworkCredentials
    client.Send(message)
End Sub

설명

MailMessage 개체의 속성은 다음과 같이 초기화됩니다.

매개 변수 재산
from From
to To
subject Subject
body Body

기본적으로 주체와 콘텐츠는 로컬 컴퓨터 설정에 따라 기본 인코딩을 사용하는 것으로 간주됩니다. 및 BodyEncodingSubjectEncoding 속성을 사용하여 다른 인코딩을 지정합니다.

적용 대상