This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Email.cs
73 lines (65 loc) · 2.59 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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using log4net;
namespace FlyingPie
{
public class Email
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static void SendErrorMail(string message, Exception exception)
{
log.Error(message, exception);
var errorRecipients = ConfigurationManager.AppSettings["ErrorEmails"];
var body = $"Error message: {message}\nException: {exception}";
SendEmail("IYD Notifier Error", body, errorRecipients, false);
}
public static bool SendNotificationMail(string body)
{
//TODO: use nice HTML formatting instead of boring plain text - this looks promising for templating: https://github.com/Antaris/RazorEngine
const string subject = "IYD Update Notification";
var updateRecipients = ConfigurationManager.AppSettings["UpdateEmails"];
return SendEmail(subject, body, updateRecipients, false);
}
private static bool SendEmail(string subject, string message, string recipients, bool isHtml)
{
//TODO: include retries for certain errors? don't want to try indefinitely though
try
{
var fromEmail = ConfigurationManager.AppSettings["FromEmailAddress"];
var fromAddress = new MailAddress(fromEmail, "Flying Pie IYD Notifier");
var emailPassword = Utilities.GetEmailPassword(false);
using (var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromEmail, emailPassword)
})
using (var email = new MailMessage
{
From = fromAddress,
Subject = subject,
Body = message,
IsBodyHtml = isHtml
})
{
email.To.Add(recipients);
smtp.Send(email);
return true;
}
}
catch (Exception e)
{
log.ErrorFormat("Failed to send email! Exception:\n{0}", e);
return false;
}
}
}
}