ASP.NETWebページ-Webメールヘルパー
WebMailヘルパー-多くの便利なASP.NETWebヘルパーの1つ。
WebMailオブジェクトを使用すると、Webページから簡単に電子メールを送信できます。
Webメールヘルパー
WebMail Helperを使用すると、SMTP(Simple Mail transfer Protocol)を使用してWebアプリケーションから電子メールを簡単に送信できます。
シナリオ:Eメールサポート
電子メールの使用法を示すために、サポート用の入力ページを作成し、ユーザーがページを別のページに送信できるようにして、サポートの問題について電子メールを送信します。
最初:AppStartページを編集します
このチュートリアルでデモアプリケーションを作成した場合は、次の内容の_AppStart.cshtmlというページが既にあります。
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId",
"Email", true);
}
WebMailヘルパーを開始するには、次のWebMailプロパティをAppStartページに追加します。
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId",
"Email", true);
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "[email protected]";
WebMail.Password = "password-goes-here";
WebMail.From = "[email protected]";
}
説明されたプロパティ:
SmtpServer:電子メールの送信に使用されるSMTPサーバーの名前。
SmtpPort:サーバーがSMTPトランザクション(電子メール)を送信するために使用するポート。
EnableSsl:サーバーがSSL(Secure Socket Layer)暗号化を使用する必要がある場合はTrue。
UserName:電子メールの送信に使用されるSMTP電子メールアカウントの名前。
パスワード: SMTP電子メールアカウントのパスワード。
差出人:差出人アドレス(多くの場合、ユーザー名と同じ)に表示される電子メール。
2番目:メール入力ページを作成します
次に、入力ページを作成し、Email_Inputという名前を付けます。
Email_Input.cshtml
<!DOCTYPE html>
<html>
<body>
<h1>Request for
Assistance</h1>
<form method="post" action="EmailSend.cshtml">
<label>Username:</label>
<input type="text" name="customerEmail" />
<label>Details about the problem:</label>
<textarea name="customerRequest"
cols="45" rows="4"></textarea>
<p><input type="submit" value="Submit"
/></p>
</form>
</body>
</html>
入力ページの目的は、情報を収集し、その情報を電子メールとして送信できる新しいページにデータを送信することです。
3番目:メール送信ページを作成する
次に、電子メールの送信に使用するページを作成し、Email_Sendという名前を付けます。
Email_Send.cshtml
@{ // Read input
var customerEmail = Request["customerEmail"];
var customerRequest = Request["customerRequest"];
try
{
// Send email
WebMail.Send(to:"[email protected]", subject: "Help request from - " + customerEmail, body:
customerRequest );
}
catch (Exception ex )
{
<text>@ex</text>
}
}
Webメールオブジェクトリファレンス-プロパティ
Properties | Description |
---|---|
SmtpServer | The name the SMTP server that will send the emails |
SmtpPort | The port the server will use to send SMTP emails |
EnableSsl | True, if the server should use SSL encryption |
UserName | The name of the SMTP account used to send the email |
Password | The password of the SMTP account |
From | The email to appear in the from address |
Webメールオブジェクトリファレンス-メソッド
Method | Description |
---|---|
Send() | Sends an email message to an SMTP server for delivery |
Send()メソッドには次のパラメーターがあります。
Parameter | Type | Description |
---|---|---|
to | String | The Email recipients (separated by semicolon) |
subject | String | The subject line |
body | String | The body of the message |
そして、次のオプションのパラメータ:
Parameter | Type | Description |
---|---|---|
from | String | The email of the sender |
cc | String | The cc emails (separated by semicolon) |
filesToAttach | Collection | Filenames |
isBodyHtml | Boolean | True if the email body is in HTML |
additionalHeaders | Collection | Additional headers |
技術データ
Name | Value |
---|---|
Class | System.Web.Helpers.WebMail |
Namespace | System.Web.Helpers |
Assembly | System.Web.Helpers.dll |
Webメールヘルパーの初期化
WebMailヘルパーを使用するには、SMTPサーバーにアクセスする必要があります。SMTPは、電子メールの「出力」部分です。Webホストを使用している場合は、SMTPサーバーの名前をすでに知っている可能性があります。企業ネットワークで作業している場合は、IT部門が名前を教えてくれます。自宅で作業している場合は、通常の電子メールプロバイダーを使用できる場合があります。
メールを送信するには、次のものが必要です。
- SMTPサーバーの名前
- ポート番号(ほとんどの場合25)
- メールユーザー名
- メールパスワード
Webのルートで、_AppStart.cshtmlという名前のページを作成します(またはページを編集します) 。
次のコードをファイル内に配置します。
_AppStart.cshtml
@{
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "[email protected]";
WebMail.Password = "password";
WebMail.From = "[email protected]"
}
上記のコードは、Webサイト(アプリケーション)が起動するたびに実行されます。Webメールオブジェクトに初期値をフィードします。
代用してください:
電子メールの送信に使用されるSMTPサーバーという名前のsmtp.example.com 。
25サーバーがSMTPトランザクション(電子メール)の送信に使用するポート番号。
サーバーがSSL(Secure Socket Layer)暗号化を使用する必要がある場合は、falseをtrueに設定します。
メールの送信に使用されるSMTPメールアカウントの名前を持つ[email protected] 。
SMTP電子メールアカウントのパスワードを使用したパスワード。
fromアドレスに表示される電子メールを含むjohn @ example 。
AppStartファイルでWebMailオブジェクトを開始する必要はありませんが、 WebMail.Send() メソッドを呼び出す前にこれらのプロパティを設定する必要があります。