protected void DoActualSend(EmailMessage emailMessage, EmailMessageDeliveryAttempt currentDeliveryAttempt, string emailTestAddress) { IUnitOfWork unitOfWork = DependencyLocator.Current.GetInstance <IUnitOfWorkFactory>().GetUnitOfWork(); unitOfWork.Reattach <EmailMessageDeliveryAttempt>(currentDeliveryAttempt); bool flag = false; MailMessage mailMessage = this.ConvertEmailMessageToMailMessage(emailMessage); if (!emailTestAddress.IsBlank()) { mailMessage.To.Clear(); mailMessage.To.Add(emailTestAddress); } try { SmtpClient smtpClient = new SmtpClient(); smtpClient.Timeout = 90000; smtpClient.Send(mailMessage); flag = true; } catch (SmtpFailedRecipientsException ex) { for (int i = 0; i < ex.InnerExceptions.Length; i++) { SmtpStatusCode status = ex.InnerExceptions[i].StatusCode; if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable || status == SmtpStatusCode.ServiceClosingTransmissionChannel || status == SmtpStatusCode.ServiceNotAvailable || status == SmtpStatusCode.TransactionFailed) { System.Threading.Thread.Sleep(10000); new SmtpClient().Send(mailMessage); } else { currentDeliveryAttempt.ErrorMessage = ex.ToString(); LogHelper.For((object)this).Error((object)("There was a problem sending the email " + (object)emailMessage.Id), ex, (string)null, (object)null); } } } catch (Exception ex) { currentDeliveryAttempt.ErrorMessage = ex.ToString(); LogHelper.For((object)this).Error((object)("There was a problem sending the email " + (object)emailMessage.Id), ex, (string)null, (object)null); } finally { mailMessage.Dispose(); } if (flag) { currentDeliveryAttempt.DeliveredDate = new DateTimeOffset?((DateTimeOffset)DateTimeProvider.Current.Now); } unitOfWork.Save(); }
public void SendEmail(SendEmailParameter parameter, IUnitOfWork unitOfWork) { foreach (string toAddress in (IEnumerable <string>)parameter.ToAddresses) { if (!RegularExpressionLibrary.IsValidEmail(toAddress)) { throw new ArgumentException("The value '{email}' in the ToAddresses collection is not a valid email address".FormatWith((object)new { email = toAddress }, (IFormatProvider)null)); } } if (!RegularExpressionLibrary.IsValidEmail(parameter.FromAddress)) { throw new ArgumentException("The value '{email}' for the FromAddress is not a valid email address".FormatWith((object)new { email = parameter.FromAddress }, (IFormatProvider)null)); } foreach (string replyToAddress in (IEnumerable <string>)parameter.ReplyToAddresses) { if (!RegularExpressionLibrary.IsValidEmail(replyToAddress)) { throw new ArgumentException("The value '{email}' in the ReplyToAddresses collection is not a valid email address".FormatWith((object)new { email = replyToAddress }, (IFormatProvider)null)); } } foreach (string ccAddress in (IEnumerable <string>)parameter.CCAddresses) { if (!RegularExpressionLibrary.IsValidEmail(ccAddress)) { throw new ArgumentException("The value '{email}' in the CCAddresses collection is not a valid email address".FormatWith((object)new { email = ccAddress }, (IFormatProvider)null)); } } foreach (string bccAddress in (IEnumerable <string>)parameter.BccAddresses) { if (!RegularExpressionLibrary.IsValidEmail(bccAddress)) { throw new ArgumentException("The value '{email}' in the BccAddresses collection is not a valid email address".FormatWith((object)new { email = bccAddress }, (IFormatProvider)null)); } } IRepository <EmailMessage> repository = unitOfWork.GetRepository <EmailMessage>(); EmailMessage emailMessage = repository.Create(); emailMessage.Body = parameter.Body; emailMessage.Subject = parameter.Subject; repository.Insert(emailMessage); foreach (string toAddress in (IEnumerable <string>)parameter.ToAddresses) { emailMessage.EmailMessageAddresses.Add(new EmailMessageAddress() { EmailAddress = toAddress, Type = EmailMessageAddressType.To.ToString() }); } emailMessage.EmailMessageAddresses.Add(new EmailMessageAddress() { EmailAddress = parameter.FromAddress, Type = EmailMessageAddressType.From.ToString() }); foreach (string ccAddress in (IEnumerable <string>)parameter.CCAddresses) { emailMessage.EmailMessageAddresses.Add(new EmailMessageAddress() { EmailAddress = ccAddress, Type = EmailMessageAddressType.CC.ToString() }); } foreach (string bccAddress in (IEnumerable <string>)parameter.BccAddresses) { emailMessage.EmailMessageAddresses.Add(new EmailMessageAddress() { EmailAddress = bccAddress, Type = EmailMessageAddressType.BCC.ToString() }); } foreach (string replyToAddress in (IEnumerable <string>)parameter.ReplyToAddresses) { emailMessage.EmailMessageAddresses.Add(new EmailMessageAddress() { EmailAddress = replyToAddress, Type = EmailMessageAddressType.ReplyTo.ToString() }); } EmailMessageDeliveryAttempt emailMessageDeliveryAttempt = new EmailMessageDeliveryAttempt(); emailMessage.EmailMessageDeliveryAttempts.Add(emailMessageDeliveryAttempt); unitOfWork.Save(); string emailTestAddress = EmailsSettings.TestEmail; Task.Factory.StartNew((Action)(() => this.DoActualSend(emailMessage, emailMessageDeliveryAttempt, emailTestAddress))); }