EmbeddedMailObject 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 EmbeddedMailObject 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| EmbeddedMailObject() |
初始化 EmbeddedMailObject 類別的新執行個體。 |
| EmbeddedMailObject(String, String) |
初始化該類別的新實例 EmbeddedMailObject ,使用指定的識別碼名稱與路徑來填充該物件。 |
EmbeddedMailObject()
初始化 EmbeddedMailObject 類別的新執行個體。
public:
EmbeddedMailObject();
public EmbeddedMailObject();
Public Sub New ()
備註
要取得或設定嵌入項目的識別碼,請使用屬性 Name 。 要取得或設定嵌入物件的路徑,請使用該 Path 屬性。 這兩個屬性都必須設定,才能成功嵌入郵件訊息中的項目。
另請參閱
適用於
EmbeddedMailObject(String, String)
初始化該類別的新實例 EmbeddedMailObject ,使用指定的識別碼名稱與路徑來填充該物件。
public:
EmbeddedMailObject(System::String ^ name, System::String ^ path);
public EmbeddedMailObject(string name, string path);
new System.Web.UI.WebControls.EmbeddedMailObject : string * string -> System.Web.UI.WebControls.EmbeddedMailObject
Public Sub New (name As String, path As String)
參數
範例
以下程式碼範例展示了一個使用 ChangePassword 控制項的 ASP.NET 頁面背後程式碼範例,並包含名為 SendingMailSendingMail的事件處理程序。 此程式碼範例假設 ASP.NET 網站已設定使用 ASP.NET 會員資格與表單驗證,且已建立一位使用者,其姓名與密碼你已知。 欲了解更多資訊,請參閱 《如何實作簡單表單認證》。
若密碼變更成功,事件處理器中的 SendingMail 程式碼會嘗試發送電子郵件給使用者以確認變更。 SMTP 必須在伺服器上已設定好,此程式碼範例才能運作。 關於如何配置 SMTP 伺服器的資訊,請參閱 《如何在 IIS 6.0 中安裝與配置 SMTP 虛擬伺服器》。 在此範例中,無需配置 SMTP 伺服器;本範例設計用以測試郵件未發送的失敗。
若郵件伺服器設定不正確或發生其他錯誤導致郵件無法發送,則會呼叫該 SendMailError 函式。 訊息會顯示給使用者。 此外,事件會被記錄到 Windows 應用程式的事件日誌,並假設已有名為 MySamplesSite 的事件來源。 欲了解更多關於建立事件來源的資訊,請參閱 網頁表單頁面中的伺服器事件處理 ASP.NET。
Handled物件的SendMailErrorEventArgs屬性被設定為true表示錯誤已被處理。
以下程式碼範例示範使用程式碼背後的檔案。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class ChangePassword_cs_aspx : System.Web.UI.Page
{
protected void Page_Load(Object sender, EventArgs e)
{
// Manually register the event-handling methods.
ChangePassword1.SendingMail += new MailMessageEventHandler(this._SendingMail);
ChangePassword1.SendMailError += new SendMailErrorEventHandler(this._SendMailError);
ChangePassword1.MailDefinition.BodyFileName = "~/Attachments/ChangePasswordMail.htm";
EmbeddedMailObject loginGif = new EmbeddedMailObject();
loginGif.Name = "LoginGif";
loginGif.Path = "~/Attachments/Login.gif";
EmbeddedMailObject privacyNoticeTxt = new EmbeddedMailObject();
privacyNoticeTxt.Name = "PrivacyNoticeTxt";
privacyNoticeTxt.Path = "~/Attachments/PrivacyNotice.txt";
ChangePassword1.MailDefinition.EmbeddedObjects.Add(loginGif);
ChangePassword1.MailDefinition.EmbeddedObjects.Add(privacyNoticeTxt);
}
protected void _SendingMail(object sender, MailMessageEventArgs e)
{
Message1.Visible = true;
Message1.Text = "Sent mail to you to confirm the password change.";
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("someone@example.com", "Someone");
System.Net.Mail.MailAddress copy = new System.Net.Mail.MailAddress("someone@example.com", "Someone");
e.Message.From = from;
e.Message.CC.Add(copy);
e.Message.Subject = "Activity information for you";
e.Message.IsBodyHtml = true;
}
protected void _SendMailError(object sender, SendMailErrorEventArgs e)
{
Message1.Visible = true;
Message1.Text = "Could not send email to confirm password change.";
// The MySamplesSite event source has already been created by an administrator.
System.Diagnostics.EventLog myLog = new System.Diagnostics.EventLog();
myLog.Source = "MySamplesSite";
myLog.Log = "Application";
myLog.WriteEntry(
"Sending mail via SMTP failed with the following error: " +
e.Exception.Message.ToString(),
System.Diagnostics.EventLogEntryType.Error);
e.Handled = true;
}
}
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Partial Class ChangePassword_vb_aspx
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal Sender As Object, ByVal e As System.EventArgs)
AddHandler ChangePassword1.SendingMail, AddressOf Me._SendingMail
AddHandler ChangePassword1.SendMailError, AddressOf Me._SendMailError
ChangePassword1.MailDefinition.BodyFileName = "~/Attachments/ChangePasswordMail.htm"
ChangePassword1.MailDefinition.Cc = "someone@example.com"
ChangePassword1.MailDefinition.From = "someone@example.com"
Dim loginGif As New EmbeddedMailObject
loginGif.Name = "LoginGif"
loginGif.Path = "~/Attachments/Login.gif"
Dim privacyNoticeTxt As New EmbeddedMailObject
privacyNoticeTxt.Name = "PrivacyNoticeTxt"
privacyNoticeTxt.Path = "~/Attachments/PrivacyNotice.txt"
ChangePassword1.MailDefinition.EmbeddedObjects.Add(loginGif)
ChangePassword1.MailDefinition.EmbeddedObjects.Add(privacyNoticeTxt)
End Sub
Protected Sub _SendingMail(ByVal Sender As Object, ByVal e As MailMessageEventArgs)
Message1.Visible = True
Message1.Text = "Sent mail to you to confirm the password change."
e.Message.Subject = "Activity information for you"
e.Message.IsBodyHtml = True
End Sub
Protected Sub _SendMailError(ByVal Sender As Object, ByVal e As SendMailErrorEventArgs)
Message1.Visible = True
Message1.Text = "Could not send mail to confirm the password change."
' The MySamplesSite event source has already been created by an administrator.
Dim myLog As System.Diagnostics.EventLog
myLog = New System.Diagnostics.EventLog
myLog.Log = "Application"
myLog.Source = "MySamplesSite"
myLog.WriteEntry("Sending mail via SMTP failed with the following error: " & e.Exception.Message.ToString(), System.Diagnostics.EventLogEntryType.Error)
e.Handled = True
End Sub
End Class
如果你需要程式化地將名為 MySamplesSite 的事件來源加入應用程式日誌,請使用以下程式碼範例。 必須有這個事件來源,第一個程式碼範例才能正確運作。 以下程式碼範例需要管理員權限。
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
#endregion
namespace CreateEventSource
{
class Program
{
static void Main(string[] args)
{
try
{
// Create the source, if it does not already exist.
if (!EventLog.SourceExists("MySamplesSite"))
{
EventLog.CreateEventSource("MySamplesSite", "Application");
Console.WriteLine("Creating Event Source");
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySamplesSite";
// Write an informational entry to the event log.
myLog.WriteEntry("Testing writing to event log.");
Console.WriteLine("Message written to event log.");
}
catch (Exception e)
{
Console.WriteLine("Exception:");
Console.WriteLine("{0}", e.ToString());
}
}
}
}
Imports System.Collections.Generic
Imports System.Text
Imports System.Diagnostics
Namespace CreateEventSource
Class Program
Sub Main()
Try
' Create the source, if it does not already exist.
If Not (EventLog.SourceExists("MySamplesSite")) Then
EventLog.CreateEventSource("MySamplesSite", "Application")
Console.WriteLine("Creating Event Source")
End If
' Create an EventLog instance and assign its source.
Dim myLog As New EventLog
myLog.Source = "MySamplesSite"
' Write an informational entry to the event log.
myLog.WriteEntry("Testing writing to event log.")
Console.WriteLine("Message written to event log.")
Catch e As Exception
Console.WriteLine("Exception:")
Console.WriteLine(e.ToString)
End Try
End Sub
End Class
End Namespace
以下範例程式碼可作為前述範例程式碼的 ChangePasswordMail.htm 檔案使用。
這很重要
在電子郵件中傳送使用者帳號名稱或密碼可能構成安全威脅。 電子郵件通常以純文字傳送,並可由特殊的網路「嗅探」應用程式讀取。 為了提升安全性,請使用《 安全登入控制》中所述的緩解措施。
<html>
<head><title></title></head>
<body>
<form>
<h1>Your password for the account named "<%Username%>" has changed.</h1>
<p>
If you did not initiate this change, please call 1-206-555-0100.
</p>
<p>
<a href="http://www.contoso.com/login.aspx">
<img src="cid:LoginGif" alt="Log In" />
</a>
</p>
<p>
Please read our attached Privacy Notice.
</p>
</form>
</body>
</html>