ASP.NET Web Pages Email

2022-09-17 18:02 更新

WebMail 幫助器 - 眾多有用的 ASP.NET Web 幫助器之一。

WebMail 幫助器的作用是可以讓郵件的發(fā)送變得簡(jiǎn)單,詳細(xì)的使用請(qǐng)參考本文內(nèi)容。


WebMail 幫助器

WebMail 幫助器讓發(fā)送郵件變得更簡(jiǎn)單,它按照 SMTP(Simple Mail Transfer Protocol 簡(jiǎn)單郵件傳輸協(xié)議)從 Web 應(yīng)用程序發(fā)送郵件。


前提:電子郵件支持

為了演示如何使用電子郵件,我們將創(chuàng)建一個(gè)輸入頁(yè)面,讓用戶提交一個(gè)頁(yè)面到另一個(gè)頁(yè)面,并發(fā)送一封關(guān)于支持問(wèn)題的郵件。


第一:編輯您的 AppStart 頁(yè)面

如果在本教程中您已經(jīng)創(chuàng)建了 Demo 應(yīng)用程序,那么您已經(jīng)有一個(gè)名為 _AppStart.cshtml 的頁(yè)面,內(nèi)容如下:

_AppStart.cshtml

@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
}

要啟動(dòng) WebMail 幫助器,向您的 AppStart 頁(yè)面中增加如下所示的 WebMail 屬性:

_AppStart.cshtml

@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "support@example.com";
WebMail.Password = "password-goes-here";
WebMail.From = "john@example.com";

}

屬性解釋:

SmtpServer: 用于發(fā)送電子郵件的 SMTP 服務(wù)器的名稱。

SmtpPort: 服務(wù)器用來(lái)發(fā)送 SMTP 事務(wù)(電子郵件)的端口。

EnableSsl: 如果服務(wù)器使用 SSL(Secure Socket Layer 安全套接層)加密,則值為 true。

UserName: 用于發(fā)送電子郵件的 SMTP 電子郵件賬戶的名稱。

Password: SMTP 電子郵件賬戶的密碼。

From: 在發(fā)件地址欄顯示的電子郵件(通常與 UserName 相同)。


第二:創(chuàng)建一個(gè)電子郵件輸入頁(yè)面

接著創(chuàng)建一個(gè)輸入頁(yè)面,并將它命名為 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>

輸入頁(yè)面的目的是手機(jī)信息,然后提交數(shù)據(jù)到可以將信息作為電子郵件發(fā)送的一個(gè)新的頁(yè)面。


第三:創(chuàng)建一個(gè)電子郵件發(fā)送頁(yè)面

接著創(chuàng)建一個(gè)用來(lái)發(fā)送電子郵件的頁(yè)面,并將它命名為 Email_Send:

Email_Send.cshtml

@{ // Read input
var customerEmail = Request["customerEmail"];
var customerRequest = Request["customerRequest"];
try
{
// Send email
WebMail.Send(to:"someone@example.com", subject: "Help request from - " + customerEmail, body: customerRequest );
}
catch (Exception ex )
{
<text>@ex</text>
}
}

想了解更多關(guān)于 ASP.NET Web Pages 應(yīng)用程序發(fā)送電子郵件的信息,請(qǐng)查閱:WebMail 對(duì)象參考手冊(cè)

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)