60 lines
1.7 KiB
C#
60 lines
1.7 KiB
C#
using BMA.EHR.Domain.Extensions;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.Net;
|
|
using System.Net.Mail;
|
|
|
|
namespace BMA.EHR.Application.Messaging
|
|
{
|
|
public class EmailSenderService
|
|
{
|
|
#region " Fields "
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
#endregion
|
|
|
|
#region " Constructor and Destructor "
|
|
|
|
public EmailSenderService(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " Methods "
|
|
|
|
public void SendMail(string subject, string body, string receiver)
|
|
{
|
|
try
|
|
{
|
|
var server = _configuration["Mail:Server"];
|
|
var user = _configuration["Mail:User"];
|
|
var password = _configuration["Mail:Password"];
|
|
var port = _configuration["Mail:Port"];
|
|
var from = _configuration["Mail:MailFrom"];
|
|
|
|
var client = new SmtpClient(server, port.ToInteger());
|
|
client.UseDefaultCredentials = false;
|
|
client.Credentials = new NetworkCredential(user, password);
|
|
client.EnableSsl = true;
|
|
client.DeliveryMethod = SmtpDeliveryMethod.Network;
|
|
|
|
|
|
var mail = new MailMessage();
|
|
mail.From = new MailAddress(from, "ระบบบริหารทรัพยากรบุคคลของกรุงเทพมหานคร");
|
|
mail.To.Add(receiver);
|
|
mail.Subject = subject;
|
|
mail.Body = body;
|
|
mail.IsBodyHtml = true;
|
|
client.Send(mail);
|
|
}
|
|
catch
|
|
{
|
|
//throw;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|