public void SendEmail(EmailCommunicationDTO email, string token) { var communication = new Communication(); communication.DomainId = 1; if (token == null && email.FromUserId == null) { throw (new InvalidOperationException("Must provide either email.FromUserId or an authentication token.")); } communication.AuthorUserId = email.FromUserId ?? _communicationService.GetUserIdFromContactId(token, email.FromContactId); var sender = _personService.GetPerson(DefaultContactEmailId); var from = new Contact { ContactId = sender.ContactId, EmailAddress = sender.EmailAddress }; communication.FromContact = from; communication.ReplyToContact = from; var receiver = _personService.GetPerson(email.ToContactId); var recipient = new Contact {ContactId = receiver.ContactId, EmailAddress = receiver.EmailAddress}; communication.ToContacts.Add(recipient); var template = _communicationService.GetTemplate(email.TemplateId); communication.TemplateId = email.TemplateId; communication.EmailBody = template.Body; communication.EmailSubject = template.Subject; communication.MergeData = email.MergeData; _communicationService.SendMessage(communication); }
public bool PasswordResetRequest(string username) { int user_ID = 0; int contact_Id = 0; // validate the email on the server side to avoid erroneous or malicious requests try { user_ID = _userService.GetUserIdByUsername(username); contact_Id = _userService.GetContactIdByUserId(user_ID); } catch (Exception ex) { _logger.Error(string.Format("Could not find email {0} for password reset", JsonConvert.SerializeObject(username, Formatting.Indented)), ex); return false; } // create a token -- see http://stackoverflow.com/questions/664673/how-to-implement-password-resets var resetArray = Encoding.UTF8.GetBytes(Guid.NewGuid() + username + System.DateTime.Now); RNGCryptoServiceProvider prov = new RNGCryptoServiceProvider(); prov.GetBytes(resetArray); var resetToken = Encoding.UTF8.GetString(resetArray); string cleanToken = Regex.Replace(resetToken, "[^A-Za-z0-9]", ""); Dictionary<string, object> userUpdateValues = new Dictionary<string, object>(); userUpdateValues["User_ID"] = user_ID; userUpdateValues["PasswordResetToken"] = cleanToken; // swap out for real implementation _userService.UpdateUser(userUpdateValues); string baseURL = _configurationWrapper.GetConfigValue("BaseURL"); string resetLink = (@"https://" + baseURL + "/reset-password?token=" + cleanToken); // add the email here... var emailCommunication = new EmailCommunicationDTO { FromContactId = 7, // church admin contact id FromUserId = 5, // church admin user id ToContactId = contact_Id, TemplateId = 13356, MergeData = new Dictionary<string, object> { { "resetlink", resetLink } } }; try { _emailCommunication.SendEmail(emailCommunication); return true; } catch (Exception ex) { _logger.Error(string.Format("Could not send email {0} for password reset", JsonConvert.SerializeObject(username, Formatting.Indented)), ex); return false; } }
public IHttpActionResult Post(EmailCommunicationDTO email) { return Authorized(token => { try { _emailCommunication.SendEmail(email, token); return Ok(); } catch (Exception ex) { return InternalServerError(ex); } }); }