/// <summary> /// Sends an e-mail notification of a password reset to a user. /// </summary> /// <param name="recipient">The user whose password is being reset.</param> /// <param name="password">The user's new password.</param> /// <exception cref="ArgumentNullException"><paramref name="recipient"/> or <paramref name="password"/> is null.</exception> public static void Send(CPUser recipient, SecureString password) { if (recipient == null) { throw new ArgumentNullException("recipient", "Password reset message recipient cannot be null."); } if (password == null) { throw new ArgumentNullException("password", "Password cannot be null."); } using (CPMailMessage message = new CPMailMessage() { Recipient = new MailAddress(recipient.Email, recipient.DisplayName), Subject = CPProviders.SettingsProvider.ApplicationName + @" Username/Password Reset", Body = string.Format(MessageBodyFormat, recipient.DisplayName, CPProviders.SettingsProvider.AgencyName, CPProviders.SettingsProvider.ApplicationName, CPProviders.SettingsProvider.ApplicationUrl, recipient.UserName, password.Decrypt(), CPProviders.SettingsProvider.AgencyPhoneNumber) }) { message.Send(); message.Body.Remove(0); } }
/// <summary> /// Sends the current notification. /// </summary> public void Send() { Candidate candidate = this.LoadCandidate != null?this.LoadCandidate(_message.CandidateID) : null; try { _mail.Body = string.Format(OpenReceiptBodyFormat, candidate != null ? candidate.Name : string.Format("candidate #{0}", _message.CandidateID), _message.CandidateID, _message.UniqueID, _message.ElectionCycle, CfbDirectorySearcher.GetUser(_message.Creator).DisplayName, _message.PostDate, _message.OpenDate, CPSecurity.Provider.GetDisplayName(_message.Opener), _message.Opener, _message.Title); _mail.Send(); } catch { } }
/// <summary> /// Sends the current notification. /// </summary> /// <returns>true if and only if all recipients were successfully notified; otherwise, false.</returns> public bool Send() { Dictionary <string, string> failedRecipients = new Dictionary <string, string>(); // track failed recipients // prepare and send message if (this.Recipients != null) { Settings settings = Properties.Settings.Default; MailAddressCollection recipients = new MailAddressCollection(); // all receipients for logging purposes Candidate candidate = null; if (this.LoadCandidate != null) { try { candidate = this.LoadCandidate(_message.CandidateID); } catch // BUGFIX #47: catch exceptions during candidate load so delivery is not interrupted { candidate = null; } } // retrieve message parameters and prepare body string election = _message.ElectionCycle; string cid = _message.CandidateID; string name = candidate != null ? candidate.Name : string.Format("candidate #{0}", cid); StringBuilder sb = new StringBuilder(); // alternate message formats if (_message.CategoryID == CmoCategory.TollingLetterCategoryID && _message.TollingLetter != null) { if (_message.TollingLetter.IsExtension) { sb.AppendFormat(settings.CmoMessageExtensionTemplate, election, name, cid); } else { sb.AppendFormat(settings.CmoMessageTollingTemplate, election, name, cid, GetMessageType()); } } else if (_message.IsInadequateResponseLetter) { sb.AppendFormat(settings.CmoMessageInadequateTemplate, election, name, cid, GetMessageType()); } else { sb.AppendFormat(settings.CmoMessageTemplate, election, name, cid, GetMessageType()); } sb.AppendLine(); string url = CPProviders.SettingsProvider.ApplicationUrl; sb.AppendFormat(settings.CmoMessageViewLinkTemplate, url, _message.UniqueID); string paragraph = sb.ToString(); // send to each recipient foreach (var user in this.Recipients) { // personalize and send message try { MailAddress recipient = _mail.GetMailAddress(user); recipients.Add(recipient); _mail.Recipient = recipient; _mail.Body = string.Format(settings.MessageBodyTemplate, recipient.DisplayName, paragraph, url); _mail.Send(); } catch (Exception e) { failedRecipients[string.Format("{0} [{1}] ({2})", user.DisplayName, user.UserName, string.IsNullOrEmpty(user.Email) ? "<no e-mail address>" : user.Email)] = e.Message; } } // carbon-copy C-Access for records _mail.To.Clear(); _mail.CC.Clear(); _mail.Bcc.Clear(); _mail.CC.Add(settings.CmoMessageCCEmailAddress); sb = new StringBuilder(); foreach (MailAddress address in recipients) { sb.AppendFormat("{0} ({1}), ", address.DisplayName, address.Address); } string allRecipients = sb.ToString(); if (allRecipients.EndsWith(", ")) { allRecipients = allRecipients.Remove(allRecipients.Length - 2); } // BUGFIX #60: To prevent timeout of long-running email notification operations, failed recipient notification // will be included in the internal carbon-copy, and sending will be executed asynchronously. int bodyStartIndex = settings.MessageBodyTemplate.IndexOf("<body>"); if (bodyStartIndex != -1) { bodyStartIndex += 6; } StringBuilder body = new StringBuilder(settings.MessageBodyTemplate.Substring(0, bodyStartIndex)); if (failedRecipients.Count > 0) { body.Append("<span style=\"color:red\">Unable to successfully e-mail the following C-Access users:</span><ol>"); } foreach (var entry in failedRecipients) { body.AppendFormat("<li>{0}: {1}</li>", entry.Key, entry.Value); } body.Append("</ol>"); body.AppendFormat(settings.MessageBodyTemplate.Substring(bodyStartIndex), allRecipients, paragraph, url); _mail.Body = body.ToString(); _mail.Send(); } // return failed recipients return(failedRecipients.Count == 0); }