ASP.NET Web Pages - WebMail 幫助器
- 上一頁 WebPages 圖表
- 下一頁 WebPages PHP
WebMail 幫助器 - 眾多有用的 ASP.NET Web 幫助器之一。
WebMail 幫助器
WebMail 幫助器使我們更容易從 web 應用程序中使用 SMTP 來發送電郵。
腳本: Email 支持
為了演示電子郵件的使用,我們將創建用于技術支持的輸入頁面,讓用戶向另一個頁面提交該頁面,然后發送一封有關支持問題的電子郵件。
首先:編輯您的 AppStart 頁面
如果您曾構建過本教程中的 DEMO 應用程序,那么站點中應該存在擁有如下內容的 _AppStart.cshtml 頁面:
_AppStart.cshtml
@{ WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true); }
如需初始化 WebMail 幫助器,請向您的 AppStart 頁面添加以下 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: 發送電郵所使用的 SMTP 服務器的名稱。
SmtpPort: 發送 SMTP transactions (電郵) 所用的服務器端口。
EnableSsl: True,如果服務器應該使用 SSL (Secure Socket Layer) 加密。
UserName: 發送電郵所用的 SMTP email 賬戶的名稱。
Password: SMTP 電郵賬戶的密碼。
From: 出現在 from 欄中的電郵地址(通常與 UserName 相同)。
第二:創建電郵輸入頁面
然后創建輸入頁面,名為 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>
輸入頁面的作用是收集信息,然后把數據提交到一個能夠將信息作為郵件來發送的新頁面。
第三:創建郵件發送頁面
然后創建用于發送電郵的頁面,名為 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> } }
如需更多有關從 ASP.NET Web Pages 應用程序發送電子郵件的信息,請參閱:WebMail 對象參考手冊。
- 上一頁 WebPages 圖表
- 下一頁 WebPages PHP