50 lines
1.8 KiB
C#
50 lines
1.8 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Net.Mail;
|
|
using Traffic.Data.ViewModels;
|
|
using Traffic.Repository.Interfaces;
|
|
using Traffic.Service.Interfaces;
|
|
|
|
namespace Traffic.Service.Implements
|
|
{
|
|
public class MailService : IMailService
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public MailService(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 傳送 Mail
|
|
/// </summary>
|
|
public void SendMail(MailViewModel mailViewModel)
|
|
{
|
|
using (MailMessage mail = new MailMessage())
|
|
{
|
|
mail.From = new MailAddress(mailViewModel.From);//寄件人
|
|
foreach (var toAddress in mailViewModel.To)
|
|
{
|
|
mail.To.Add(toAddress);//收件人
|
|
}
|
|
mail.Subject = mailViewModel.Subject;
|
|
mail.SubjectEncoding = mailViewModel.SubjectEncoding;
|
|
mail.Body = mailViewModel.Body;
|
|
mail.IsBodyHtml = mailViewModel.IsBodyHtml;
|
|
mail.BodyEncoding = mailViewModel.BodyEncoding;
|
|
mail.Priority = mailViewModel.Priority;
|
|
|
|
using (var smtp = new SmtpClient(_configuration["MailServer:Host"], Convert.ToInt16(_configuration["MailServer:Port"])))
|
|
{
|
|
var account = _configuration["MailServer:Account"];//mail account
|
|
var password = _configuration["MailServer:Password"];//mail password
|
|
smtp.Credentials = new System.Net.NetworkCredential(account, password);
|
|
smtp.EnableSsl = true;
|
|
smtp.Send(mail);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|