/// <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); }