public ChangeCredentialsStatus ChangeEmail(IUserBasic userBasic, string newEmail) { if (!ValidationExpressions.IsValidEmail(newEmail)) { return(ChangeCredentialsStatus.EmailNotValid); } ChangeCredentialsStatus changeCredentialsStatus; try { using (IDataStoreContext dataStoreContext = this.DataStore.CreateContext()) changeCredentialsStatus = dataStoreContext.wm_Users_UpdateEmail(userBasic.UserId, newEmail); } catch (Exception ex) { _Log.Error("Error at Users_UpdateUserName", ex); throw new DataStoreException(ex, true); } if (changeCredentialsStatus == ChangeCredentialsStatus.Success) { _Log.InfoFormat("User {0} changed email from {1} to {2}.", userBasic.UserId, userBasic.Email, newEmail); userBasic.Email = newEmail; } return(changeCredentialsStatus); }
internal bool SendEmail(IEmail mail) { using (MailMessage message = new MailMessage()) { foreach (string toAddress in mail.Recipients.Replace(';', ',') .Split(',')) { if (ValidationExpressions.IsValidEmail(toAddress)) { message.To.Add(toAddress); } } if (message.To.Count == 0) { _Log.WarnFormat("No valid email address found at reccipient string {0}", mail.Recipients); return(false); } if (!ValidationExpressions.IsValidEmail(mail.Sender)) { _Log.WarnFormat("Sender {0} is not a valid email address", mail.Sender); return(false); } message.From = new MailAddress(mail.Sender); message.Subject = mail.Subject; message.Body = mail.Body; message.IsBodyHtml = true; using (SmtpClient smtp = new SmtpClient()) { smtp.Host = _ApplicationSettings.SmtpHost; smtp.Port = _ApplicationSettings.SmtpPort; smtp.EnableSsl = _ApplicationSettings.SmtpEnableSsl; smtp.DeliveryMethod = _ApplicationSettings.SmtpDeliveryMethod; smtp.UseDefaultCredentials = _ApplicationSettings.SmtpUseDefaultCredentials; if (!smtp.UseDefaultCredentials) { smtp.Credentials = new NetworkCredential(_ApplicationSettings.SmtpUserName, _ApplicationSettings.SmtpPassword); } bool success = false; try { smtp.Send(message); success = true; } catch (SmtpFailedRecipientsException err) { _Log.Warn(string.Format("The following email addresses failed: {0}", err.FailedRecipient), err); // we update the db so we can resend later if (mail.TotalSendAttempts > _ApplicationSettings.TotalResendAttempts + 1) { InstanceContainer.EmailManager.SetToSent(mail.EmailId, EmailStatus.SendFailed, EmailPriority.CanWait); } else { InstanceContainer.EmailManager.SetToSent(mail.EmailId, EmailStatus.Undelivered, EmailPriority.CanWait); } } catch (SmtpException err) { _Log.Error("Unable to send email. See inner exception for further details", err); // we update the db so we can resend later if (mail.TotalSendAttempts > _ApplicationSettings.TotalResendAttempts + 1) { InstanceContainer.EmailManager.SetToSent(mail.EmailId, EmailStatus.SendFailed, EmailPriority.CanWait); } else { InstanceContainer.EmailManager.SetToSent(mail.EmailId, EmailStatus.Undelivered, EmailPriority.CanWait); } } catch (Exception err) { _Log.Error("Unable to send email. See inner exception for further details", err); } if (success) { // make sure the db gets updated... InstanceContainer.EmailManager.SetToSent(mail.EmailId, EmailStatus.Sent, mail.Priority); _Log.InfoFormat("Successfully sent {0} email to {1}", mail.EmailType.ToString(), mail.Recipients.ToString()); } return(success); } } }