-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathMailService.cs
139 lines (127 loc) · 3.69 KB
/
MailService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System.Net;
using System.Net.Mail;
using Application.Common.Interfaces.Services.Mail;
using Contracts.Dtos.Requests;
using Microsoft.Extensions.Options;
using Serilog;
namespace Infrastructure.Services.Mail;
public class MailService : IMailService
{
private readonly SmtpClient smtpClient;
private readonly ILogger logger;
private readonly EmailSettings emailSettings;
private bool disposed;
public MailService(IOptions<EmailSettings> options, ILogger logger)
{
emailSettings = options.Value;
smtpClient = new(emailSettings.Host, emailSettings.Port)
{
UseDefaultCredentials = false,
EnableSsl = true,
Credentials = new NetworkCredential(emailSettings.From, emailSettings.Password),
};
this.logger = logger;
}
public async Task<bool> SendAsync(MailMessageData metaData)
{
try
{
MailMessage message = CreateEmailMessage(
new MailData()
{
DisplayName = metaData.DisplayName,
Subject = metaData.Subject,
To = metaData.To,
},
metaData.Message!
);
await smtpClient.SendMailAsync(message);
logger.Information(
"Email has been sent successfully to {recipients}",
string.Join(", ", metaData.To)
);
return true;
}
catch (Exception ex)
{
logger.Error(
ex,
"Failed to send email to {recipients}: {error}",
string.Join(", ", metaData.To),
ex.Message
);
return false;
}
}
public async Task<bool> SendWithTemplateAsync(MailTemplateData metaData)
{
try
{
string template = await RazorViewToStringRenderer.RenderViewToStringAsync(
metaData.Template!
);
MailMessage message = CreateEmailMessage(
new MailData()
{
DisplayName = metaData.DisplayName,
Subject = metaData.Subject,
To = metaData.To,
},
template
);
await smtpClient.SendMailAsync(message);
logger.Information(
"Email has been sent successfully to {recipients}",
string.Join(", ", metaData.To)
);
return true;
}
catch (Exception ex)
{
logger.Error(
ex,
"Failed to send email to {recipients}: {error}",
string.Join(", ", metaData.To),
ex.Message
);
return false;
}
}
private MailMessage CreateEmailMessage(MailData mailData, string body)
{
MailMessage mailMessage =
new()
{
Subject = mailData.Subject,
Body = body,
From = new MailAddress(emailSettings.From!, mailData.DisplayName),
IsBodyHtml = true,
};
foreach (string recipient in mailData.To)
{
mailMessage.To.Add(recipient);
}
return mailMessage;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
smtpClient?.Dispose();
}
disposed = true;
}
}
// Finalizer in case Dispose is not called
~MailService()
{
Dispose(false);
}
}