-
-
Notifications
You must be signed in to change notification settings - Fork 958
/
Email.cs
203 lines (157 loc) · 6.74 KB
/
Email.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation.Results;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http.Dispatchers;
using NzbDrone.Core.Localization;
using NzbDrone.Core.Movies;
namespace NzbDrone.Core.Notifications.Email
{
public class Email : NotificationBase<EmailSettings>
{
private readonly ICertificateValidationService _certificateValidationService;
private readonly ILocalizationService _localizationService;
private readonly Logger _logger;
public override string Name => _localizationService.GetLocalizedString("NotificationsEmailSettingsName");
public Email(ICertificateValidationService certificateValidationService, ILocalizationService localizationService, Logger logger)
{
_certificateValidationService = certificateValidationService;
_localizationService = localizationService;
_logger = logger;
}
public override string Link => null;
public override void OnGrab(GrabMessage grabMessage)
{
var body = $"{grabMessage.Message} sent to queue.";
SendEmail(Settings, MOVIE_GRABBED_TITLE_BRANDED, body);
}
public override void OnDownload(DownloadMessage message)
{
var body = $"{message.Message} Downloaded and sorted.";
SendEmail(Settings, MOVIE_DOWNLOADED_TITLE_BRANDED, body);
}
public override void OnMovieAdded(Movie movie)
{
var body = $"{movie.Title} added to library.";
SendEmail(Settings, MOVIE_ADDED_TITLE_BRANDED, body);
}
public override void OnMovieFileDelete(MovieFileDeleteMessage deleteMessage)
{
var body = $"{deleteMessage.Message} deleted.";
SendEmail(Settings, MOVIE_FILE_DELETED_TITLE_BRANDED, body);
}
public override void OnMovieDelete(MovieDeleteMessage deleteMessage)
{
var body = $"{deleteMessage.Message}";
SendEmail(Settings, MOVIE_DELETED_TITLE_BRANDED, body);
}
public override void OnHealthIssue(HealthCheck.HealthCheck message)
{
SendEmail(Settings, HEALTH_ISSUE_TITLE_BRANDED, message.Message);
}
public override void OnHealthRestored(HealthCheck.HealthCheck previousMessage)
{
SendEmail(Settings, HEALTH_RESTORED_TITLE_BRANDED, $"The following issue is now resolved: {previousMessage.Message}");
}
public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage)
{
var body = $"{updateMessage.Message}";
SendEmail(Settings, APPLICATION_UPDATE_TITLE_BRANDED, body);
}
public override void OnManualInteractionRequired(ManualInteractionRequiredMessage message)
{
var body = $"{message.Message} requires manual interaction.";
SendEmail(Settings, MANUAL_INTERACTION_REQUIRED_TITLE_BRANDED, body);
}
public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(Test(Settings));
return new ValidationResult(failures);
}
private void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false)
{
var email = new MimeMessage();
email.From.Add(ParseAddress("From", settings.From));
email.To.AddRange(settings.To.Select(x => ParseAddress("To", x)));
email.Cc.AddRange(settings.Cc.Select(x => ParseAddress("CC", x)));
email.Bcc.AddRange(settings.Bcc.Select(x => ParseAddress("BCC", x)));
email.Subject = subject;
email.Body = new TextPart(htmlBody ? "html" : "plain")
{
Text = body
};
_logger.Debug("Sending email Subject: {0}", subject);
try
{
Send(email, settings);
_logger.Debug("Email sent. Subject: {0}", subject);
}
catch (Exception ex)
{
_logger.Error("Error sending email. Subject: {0}", email.Subject);
_logger.Debug(ex, ex.Message);
throw;
}
_logger.Debug("Finished sending email. Subject: {0}", subject);
}
private void Send(MimeMessage email, EmailSettings settings)
{
using var client = new SmtpClient();
client.Timeout = 10000;
var useEncyption = (EmailEncryptionType)settings.UseEncryption;
var serverOption = useEncyption switch
{
EmailEncryptionType.Always => settings.Port == 465
? SecureSocketOptions.SslOnConnect
: SecureSocketOptions.StartTls,
EmailEncryptionType.Never => SecureSocketOptions.None,
_ => SecureSocketOptions.Auto
};
client.ServerCertificateValidationCallback = _certificateValidationService.ShouldByPassValidationError;
_logger.Debug("Connecting to mail server");
client.Connect(settings.Server, settings.Port, serverOption);
if (!string.IsNullOrWhiteSpace(settings.Username))
{
_logger.Debug("Authenticating to mail server");
client.Authenticate(settings.Username, settings.Password);
}
_logger.Debug("Sending to mail server");
client.Send(email);
_logger.Debug("Sent to mail server, disconnecting");
client.Disconnect(true);
_logger.Debug("Disconnecting from mail server");
}
public ValidationFailure Test(EmailSettings settings)
{
const string body = "Success! You have properly configured your email notification settings";
try
{
SendEmail(settings, "Radarr - Test Notification", body);
}
catch (Exception ex)
{
_logger.Error(ex, "Unable to send test email");
return new ValidationFailure("Server", _localizationService.GetLocalizedString("NotificationsValidationUnableToSendTestMessage", new Dictionary<string, object> { { "exceptionMessage", ex.Message } }));
}
return null;
}
private MailboxAddress ParseAddress(string type, string address)
{
try
{
return MailboxAddress.Parse(address);
}
catch (Exception ex)
{
_logger.Error(ex, "{0} email address '{1}' invalid", type, address);
throw;
}
}
}
}