| | | 1 | | using System.Net; |
| | | 2 | | using System.Net.Mail; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using SlqStudio.Application.Services.EmailService.Models; |
| | | 5 | | |
| | | 6 | | namespace SlqStudio.Application.Services.EmailService.Implementation; |
| | | 7 | | |
| | | 8 | | public class EmailService : IEmailService |
| | | 9 | | { |
| | | 10 | | private readonly SmtpSettings _smtpSettings; |
| | | 11 | | |
| | 0 | 12 | | public EmailService(IOptions<SmtpSettings> smtpSettings) |
| | | 13 | | { |
| | 0 | 14 | | _smtpSettings = smtpSettings.Value; |
| | 0 | 15 | | } |
| | | 16 | | |
| | | 17 | | public async Task<bool> SendEmailAsync(string subject, string body) |
| | | 18 | | { |
| | | 19 | | try |
| | | 20 | | { |
| | 0 | 21 | | var fromAddress = new MailAddress(_smtpSettings.SenderEmail ?? "", _smtpSettings.SenderName); |
| | 0 | 22 | | var toAddress = new MailAddress(_smtpSettings.RecipientEmail ?? ""); |
| | | 23 | | |
| | 0 | 24 | | var htmlBody = System.Net.WebUtility.HtmlEncode(body) |
| | 0 | 25 | | .Replace("\r\n", "<br>") |
| | 0 | 26 | | .Replace("\n", "<br>"); |
| | | 27 | | |
| | 0 | 28 | | using var message = new MailMessage(fromAddress, toAddress) |
| | 0 | 29 | | { |
| | 0 | 30 | | Subject = subject, |
| | 0 | 31 | | Body = htmlBody, |
| | 0 | 32 | | IsBodyHtml = true |
| | 0 | 33 | | }; |
| | | 34 | | |
| | 0 | 35 | | using var smtp = new SmtpClient(_smtpSettings.Server, _smtpSettings.Port) |
| | 0 | 36 | | { |
| | 0 | 37 | | Credentials = new NetworkCredential(_smtpSettings.SenderEmail, _smtpSettings.Password), |
| | 0 | 38 | | EnableSsl = _smtpSettings.EnableSsl |
| | 0 | 39 | | }; |
| | | 40 | | |
| | 0 | 41 | | await smtp.SendMailAsync(message); |
| | 0 | 42 | | return true; |
| | | 43 | | } |
| | 0 | 44 | | catch |
| | | 45 | | { |
| | 0 | 46 | | return false; |
| | | 47 | | } |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | public async Task<bool> SendEmailWithAttachmentAsync( |
| | | 51 | | string toAddress, |
| | | 52 | | string subject, |
| | | 53 | | string body, |
| | | 54 | | byte[] attachment, |
| | | 55 | | string attachmentName, |
| | | 56 | | string attachmentMimeType = "application/octet-stream") |
| | | 57 | | { |
| | | 58 | | try |
| | | 59 | | { |
| | 0 | 60 | | var fromAddress = new MailAddress(_smtpSettings.SenderEmail ?? "", _smtpSettings.SenderName); |
| | 0 | 61 | | var to = new MailAddress(toAddress); |
| | | 62 | | |
| | 0 | 63 | | using var message = new MailMessage(fromAddress, to) |
| | 0 | 64 | | { |
| | 0 | 65 | | Subject = subject, |
| | 0 | 66 | | Body = body, |
| | 0 | 67 | | IsBodyHtml = false |
| | 0 | 68 | | }; |
| | | 69 | | |
| | 0 | 70 | | using var ms = new MemoryStream(attachment); |
| | 0 | 71 | | var ct = new System.Net.Mime.ContentType(attachmentMimeType) { Name = attachmentName }; |
| | 0 | 72 | | message.Attachments.Add(new Attachment(ms, ct)); |
| | | 73 | | |
| | 0 | 74 | | using var smtp = new SmtpClient(_smtpSettings.Server, _smtpSettings.Port) |
| | 0 | 75 | | { |
| | 0 | 76 | | Credentials = new NetworkCredential(_smtpSettings.SenderEmail, _smtpSettings.Password), |
| | 0 | 77 | | EnableSsl = _smtpSettings.EnableSsl |
| | 0 | 78 | | }; |
| | | 79 | | |
| | 0 | 80 | | await smtp.SendMailAsync(message); |
| | 0 | 81 | | return true; |
| | | 82 | | } |
| | 0 | 83 | | catch |
| | | 84 | | { |
| | 0 | 85 | | return false; |
| | | 86 | | } |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | } |