語言

MailMessage.Body 屬性

定義

接收或設定訊息主體。

public:
 property System::String ^ Body { System::String ^ get(); void set(System::String ^ value); };
public string Body { get; set; }
member this.Body : string with get, set
Public Property Body As String

屬性值

一個 String 包含正文的值。

範例

以下程式碼範例示範如何設定該 Body 屬性。

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

備註

ContentType內容是Body「文字/純文字」。 指定用於該 BodyEncoding 實體的編碼方式。

如果正文內容有其他格式,能為收件人提供更豐富的呈現選項,你可以透過屬性 AlternateViews 指定正文內容的替代視圖。 例如,應用程式可能會同時傳送純文字正文與訊息正文的 HTML 版本。 能顯示 HTML 的電子郵件閱讀器會呈現 HTML 版本的正文給收件人,而無法顯示 HTML 的閱讀器則會顯示純文字版本的訊息。

適用於