AtlasX Web Service รองรับการส่งอีเมลผ่าน SMTP server โดยใช้ AppMailService ซึ่งรองรับทั้ง plain และ SSL/TLS connection และรองรับ self-signed certificate
การตั้งค่า
กำหนดค่า mail server ในไฟล์ .env
App__Email__SERVER=10.254.7.15
App__Email__PORT=25
App__Email__ENABLESSL=false
App__Email__USERNAME=username@domain.com
App__Email__PASSWORD=password
App__Email__SENDERADDRESS=sender@domain.com|Sender Name
App__Email__FROMPARAMETER=FROM
App__Email__TOPARAMETER=TO
App__Email__CCPARAMETER=CC
App__Email__BCCPARAMETER=BCC
App__Email__SUBJECTPARAMETER=SUBJECT
App__Email__BODYPARAMETER=BODY
App__Email__PRIORITYPARAMETER=PRIORITY
App__Email__TEMPLATEPATH=FilesLocalStorage/EmailTemplates| Field | Required | Description |
SERVER | Required | hostname หรือ IP address ของ SMTP server |
PORT | Required | Port ของ SMTP server เช่น 25, 587 |
ENABLESSL | Required | ถ้าเป็น true จะเชื่อมต่อด้วย StartTLS ถ้าเป็น false จะเชื่อมต่อแบบไม่เข้ารหัส |
USERNAME | Required | ชื่อผู้ใช้สำหรับ authenticate กับ SMTP server ถ้าว่างจะข้าม authentication |
PASSWORD | Required | รหัสผ่านสำหรับ authenticate กับ SMTP server |
SENDERADDRESS | Optional | อีเมลและชื่อผู้ส่งในรูปแบบ email|display name เช่น sender@domain.com|Sender Name |
FROMPARAMETER | Optional | อีเมลผู้ส่ง |
TOPARAMETER | Optional | ผู้รับ |
CCPARAMETER | Optional | ผู้รับสำเนา |
BCCPARAMETER | Optional | ผู้รับสำเนาลับ |
SUBJECTPARAMETER | Optional | หัวข้ออีเมล |
BODYPARAMETER | Optional | เนื้อหาอีเมล |
PRIORITYPARAMETER | Optional | ความสำคัญของอีเมล Low, Normal, High ค่าใดค่าหนึ่ง |
| Optional | path ของ folder ที่เก็บ email templates สำหรับใช้กับ GetTemplateAbsolutePath() |
Service Registration
1. โหลด Environment Variables
เรียก EnvironmentUtils.Load() ก่อน build application
EnvironmentUtils.Load();2. ลงทะเบียน Service
เพิ่ม MailService เข้า DI container ใน Program.cs
builder.Services.AddTransient<IAppMailService, AppMailService>();การใช้งาน
Inject IAppMailService
public class MyService
{
private readonly IAppMailService _mailService;
public MyService(IAppMailService mailService)
{
_mailService = mailService;
}
}Send Email
เรียก SendAsync() โดยระบุ recipients, subject และ HTML body
await _mailService.SendAsync(
toAddress: ["user@domain.com"],
subject: "แจ้งเตือนระบบ",
htmlBody: "<p>This is a test email.</p>"
);ส่งอีเมลหลายคนพร้อมกัน
await _mailService.SendAsync(
toAddress: [
"user1@domain.com",
"user2@domain.com",
"user3@domain.com"
],
subject: "แจ้งเตือนระบบ",
htmlBody: "<p>This is a test email.</p>"
);Email Template
GetTemplateAbsolutePath() คืน path เต็มของ folder ที่เก็บ template ตามที่กำหนดใน TemplatePath
ReplacePlaceholdersWithRegex() แทนที่ {{KEY}} ใน template ด้วยค่าจริงที่ส่งเข้ามา
var templatePath = _appSettings.Email!.GetTemplateAbsolutePath();
var template = await File.ReadAllTextAsync(Path.Join(templatePath, "test.html"));
var queryParameter = new QueryParameter
{
Parameters = new Dictionary<string, object?>
{
{ "NAME", "user" },
{ "EMAIL", "user@domain.com" }
}
};
var htmlBody = queryParameter.ReplacePlaceholdersWithRegex(template);
await _mailService.SendAsync(
toAddress: ["user@domain.com"],
subject: "test",
htmlBody: htmlBody
);