ASP.NET Web Pages - WebMail 幫助器
WebMail 幫助器 - 眾多有用的 ASP.NET Web 幫助器之一。
WebMail 幫助器
WebMail 幫助器使我們更容易從 web 應(yīng)用程序中使用 SMTP 來(lái)發(fā)送電郵。
腳本: Email 支持
為了演示電子郵件的使用,我們將創(chuàng)建用于技術(shù)支持的輸入頁(yè)面,讓用戶向另一個(gè)頁(yè)面提交該頁(yè)面,然后發(fā)送一封有關(guān)支持問(wèn)題的電子郵件。
首先:編輯您的 AppStart 頁(yè)面
如果您曾構(gòu)建過(guò)本教程中的 DEMO 應(yīng)用程序,那么站點(diǎn)中應(yīng)該存在擁有如下內(nèi)容的 _AppStart.cshtml 頁(yè)面:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email",
true);
}
如需初始化 WebMail 幫助器,請(qǐng)向您的 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: 發(fā)送 SMTP transactions (電郵) 所用的服務(wù)器端口。
EnableSsl: True,如果服務(wù)器應(yīng)該使用 SSL (Secure Socket Layer) 加密。
UserName: 發(fā)送電郵所用的 SMTP email 賬戶的名稱。
Password: SMTP 電郵賬戶的密碼。
From: 出現(xiàn)在 from 欄中的電郵地址(通常與 UserName 相同)。
第二:創(chuàng)建電郵輸入頁(yè)面
然后創(chuàng)建輸入頁(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è)面的作用是收集信息,然后把數(shù)據(jù)提交到一個(gè)能夠?qū)⑿畔⒆鳛猷]件來(lái)發(fā)送的新頁(yè)面。
第三:創(chuàng)建郵件發(fā)送頁(yè)面
然后創(chuàng)建用于發(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è)。