MailMessage 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 MailMessage 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| MailMessage() |
初始化類別的 MailMessage 空實例。 |
| MailMessage(MailAddress, MailAddress) |
透過使用指定的MailAddress類別物件初始化該類別的新MailMessage實例。 |
| MailMessage(String, String) |
透過使用指定的String類別物件初始化該類別的新MailMessage實例。 |
| MailMessage(String, String, String, String) |
初始化 MailMessage 類別的新執行個體。 |
MailMessage()
初始化類別的 MailMessage 空實例。
public:
MailMessage();
public MailMessage();
Public Sub New ()
備註
From 設定為 mailSettings<的網路元素 smtp> 元素(如果存在的話)。
適用於
MailMessage(MailAddress, MailAddress)
透過使用指定的MailAddress類別物件初始化該類別的新MailMessage實例。
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
MailAddress A 裡面包含電子郵件寄件人的地址。
- to
- MailAddress
MailAddress A 包含電子郵件收件人的地址。
例外狀況
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
備註
適用於
MailMessage(String, String)
透過使用指定的String類別物件初始化該類別的新MailMessage實例。
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 或 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
備註
適用於
MailMessage(String, String, String, String)
初始化 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 或 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 屬性來指定不同的編碼。