public static void ApplyRecipients(MailAddressCollection collection, string emails) { string[] list = null; if (string.IsNullOrEmpty(emails)) { return; } list = emails.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string item in list) { // If address is empty then continue. if (string.IsNullOrEmpty(item.Trim())) { continue; } var a = new MailAddress(item.Trim()); // If address is on the list already then continue. var exists = collection.Any(x => string.Compare(x.Address, a.Address, true) == 0 && string.Compare(x.DisplayName, a.DisplayName, true) == 0 ); if (exists) { continue; } collection.Add(a); } }
// FIXME in case of network error will this freeze the app despite of SendAsync? private static void SendMessageByEmail(Message msg) { try { var Emails = new MailAddressCollection(); Emails.Add(GlobalSettings.EmailRecipients); if (Emails == null || !Emails.Any()) { return; } var mail = CredentialManager.GetCredentials(GlobalSettings.MailHostTarget).UserName; var mailPass = CredentialManager.GetCredentials(GlobalSettings.MailHostTarget).Password; var fromMail = new MailAddress(mail, "REGATA Report Service"); using (var smtp = new SmtpClient { Host = "smtp.jinr.ru", Port = 465, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(mail, mailPass) }) { foreach (var toAddress in Emails) { using (var message = new MailMessage(fromMail, toAddress) { Subject = msg.ToString(), Body = string.Join( msg.Text, Environment.NewLine, Environment.NewLine, "===*******TECH INFO*******==", Environment.NewLine, Environment.NewLine, msg.DetailedText ) }) { //var ct = new CancellationTokenSource(TimeSpan.FromSeconds(30)); smtp.Send(message); } } } } catch (Exception ex) { Notify(new Message(Codes.ERR_REP_SEND_MAIL_UNREG) { DetailedText = ex.ToString() }); } } // private static void SendMessageByEmail(Message msg)
private async Task SendHelperAsync(MailAddress from, MailAddressCollection tos, string subject, Dictionary <string, object> replacements = null, MailAddressCollection replyTos = null, MailAddressCollection ccs = null, IEnumerable <Attachment> attachments = null) { if (@from == null) { throw new ArgumentNullException(nameof(@from)); } if (tos == null) { throw new ArgumentNullException(nameof(tos)); } if (subject == null) { throw new ArgumentNullException(nameof(subject)); } if (!Regex.IsMatch(@from.Address, RegexPatterns.Email)) { throw new Exception($"From address '{@from.Address} is not valid e-mail."); } var content = _template.Content; var mail = new MailMessage { From = @from, Subject = subject.ToIsoString(), SubjectEncoding = Encoding.GetEncoding("ISO-8859-2"), }; foreach (var to in tos) { if (!Regex.IsMatch(to.Address, RegexPatterns.Email)) { throw new Exception($"To address '{to.Address} is not valid e-mail."); } mail.To.Add(to); } if (replyTos != null && replyTos.Any()) { foreach (var rto in replyTos) { if (!Regex.IsMatch(rto.Address, RegexPatterns.Email)) { throw new Exception($"Reply to address '{rto.Address} is not valid e-mail."); } mail.ReplyToList.Add(rto); } } if (ccs != null && ccs.Any()) { foreach (var cc in ccs) { if (!Regex.IsMatch(cc.Address, RegexPatterns.Email)) { throw new Exception($"CC address '{cc.Address} is not valid e-mail."); } mail.CC.Add(cc); } } if (replacements != null && replacements.Count > 0) { content = replacements.Aggregate(content, (current, r) => current.Replace("{" + r.Key + "}", r.Value.ToString())); } var htmlView = AlternateView.CreateAlternateViewFromString(content.ToIsoString(), Encoding.GetEncoding("ISO-8859-2"), "text/html"); htmlView.TransferEncoding = TransferEncoding.QuotedPrintable; if (_template.Images != null && _template.Images.Any()) { foreach (var it in _template.Images) { var lr = new LinkedResource(it.OriginalNameWithFullPath, it.OriginalName.ToEncodingType()) { ContentId = it.ImageNameInHtml }; htmlView.LinkedResources.Add(lr); } } mail.AlternateViews.Add(htmlView); if (attachments != null) { var attachmentList = attachments as IList <Attachment> ?? attachments.ToList(); foreach (var attachment in attachmentList) { mail.Attachments.Add(attachment); } } if (Configuration != null) { mail.Headers.Add(Configuration.HeaderValues); } await _smtp.Value.SendMailAsync(mail); }