/// <summary>Send bulkmail to all recipients according to settings</summary> /// <returns>Number of emails sent, null.integer if not determinable</returns> /// <remarks>Detailed status report is sent by email to sending user</remarks> public int SendMails() { EnsureNotDisposed(); int recipients = 0; int messagesSent = 0; int errors = 0; try { //send to recipients string body = _body; if (BodyFormat == MailFormat.Html) //Add Base Href for any images inserted in to the email. { var host = PortalAlias.Contains("/") ? PortalAlias.Substring(0, PortalAlias.IndexOf('/')) : PortalAlias; body = "<html><head><base href='http://" + host + "'><title>" + Subject + "</title></head><body>" + body + "</body></html>"; } string subject = Subject; string startedAt = DateTime.Now.ToString(CultureInfo.InvariantCulture); bool replaceTokens = !SuppressTokenReplace && (_tokenReplace.ContainsTokens(Subject) || _tokenReplace.ContainsTokens(_body)); bool individualSubj = false; bool individualBody = false; var mailErrors = new StringBuilder(); var mailRecipients = new StringBuilder(); switch (AddressMethod) { case AddressMethods.Send_TO: case AddressMethods.Send_Relay: //optimization: if (replaceTokens) { individualBody = (_tokenReplace.Cacheability(_body) == CacheLevel.notCacheable); individualSubj = (_tokenReplace.Cacheability(Subject) == CacheLevel.notCacheable); if (!individualBody) { body = _tokenReplace.ReplaceEnvironmentTokens(body); } if (!individualSubj) { subject = _tokenReplace.ReplaceEnvironmentTokens(subject); } } foreach (UserInfo user in Recipients()) { recipients += 1; if (individualBody || individualSubj) { _tokenReplace.User = user; _tokenReplace.AccessingUser = user; if (individualBody) { body = _tokenReplace.ReplaceEnvironmentTokens(_body); } if (individualSubj) { subject = _tokenReplace.ReplaceEnvironmentTokens(Subject); } } string recipient = AddressMethod == AddressMethods.Send_TO ? user.Email : RelayEmailAddress; string mailError = Mail.SendMail(_sendingUser.Email, recipient, "", "", ReplyTo.Email, Priority, subject, BodyFormat, Encoding.UTF8, body, LoadAttachments(), _smtpServer, _smtpAuthenticationMethod, _smtpUsername, _smtpPassword, _smtpEnableSSL); if (!string.IsNullOrEmpty(mailError)) { mailErrors.Append(mailError); mailErrors.AppendLine(); errors += 1; } else { mailRecipients.Append(user.Email); mailRecipients.Append(BodyFormat == MailFormat.Html ? "<br />" : Environment.NewLine); messagesSent += 1; } } break; case AddressMethods.Send_BCC: var distributionList = new StringBuilder(); messagesSent = Null.NullInteger; foreach (UserInfo user in Recipients()) { recipients += 1; distributionList.Append(user.Email + "; "); mailRecipients.Append(user.Email); mailRecipients.Append(BodyFormat == MailFormat.Html ? "<br />" : Environment.NewLine); } if (distributionList.Length > 2) { if (replaceTokens) { //no access to User properties possible! var tr = new TokenReplace(Scope.Configuration); body = tr.ReplaceEnvironmentTokens(_body); subject = tr.ReplaceEnvironmentTokens(Subject); } else { body = _body; subject = Subject; } string mailError = Mail.SendMail(_sendingUser.Email, _sendingUser.Email, "", distributionList.ToString(0, distributionList.Length - 2), ReplyTo.Email, Priority, subject, BodyFormat, Encoding.UTF8, body, LoadAttachments(), _smtpServer, _smtpAuthenticationMethod, _smtpUsername, _smtpPassword, _smtpEnableSSL); if (mailError == string.Empty) { messagesSent = 1; } else { mailErrors.Append(mailError); errors += 1; } } break; } if (mailErrors.Length > 0) { mailRecipients = new StringBuilder(); } SendConfirmationMail(recipients, messagesSent, errors, subject, startedAt, mailErrors.ToString(), mailRecipients.ToString()); } catch (Exception exc) //send mail failure { Logger.Error(exc); Debug.Write(exc.Message); } finally { foreach (var attachment in _attachments) { attachment.Dispose(); } } return(messagesSent); }
public void Send() { try { string endDelimit; //Use either vbCrLF or <br><br> depending on BodyFormat. if (BodyFormat == MailFormat.Html) { endDelimit = "<br>"; } else { endDelimit = "\r\n"; } string strConfirmation; strConfirmation = "Bulk Email Operation Started: " + DateTime.Now.ToString() + endDelimit + endDelimit; // send to recipients string strBody; int intRecipients = 0; int intMessages = 0; string strDistributionList = ""; ListItem objRecipient; switch (SendMethod) { case "TO": foreach (ListItem tempLoopVar_objRecipient in Recipients) { objRecipient = tempLoopVar_objRecipient; if (!String.IsNullOrEmpty(objRecipient.Text)) { intRecipients++; strBody = Heading + objRecipient.Value + "," + endDelimit + endDelimit + Body; Mail.SendMail(Administrator, objRecipient.Text, "", "", Priority, Subject, BodyFormat, Encoding.UTF8, strBody, Attachment, SMTPServer, SMTPAuthentication, SMTPUsername, SMTPPassword); intMessages++; } } break; case "BCC": foreach (ListItem tempLoopVar_objRecipient in Recipients) { objRecipient = tempLoopVar_objRecipient; if (!String.IsNullOrEmpty(objRecipient.Text)) { intRecipients++; strDistributionList += "; " + objRecipient.Text; } } intMessages = 1; strBody = Body; Mail.SendMail(Administrator, "", "", strDistributionList, Priority, Subject, BodyFormat, Encoding.UTF8, strBody, Attachment, SMTPServer, SMTPAuthentication, SMTPUsername, SMTPPassword); break; } // send confirmation strConfirmation += "Email Recipients: " + intRecipients.ToString() + endDelimit; strConfirmation += "Email Messages: " + intMessages.ToString() + endDelimit + endDelimit; strConfirmation += "Bulk Email Operation Completed: " + DateTime.Now.ToString() + endDelimit; Mail.SendMail(Administrator, Administrator, "", "", Priority, "Bulk Email Confirmation", BodyFormat, Encoding.UTF8, strConfirmation, "", SMTPServer, SMTPAuthentication, SMTPUsername, SMTPPassword); } catch (Exception exc) { // send mail failure Debug.Write(exc.Message); } }