EmbeddedMailObject コンストラクタ

名前 | 説明 |
---|---|
EmbeddedMailObject () | EmbeddedMailObject クラスの新しいインスタンスを初期化します。 |
EmbeddedMailObject (String, String) | 識別名とオブジェクトを読み込むパスを指定して、EmbeddedMailObject クラスの新しいインスタンスを初期化します。 |

EmbeddedMailObject コンストラクタ (String, String)
アセンブリ: System.Web (system.web.dll 内)


ChangePassword コントロールを使用し、SendingMail という名前の SendingMail イベントのイベント ハンドラを含む、ASP.NET ページの分離コードの例を次のコード例に示します。このコード例では、ASP.NET メンバシップとフォーム認証を使用するように構成された ASP.NET Web サイトと、名前とパスワードがわかっているユーザーが作成されていることを前提にしています。詳細については、「方法 : 簡単なフォーム認証を実装する」を参照してください。
パスワードの変更に成功した場合、SendingMail イベント ハンドラのコードは、変更内容を確認する電子メール メッセージをユーザーに送信します。このコード例を実行するには、あらかじめサーバー上で SMTP を構成しておく必要があります。SMTP サーバーを構成する方法については、「方法 : IIS 内で SMTP 仮想サーバーをインストールおよび構成する」を参照してください。この例の目的では、必ずしも SMTP サーバーを構成する必要はありません。この例は、電子メール メッセージの送信エラーをテストするために構築されています。
メール サーバーが正しく構成されていない場合、または他のエラーが発生して、電子メール メッセージを送信できない場合、SendMailError 関数が呼び出されます。ユーザーにメッセージが表示されます。さらに、MySamplesSite という名前のイベント ソースが既に存在するという前提で、Windows のアプリケーション イベント ログにイベントが記録されます。指定されたイベント ソースを作成するには、下記のコード例を参照してください。イベント ソースの作成の詳細については、「ASP.NET Web ページのサーバー イベント処理」を参照してください。エラーが処理されたことを示すために、SendMailErrorEventArgs オブジェクトの Handled プロパティは true に設定されます。
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="ChangePassword.vb" Inherits="ChangePassword_vb_aspx" %> <html > <head runat="server"> <title>ChangePassword using code-behind including a SendMailError Event</title> </head> <body> <form id="form1" runat="server"> <div align="center"> <h1>ChangePassword</h1> <asp:LoginView ID="LoginView1" Runat="server" Visible="true"> <LoggedInTemplate> <asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." /> <BR /> </LoggedInTemplate> <AnonymousTemplate> You are not logged in </AnonymousTemplate> </asp:LoginView><br /> <asp:ChangePassword ID="ChangePassword1" Runat="server" BorderStyle="Solid" BorderWidth="1" CancelDestinationPageUrl="~/Default.aspx" DisplayUserName="true" ContinueDestinationPageUrl="~/Default.aspx" OnSendingMail="_SendingMail" OnSendMailError="_SendMailError" > </asp:ChangePassword><br /> <asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br /> <asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="~/Default.aspx"> Home </asp:HyperLink> </div> </form> </body> </html>
<%@ Page Language="C#" CodeFile="ChangePassword.cs" Inherits="ChangePassword_cs_aspx" %> <html > <head id="Head1" runat="server"> <title>ChangePassword using code-behind including a SendMailError Event</title> </head> <body> <form id="form1" runat="server"> <div align="center"> <h1>ChangePassword</h1> <asp:LoginView ID="LoginView1" Runat="server" Visible="true"> <LoggedInTemplate> <asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." /> <BR /> </LoggedInTemplate> <AnonymousTemplate> You are not logged in </AnonymousTemplate> </asp:LoginView><br /> <asp:ChangePassword ID="ChangePassword1" Runat="server" BorderStyle="Solid" BorderWidth="1" CancelDestinationPageUrl="~/Default.aspx" DisplayUserName="true" ContinueDestinationPageUrl="~/Default.aspx" > </asp:ChangePassword><br /> <asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br /> <asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="~/Default.aspx"> Home </asp:HyperLink> </div> </form> </body> </html>
Imports System 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 Imports Microsoft.VisualBasic 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
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; } }
MySamplesSite という名前のイベント ソースをプログラムによってアプリケーション ログに追加する必要がある場合は、次のコード例を使用します。最初のコード例を正常に実行するには、このイベント ソースが存在する必要があります。次のコード例には、管理者特権が必要です。
Imports System 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
#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()); } } } }
次のコード例は、前のコード例の 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="https://cdn.weblio.jp/e7/img/dict/msdnc/cid:LoginGif" alt="Log In" /> </a> </p> <p> Please read our attached Privacy Notice. </p> </form> </body> </html>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


EmbeddedMailObject コンストラクタ ()
アセンブリ: System.Web (system.web.dll 内)


埋め込み項目の ID を取得または設定するには、Name プロパティを使用します。埋め込み項目へのパスを取得または設定するには、Path プロパティを使用します。メール メッセージに正常に項目を埋め込むには、両方のプロパティを設定する必要があります。

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- EmbeddedMailObject コンストラクタのページへのリンク