หน้าเว็บ ASP.NET - WebMail Helper
WebMail Helper - หนึ่งในผู้ช่วยเว็บ ASP.NET ที่มีประโยชน์มากมาย
ด้วยวัตถุ WebMail คุณสามารถส่งอีเมลจากหน้าเว็บได้อย่างง่ายดาย
ตัวช่วยเว็บเมล
WebMail Helper ทำให้ง่ายต่อการส่งอีเมลจากเว็บแอปพลิเคชันโดยใช้ SMTP (Simple Mail Transfer Protocol)
สถานการณ์จำลอง: การสนับสนุนทางอีเมล
เพื่อสาธิตการใช้อีเมล เราจะสร้างหน้าป้อนข้อมูลสำหรับการสนับสนุน ให้ผู้ใช้ส่งหน้าไปยังหน้าอื่น และส่งอีเมลเกี่ยวกับปัญหาการสนับสนุน
ขั้นแรก: แก้ไข AppStart Page ของคุณ
หากคุณได้สร้างแอปพลิเคชันสาธิตในบทช่วยสอนนี้ แสดงว่าคุณมีหน้าที่ชื่อ _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)
ชื่อ ผู้ใช้:ชื่อของบัญชีอีเมล SMTP ที่ใช้ในการส่งอีเมล
รหัสผ่าน:รหัสผ่านของบัญชีอีเมล SMTP
จาก:อีเมลที่จะปรากฏในที่อยู่ต้นทาง (มักจะเหมือนกับชื่อผู้ใช้)
ประการที่สอง: สร้างหน้าป้อนอีเมล
จากนั้นสร้างหน้าป้อนข้อมูลและตั้งชื่อเป็น 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:"[email protected]", subject: "Help request from - " + customerEmail, body:
customerRequest );
}
catch (Exception ex )
{
<text>@ex</text>
}
}
การอ้างอิงออบเจ็กต์เว็บเมล - คุณสมบัติ
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 |
การอ้างอิงออบเจ็กต์เว็บเมล - เมธอด
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 |
การเริ่มต้น WebMail Helper
หากต้องการใช้ตัวช่วย WebMail คุณต้องเข้าถึงเซิร์ฟเวอร์ SMTP SMTP คือส่วน "เอาต์พุต" ของอีเมล หากคุณใช้โฮสต์เว็บ คุณอาจทราบชื่อเซิร์ฟเวอร์ SMTP แล้ว หากคุณทำงานในเครือข่ายองค์กร แผนกไอทีของคุณสามารถตั้งชื่อให้คุณได้ หากคุณกำลังทำงานอยู่ที่บ้าน คุณอาจใช้ผู้ให้บริการอีเมลทั่วไปได้
ในการส่งอีเมล คุณจะต้อง:
- ชื่อของเซิร์ฟเวอร์ SMTP
- หมายเลขพอร์ต (ส่วนใหญ่มักจะ 25)
- ชื่อผู้ใช้อีเมล
- รหัสผ่านอีเมล
ในรูทของเว็บ ให้สร้างเพจ (หรือแก้ไขเพจ ) ชื่อ_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]"
}
รหัสด้านบนจะทำงานทุกครั้งที่เว็บไซต์ (แอปพลิเคชัน) เริ่มทำงาน มันฟีดWebMail Object ของคุณ ด้วยค่าเริ่มต้น
กรุณาแทนที่:
smtp.example.comด้วยชื่อเซิร์ฟเวอร์ SMTP ที่จะใช้ในการส่งอีเมล
25พร้อมหมายเลขพอร์ตที่เซิร์ฟเวอร์จะใช้เพื่อส่งธุรกรรม SMTP (อีเมล)
falseกับ true หากเซิร์ฟเวอร์ควรใช้การเข้ารหัส SSL (Secure Socket Layer)
[email protected]โดยใช้ชื่อบัญชีอีเมล SMTP ที่ใช้ส่งอีเมล
รหัสผ่านด้วยรหัสผ่านของบัญชีอีเมล SMTP
john@exampleพร้อมอีเมลที่จะปรากฏในที่อยู่ต้นทาง
คุณไม่จำเป็นต้องเริ่มต้นวัตถุ WebMail ในไฟล์ AppStart แต่คุณต้องตั้งค่าคุณสมบัติเหล่านี้ก่อนที่จะเรียกใช้ เมธอดWebMail.Send()