MailMessage 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
警告
The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202
提供建構電子郵件訊息的屬性和方法。 建議的替代做法: System.Net.Mail。
public ref class MailMessage
public class MailMessage
[System.Obsolete("The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202")]
public class MailMessage
type MailMessage = class
[<System.Obsolete("The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202")>]
type MailMessage = class
Public Class MailMessage
- 繼承
-
MailMessage
- 屬性
範例
下列範例將示範如何使用 MailMessage 類別。
重要
此控件有一個文本框可接受使用者輸入,這是潛在的安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<%--
This example shows how to send a mail message from a Web Forms page
using the classes in the System.Web.Mail namespace.
--%>
<%@ IMPORT namespace="System.Web.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="C#" runat="server">
void Page_Load()
{
if (!IsPostBack)
{
txtTo.Text="john@contoso.com";
txtFrom.Text="marsha@contoso.com";
txtCc.Text="fred@contoso.com";
txtBcc.Text="wilma@contoso.com";
txtSubject.Text="Hello";
txtBody.Text="This is a test message.";
txtAttach.Text=@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg,"
+ @"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg";
txtBodyEncoding.Text = Encoding.ASCII.EncodingName;
txtBodyFormat.Text="HTML";
txtPriority.Text="Normal";
txtUrlContentBase.Text="http://www.contoso.com/images";
txtUrlContentLocation.Text="http://www.contoso.com/images";
// Name of relay mail server in your domain.
txtMailServer.Text="smarthost";
}
}
void btnSubmit_Click(Object sender, EventArgs e)
{
string sTo, sFrom, sSubject, sBody;
string sAttach, sCc, sBcc, sBodyEncoding;
string sBodyFormat, sMailServer, sPriority;
string sUrlContentBase, sUrlContentLocation;
int iLoop1;
sTo = txtTo.Text.Trim();
sFrom = txtFrom.Text.Trim();
sSubject = txtSubject.Text.Trim();
sBody = txtBody.Text.Trim();
sAttach = txtAttach.Text.Trim();
sCc = txtCc.Text.Trim();
sBcc = txtBcc.Text.Trim();
sBodyFormat = txtBodyFormat.Text.Trim();
sBodyEncoding = txtBodyEncoding.Text.Trim();
sPriority = txtPriority.Text.Trim();
sUrlContentBase = txtUrlContentBase.Text.Trim();
sUrlContentLocation = txtUrlContentLocation.Text.Trim();
sMailServer = txtMailServer.Text.Trim();
MailMessage MyMail = new MailMessage();
MyMail.From = sFrom;
MyMail.To = sTo;
MyMail.Subject = sSubject;
MyMail.Body = sBody;
MyMail.Cc = sCc;
MyMail.Bcc = sBcc;
MyMail.UrlContentBase = sUrlContentBase;
MyMail.UrlContentLocation = sUrlContentLocation;
if (txtBodyEncoding.Text == Encoding.UTF7.EncodingName)
MyMail.BodyEncoding = Encoding.UTF7;
else if (txtBodyEncoding.Text == Encoding.UTF8.EncodingName)
MyMail.BodyEncoding = Encoding.UTF8;
else
MyMail.BodyEncoding = Encoding.ASCII;
switch (sBodyFormat.ToUpper())
{
case "HTML":
MyMail.BodyFormat = MailFormat.Html;
break;
default:
MyMail.BodyFormat = MailFormat.Text;
break;
}
switch (sPriority.ToUpper())
{
case "HIGH":
MyMail.Priority = MailPriority.High;
break;
case "LOW":
MyMail.Priority = MailPriority.Low;
break;
default:
MyMail.Priority = MailPriority.Normal;
break;
}
// Build an IList of mail attachments.
if (sAttach != "")
{
char[] delim = new char[] {','};
foreach (string sSubstr in sAttach.Split(delim))
{
MailAttachment MyAttachment = new MailAttachment(sSubstr);
MyMail.Attachments.Add(MyAttachment);
}
}
SmtpMail.SmtpServer = sMailServer;
SmtpMail.Send(MyMail);
lblMsg1.Text="C# Message sent to " + MyMail.To;
}
void btnClear_Click(Object sender, EventArgs e)
{
lblMsg1.Text="";
txtTo.Text="";
txtFrom.Text="";
txtSubject.Text="";
txtBody.Text="";
txtAttach.Text="";
txtBcc.Text="";
txtCc.Text="";
txtBodyEncoding.Text="";
txtBodyFormat.Text="";
txtPriority.Text="";
txtUrlContentBase.Text="";
txtUrlContentLocation.Text="";
txtMailServer.Text="";
btnSubmit.Text="Submit";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Mail Form Example</title>
</head>
<body>
<h4>Send a new mail message:</h4>
<form id="form1" method="Post" action="MailForm.aspx" runat="server">
<table style="width:350; background-color:#FFFF99">
<tr>
<td align="Right"><b>To:</b></td>
<td><Asp:Textbox id="txtTo" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>From:</b></td>
<td><Asp:Textbox id="txtFrom" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>Subject:</b></td>
<td><Asp:Textbox id="txtSubject" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>MessageBody:</b></td>
<td><Asp:Textbox id="txtBody" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>Attachments:</b></td>
<td><Asp:Textbox id="txtAttach" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>CC:</b></td>
<td><Asp:Textbox id="txtBcc" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>BCC:</b></td>
<td><Asp:Textbox id="txtCc" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>BodyEncoding:</b></td>
<td><Asp:Textbox id="txtBodyEncoding" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>BodyFormat:</b></td>
<td><Asp:Textbox id="txtBodyFormat" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>Priority:</b></td>
<td><Asp:Textbox id="txtPriority" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>URL Content Base:</b></td>
<td><Asp:Textbox id="txtUrlContentBase" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>URL Content Location:</b></td>
<td><Asp:Textbox id="txtUrlContentLocation" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>Mail Server:</b></td>
<td><Asp:Textbox id="txtMailServer" runat="server"/></td>
</tr>
</table><br />
<asp:button id="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat="server"/>
<asp:button id="btnClear" Text="Clear" OnClick="btnClear_Click" runat="server"/>
<p><asp:Label id="lblMsg1" runat="server"/></p>
</form>
</body>
</html>
<%--
This example shows how to send a mail message from a Web Forms page
using the classes in the System.Web.Mail namespace.
--%>
<%@ IMPORT namespace="System.Web.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="VB" runat="server">
Sub Page_Load()
If Not IsPostBack Then
lblMsg1.Text = ""
txtTo.Text = "john@contoso.com"
txtFrom.Text = "marsha@contoso.com"
txtCc.Text = "fred@contoso.com"
txtBcc.Text = "wilma@contoso.com"
txtSubject.Text = "Hello"
txtBody.Text = "This is a test message."
txtAttach.Text = "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg," _
& "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg"
txtBodyEncoding.Text = Encoding.ASCII.EncodingName
txtBodyFormat.Text = "HTML"
txtPriority.Text = "Normal"
txtUrlContentBase.Text = "http://www.contoso.com/images"
txtUrlContentLocation.Text = "http://www.contoso.com/images"
' Name of relay mail server in your domain.
txtMailServer.Text = "smarthost" '
End If
End Sub
Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim sTo As String, sFrom As String, sSubject As String, sBody As String
Dim sAttach As String, sCc As String, sBcc As String, sBodyEncoding As String
Dim sBodyFormat As String, sMailServer As String, sPriority As String
Dim sUrlContentBase As String, sUrlContentLocation As String
Dim iLoop1 As Integer
sTo = Trim(txtTo.Text)
sFrom = Trim(txtFrom.Text)
sSubject = Trim(txtSubject.Text)
sBody = Trim(txtBody.Text)
sAttach = Trim(txtAttach.Text)
sCc = Trim(txtCc.Text)
sBcc = Trim(txtBcc.Text)
sBodyFormat = Trim(txtBodyFormat.Text)
sBodyEncoding = Trim(txtBodyEncoding.Text)
sPriority = Trim(txtPriority.Text)
sUrlContentBase = Trim(txtUrlContentBase.Text)
sUrlContentLocation = Trim(txtUrlContentLocation.Text)
sMailServer = Trim(txtMailServer.Text)
Dim MyMail As MailMessage = New MailMessage()
MyMail.From = sFrom
MyMail.To = sTo
MyMail.Subject = sSubject
MyMail.Body = sBody
MyMail.Cc = sCc
MyMail.Bcc = sBcc
MyMail.UrlContentBase = sUrlContentBase
MyMail.UrlContentLocation = sUrlContentLocation
Select Case txtBodyEncoding.Text
Case Encoding.UTF7.EncodingName : MyMail.BodyEncoding = Encoding.UTF7
Case Encoding.UTF8.EncodingName : MyMail.BodyEncoding = Encoding.UTF8
Case Else : MyMail.BodyEncoding = Encoding.ASCII
End Select
Select Case UCase(sBodyFormat)
Case "HTML" : MyMail.BodyFormat = MailFormat.Html
Case Else : MyMail.BodyFormat = MailFormat.Text
End Select
Select Case UCase(sPriority)
Case "HIGH" : MyMail.Priority = MailPriority.High
Case "LOW" : MyMail.Priority = MailPriority.Low
Case Else : MyMail.Priority = MailPriority.Normal
End Select
' Build an IList of mail attachments.
If sAttach <> "" Then
Dim delim As Char = ","
Dim sSubstr As String
For Each sSubstr In sAttach.Split(delim)
Dim myAttachment As MailAttachment = New MailAttachment(sSubstr)
MyMail.Attachments.Add(myAttachment)
Next
End If
SmtpMail.SmtpServer = sMailServer
SmtpMail.Send(MyMail)
lblMsg1.Text = "VB Message sent to " & MyMail.To
End Sub
Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs)
lblMsg1.Text = ""
txtTo.Text = ""
txtFrom.Text = ""
txtSubject.Text = ""
txtBody.Text = ""
txtAttach.Text = ""
txtBcc.Text = ""
txtCc.Text = ""
txtBodyEncoding.Text = ""
txtBodyFormat.Text = ""
txtPriority.Text = ""
txtUrlContentBase.Text = ""
txtUrlContentLocation.Text = ""
txtMailServer.Text = ""
btnSubmit.Text = "Submit"
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Send a new mail message:</title>
</head>
<body>
<h4>Send a new mail message:</h4>
<form id="form1" method="Post" action="MailForm.aspx" runat="server">
<table style="width:350; background-color:#FFFF99">
<tr>
<td align="Right"><b>To:</b></td>
<td><Asp:Textbox id="txtTo" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>From:</b></td>
<td><Asp:Textbox id="txtFrom" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>Subject:</b></td>
<td><Asp:Textbox id="txtSubject" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>MessageBody:</b></td>
<td><Asp:Textbox id="txtBody" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>Attachments:</b></td>
<td><Asp:Textbox id="txtAttach" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>CC:</b></td>
<td><Asp:Textbox id="txtBcc" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>BCC:</b></td>
<td><Asp:Textbox id="txtCc" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>BodyEncoding:</b></td>
<td><Asp:Textbox id="txtBodyEncoding" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>BodyFormat:</b></td>
<td><Asp:Textbox id="txtBodyFormat" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>Priority:</b></td>
<td><Asp:Textbox id="txtPriority" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>URL Content Base:</b></td>
<td><Asp:Textbox id="txtUrlContentBase" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>URL Content Location:</b></td>
<td><Asp:Textbox id="txtUrlContentLocation" runat="server"/></td>
</tr>
<tr>
<td align="Right"><b>Mail Server:</b></td>
<td><Asp:Textbox id="txtMailServer" runat="server"/></td>
</tr>
</table><br />
<asp:button id="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat="server"/>
<asp:button id="btnClear" Text="Clear" OnClick="btnClear_Click" runat="server"/>
<p><asp:Label id="lblMsg1" runat="server"/></p>
</form>
</body>
</html>
建構函式
MailMessage() |
已淘汰.
初始化 MailMessage 類別的新執行個體。 建議的替代做法: System.Net.Mail。 |
屬性
Attachments |
已淘汰.
指定與訊息一起傳送的附件集合。 建議的替代做法: System.Net.Mail。 |
Bcc |
已淘汰.
對於會接收電子郵件訊息之密件副本 (BCC) 的電子郵件地址,取得或設定分號分隔清單。 建議的替代做法: System.Net.Mail。 |
Body |
已淘汰.
取得或設定電子郵件訊息的內文。 建議的替代做法: System.Net.Mail。 |
BodyEncoding |
已淘汰.
取得或設定電子郵件訊息的內文編碼類型。 建議的替代做法: System.Net.Mail。 |
BodyFormat |
已淘汰.
取得或設定電子郵件訊息的內文內容類型。 建議的替代做法: System.Net.Mail。 |
Cc |
已淘汰.
對於會接收電子郵件訊息之副本 (CC) 的電子郵件地址,取得或設定分號分隔清單。 建議的替代做法: System.Net.Mail。 |
Fields |
已淘汰.
取得物件的集合 (這些物件會對應至 Microsoft Collaboration Data Objects (CDO) 欄位)。 建議的替代做法: System.Net.Mail。 |
From |
已淘汰.
取得或設定寄件者的電子郵件地址。 建議的替代做法: System.Net.Mail。 |
Headers |
已淘汰.
指定會與電子郵件訊息一起傳送的自訂標頭。 建議的替代做法: System.Net.Mail。 |
Priority |
已淘汰.
取得或設定電子郵件訊息的優先權。 建議的替代做法: System.Net.Mail。 |
Subject |
已淘汰.
取得或設定電子郵件訊息的主旨。 建議的替代做法: System.Net.Mail。 |
To |
已淘汰.
取得或設定以分號區隔的收件者電子郵件地址清單。 建議的替代做法: System.Net.Mail。 |
UrlContentBase |
已淘汰.
取得或設定 |
UrlContentLocation |
已淘汰.
取得或設定電子郵件訊息的 |
方法
Equals(Object) |
已淘汰.
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
已淘汰.
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
已淘汰.
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
已淘汰.
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
已淘汰.
傳回代表目前物件的字串。 (繼承來源 Object) |