通过


EmbeddedMailObject 构造函数

定义

初始化 EmbeddedMailObject 类的新实例。

重载

名称 说明
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)

参数

name
String

用作邮件中嵌入的项目标识符的名称。 有关详细信息,请参阅 Name

path
String

用于检索要在邮件中嵌入的项目的路径。 有关详细信息,请参阅 Path

示例

下面的代码示例显示了使用ChangePassword控件的 ASP.NET 页的代码隐藏示例,并包括名为SendingMailSendingMail事件的事件处理程序。 此代码示例假定 ASP.NET 网站已配置为使用 ASP.NET 成员身份和表单身份验证,并且已创建一个用户,其名称和密码已知。 有关详细信息,请参阅 “如何:实现简单表单身份验证”。

如果密码更改成功,事件处理程序中的 SendingMail 代码将尝试向用户发送电子邮件以确认更改。 必须已在服务器上配置 SMTP 才能运行此代码示例。 有关如何配置 SMTP 服务器的信息,请参阅 如何:在 IIS 6.0 中安装和配置 SMTP 虚拟服务器。 出于此示例的目的,不需要配置 SMTP 服务器;该示例构造为测试未能发送电子邮件。

如果未正确配置邮件服务器或发生其他错误,并且无法发送电子邮件,则会调用该 SendMailError 函数。 向用户显示一条消息。 此外,事件将记录到 Windows 应用程序事件日志中,假设名为 MySamplesSite 的事件源已存在。 有关创建事件源的详细信息,请参阅 ASP.NET Web 窗体页中的服务器事件处理。 对象的 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 &quot;<%Username%>&quot; 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>

另请参阅

适用于