private static void SendEmail(EmailDetails details, string sendingTo, string subject, string content) { var mail = new MailMessage(); var client = new SmtpClient(); client.Port = 587; client.Host = details.Host; mail.To.Add(new MailAddress(sendingTo)); mail.From = new MailAddress(details.From); mail.Subject = subject; mail.Body = content; mail.IsBodyHtml = true; client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(details.Username, details.Password); if (Constants.SendEmails) { client.Send(mail); } else { Logger.WriteLine("SENDING MASS EMAIS IS SET TO FALSE"); Logger.SaveToFile(); File.WriteAllText("testEmail.html", content); } }
private static void SendEmail(EmailDetails details, string sendingTo, string subject, string content) { var mail = new MailMessage(); var client = new SmtpClient(); client.Port = 587; client.Host = details.Host; mail.To.Add(new MailAddress(sendingTo)); mail.From = new MailAddress(details.From); mail.Subject = subject; mail.Body = content; mail.IsBodyHtml = true; client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(details.Username, details.Password); if (Constants.SendEmails) { client.Send(mail); } else { Logger.WriteLine("SENDING MASS EMAIS IS SET TO FALSE"); Logger.SaveToFile(); File.WriteAllText("testEmail.html", content); } }
public MassEmailingActor() { Receive <BranchInfoAggregationActor.BranchInfosToPrint>(message => { var details = EmailDetails.Get(); foreach (var reposAndBranchInfo in message.ReposAndBranchInfos) { #if DEBUG string sendingTo = "*****@*****.**"; #else var committerEmail = reposAndBranchInfo.BranchInfo.CommitterEmail; var sendingTo = details.Exceptions.ContainsKey(committerEmail) ? details.Exceptions[committerEmail] : committerEmail; #endif var subject = GetSubject(reposAndBranchInfo); var content = GetContent(reposAndBranchInfo); try { SendEmail(details, sendingTo, subject, content); } catch (Exception e) { Logger.WriteLine($"Exception happened - {e.Message}"); Logger.SaveToFile(); } } }); }
public static Option <EmailDetails> GetFromFilePath(string filePath) { var emailDetailsFile = File.ReadAllText(filePath); var lines = emailDetailsFile.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); try { var toLine = CheckStartOfString(1, lines[0], "to:"); var fromLine = CheckStartOfString(2, lines[1], "from:"); var hostLine = CheckStartOfString(3, lines[2], "host:"); var usernameLine = CheckStartOfString(4, lines[3], "username:"******"password:"******"exceptions:"); var exceptionsAndReplacements = exceptionsLine.Split(';').ToArray(); if (exceptionsAndReplacements.Length % 2 != 0) { throw new InvalidProgramException("Pairs don't exist for exceptions"); } var oldEmailAddresses = exceptionsAndReplacements.Where(e => Array.FindIndex(exceptionsAndReplacements, a => a == e) % 2 == 0).ToList(); var newEmailAddresses = exceptionsAndReplacements.Where(e => Array.FindIndex(exceptionsAndReplacements, a => a == e) % 2 == 1).ToList(); var exceptions = Enumerable.Range(0, oldEmailAddresses.Count).ToDictionary(i => oldEmailAddresses[i], i => newEmailAddresses[i]); var details = new EmailDetails { To = toLine, From = fromLine, Username = usernameLine, Host = hostLine, Password = passwordLine, Exceptions = exceptions }; return(Option.Some(details)); } catch (EmailDetailsFileIncorrect) {} return(Option.None <EmailDetails>()); }
public EmailingActor(IActorRef lastEmailedFileActor) { _lastEmailedFileActor = lastEmailedFileActor; Receive <PrinterActor.EmailContentToBeSent>(message => { #if DEBUG File.WriteAllText("..\\..\\Email.html", message.Content); #else var content = message.Content; var details = EmailDetails.Get(); foreach (var exception in details.Exceptions) { content = content.Replace(exception.Key, exception.Value); } var mail = new MailMessage(); var client = new SmtpClient(); client.Port = 587; client.Host = details.Host; mail.To.Add(new MailAddress(details.To)); mail.From = new MailAddress(details.From); mail.Subject = EmailSubject; mail.Body = content; mail.IsBodyHtml = true; client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(details.Username, details.Password); if (Constants.SendEmails) { client.Send(mail); } #endif _lastEmailedFileActor.Tell(new EmailedAtTime(DateTime.Now)); }); }
public static Option<EmailDetails> GetFromFilePath(string filePath) { var emailDetailsFile = File.ReadAllText(filePath); var lines = emailDetailsFile.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); try { var toLine = CheckStartOfString(1, lines[0], "to:"); var fromLine = CheckStartOfString(2, lines[1], "from:"); var hostLine = CheckStartOfString(3, lines[2], "host:"); var usernameLine = CheckStartOfString(4, lines[3], "username:"******"password:"******"exceptions:"); var exceptionsAndReplacements = exceptionsLine.Split(';').ToArray(); if (exceptionsAndReplacements.Length % 2 != 0) { throw new InvalidProgramException("Pairs don't exist for exceptions"); } var oldEmailAddresses = exceptionsAndReplacements.Where(e => Array.FindIndex(exceptionsAndReplacements, a => a == e) % 2 == 0).ToList(); var newEmailAddresses = exceptionsAndReplacements.Where(e => Array.FindIndex(exceptionsAndReplacements, a => a == e) % 2 == 1).ToList(); var exceptions = Enumerable.Range(0, oldEmailAddresses.Count).ToDictionary(i => oldEmailAddresses[i], i => newEmailAddresses[i]); var details = new EmailDetails { To = toLine, From = fromLine, Username = usernameLine, Host = hostLine, Password = passwordLine, Exceptions = exceptions }; return Option.Some(details); } catch (EmailDetailsFileIncorrect) {} return Option.None<EmailDetails>(); }