From 79bf391df757c50374a5acde3008860d48295926 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Fri, 1 Dec 2017 17:56:38 +0100 Subject: Added periodical checking of pollution and sending e-mail. --- PollutometerWebApi/EmailSender.cs | 40 +++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'PollutometerWebApi/EmailSender.cs') diff --git a/PollutometerWebApi/EmailSender.cs b/PollutometerWebApi/EmailSender.cs index 0e328c9..9effa47 100644 --- a/PollutometerWebApi/EmailSender.cs +++ b/PollutometerWebApi/EmailSender.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Net.Mail; namespace PollutometerWebApi @@ -7,20 +8,35 @@ namespace PollutometerWebApi { public EmailSender() {} - public static void SendEmail() + public static void SendEmail(string gasName, double max) { - MailMessage mail = new MailMessage("***REMOVED***", "***REMOVED***@edu.easj.dk"); - SmtpClient client = new SmtpClient() + try { - Host = "mail.cock.li", - Port = 465, - DeliveryMethod = SmtpDeliveryMethod.Network, - UseDefaultCredentials = false, - Credentials = new System.Net.NetworkCredential("***REMOVED***", "***REMOVED***") - }; - mail.Subject = "this is a test email."; - mail.Body = "this is my test email body"; - client.Send(mail); + MailMessage mail = new MailMessage("***REMOVED***", "***REMOVED***@edu.easj.dk"); + SmtpClient client = new SmtpClient() + { + Host = "mail.cock.li", + Port = 587, + EnableSsl = true, + Timeout = 100, + DeliveryMethod = SmtpDeliveryMethod.Network, + UseDefaultCredentials = false, + Credentials = new System.Net.NetworkCredential("***REMOVED***", "***REMOVED***") + }; + mail.Subject = $"Pollutometer warning - {DateTime.Now}"; + mail.IsBodyHtml = true; + mail.Body = "

WARNING!

\n" + + "\n" + + "\n" + + "\n" + + $"The warning was triggered by {gasName}. " + + $"Air quality index: {max}"; + client.Send(mail); + } + catch(Exception ex) + { + Console.Write(ex.Message); + } } } } \ No newline at end of file -- cgit v1.2.3