/// <inheritdoc /> public override string SendMail(MailInfo mailInfo, SmtpInfo smtpInfo = null) { // validate smtp server if (smtpInfo == null || string.IsNullOrEmpty(smtpInfo.Server)) { if (string.IsNullOrWhiteSpace(Host.SMTPServer)) { return("SMTP Server not configured"); } smtpInfo = new SmtpInfo { Server = Host.SMTPServer, Authentication = Host.SMTPAuthentication, Username = Host.SMTPUsername, Password = Host.SMTPPassword, EnableSSL = Host.EnableSMTPSSL, }; } var mailMessage = new MimeMessage(); mailMessage.From.Add(ParseAddressWithDisplayName(displayName: mailInfo.FromName, address: mailInfo.From)); if (!string.IsNullOrEmpty(mailInfo.Sender)) { mailMessage.Sender = MailboxAddress.Parse(mailInfo.Sender); } // translate semi-colon delimiters to commas as ASP.NET 2.0 does not support semi-colons if (!string.IsNullOrEmpty(mailInfo.To)) { mailInfo.To = mailInfo.To.Replace(";", ","); mailMessage.To.AddRange(InternetAddressList.Parse(mailInfo.To)); } if (!string.IsNullOrEmpty(mailInfo.CC)) { mailInfo.CC = mailInfo.CC.Replace(";", ","); mailMessage.Cc.AddRange(InternetAddressList.Parse(mailInfo.CC)); } if (!string.IsNullOrEmpty(mailInfo.BCC)) { mailInfo.BCC = mailInfo.BCC.Replace(";", ","); mailMessage.Bcc.AddRange(InternetAddressList.Parse(mailInfo.BCC)); } if (!string.IsNullOrEmpty(mailInfo.ReplyTo)) { mailInfo.ReplyTo = mailInfo.ReplyTo.Replace(";", ","); mailMessage.ReplyTo.AddRange(InternetAddressList.Parse(mailInfo.ReplyTo)); } mailMessage.Priority = (MessagePriority)mailInfo.Priority; // Only modify senderAddress if smtpAuthentication is enabled // Can be "0", empty or Null - anonymous, "1" - basic, "2" - NTLM. if (smtpInfo.Authentication == "1" || smtpInfo.Authentication == "2") { // if the senderAddress is the email address of the Host then switch it smtpUsername if different // if display name of senderAddress is empty, then use Host.HostTitle for it if (mailMessage.Sender != null) { var senderAddress = mailInfo.Sender; var senderDisplayName = mailInfo.FromName; var needUpdateSender = false; if (smtpInfo.Username.Contains("@") && senderAddress == Host.HostEmail && !senderAddress.Equals(smtpInfo.Username, StringComparison.InvariantCultureIgnoreCase)) { senderAddress = smtpInfo.Username; needUpdateSender = true; } if (string.IsNullOrEmpty(senderDisplayName)) { senderDisplayName = Host.SMTPPortalEnabled ? PortalSettings.Current.PortalName : Host.HostTitle; needUpdateSender = true; } if (needUpdateSender) { mailMessage.Sender = ParseAddressWithDisplayName(displayName: senderDisplayName, address: senderAddress); } } else if (smtpInfo.Username.Contains("@")) { mailMessage.Sender = ParseAddressWithDisplayName( displayName: Host.SMTPPortalEnabled ? PortalSettings.Current.PortalName : Host.HostTitle, address: smtpInfo.Username); } } var builder = new BodyBuilder { TextBody = Mail.ConvertToText(mailInfo.Body), }; if (mailInfo.BodyFormat == MailFormat.Html) { builder.HtmlBody = mailInfo.Body; } // attachments if (mailInfo.Attachments != null) { foreach (var attachment in mailInfo.Attachments.Where(attachment => attachment.Content != null)) { builder.Attachments.Add(attachment.Filename, attachment.Content, ContentType.Parse(attachment.ContentType)); } } // message mailMessage.Subject = HtmlUtils.StripWhiteSpace(mailInfo.Subject, true); mailMessage.Body = builder.ToMessageBody(); smtpInfo.Server = smtpInfo.Server.Trim(); if (!SmtpServerRegex.IsMatch(smtpInfo.Server)) { return(Localize.GetString("SMTPConfigurationProblem")); } try { var smtpHostParts = smtpInfo.Server.Split(':'); var host = smtpHostParts[0]; var port = 25; if (smtpHostParts.Length > 1) { // port is guaranteed to be of max 5 digits numeric by the RegEx check port = int.Parse(smtpHostParts[1]); if (port < 1 || port > 65535) { return(Localize.GetString("SmtpInvalidPort")); } } // to workaround problem in 4.0 need to specify host name using (var smtpClient = new SmtpClient()) { smtpClient.Connect(host, port, SecureSocketOptions.Auto); switch (smtpInfo.Authentication) { case "": case "0": // anonymous break; case "1": // basic if (!string.IsNullOrEmpty(smtpInfo.Username) && !string.IsNullOrEmpty(smtpInfo.Password)) { smtpClient.Authenticate(smtpInfo.Username, smtpInfo.Password); } break; case "2": // NTLM (Not Supported by MailKit) throw new NotSupportedException("NTLM authentication is not supported by MailKit provider"); } smtpClient.Send(mailMessage); smtpClient.Disconnect(true); } return(string.Empty); } catch (Exception exc) { var retValue = Localize.GetString("SMTPConfigurationProblem") + " "; // mail configuration problem if (exc.InnerException != null) { retValue += string.Concat(exc.Message, Environment.NewLine, exc.InnerException.Message); Exceptions.Exceptions.LogException(exc.InnerException); } else { retValue += exc.Message; Exceptions.Exceptions.LogException(exc); } return(retValue); } }
/// <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); }
protected virtual void generateOrderConfirmation() { Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - Start generateOrderConfirmation", " ", "", "", "", "", "", ""); StringBuilder emailText = new StringBuilder(); string textLine = ""; string storeEmail = ""; string customerEmail = ""; StoreInfo storeInfo = CheckoutControl.StoreData; IAddressInfo billingAddress = CheckoutControl.BillingAddress; IAddressInfo shippingAddress = CheckoutControl.ShippingAddress; //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 1", " ", "", "", "", "", "", ""); OrderInfo orderInfo = CheckoutControl.OrderInfo; if (DotNetNuke.Common.Utilities.Null.IsNull(shippingAddress.Address1) || shippingAddress.Address1.Length == 0) { // canandean changed: load the address from the order if the address controls are empty if (DotNetNuke.Common.Utilities.Null.IsNull(billingAddress.Address1) || billingAddress.Address1.Length == 0) { AddressController controller = new AddressController(); billingAddress = controller.GetAddress(orderInfo.BillingAddressID); shippingAddress = controller.GetAddress(orderInfo.ShippingAddressID); } shippingAddress = billingAddress; } //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 2", " ", "", "", "", "", "", ""); if (storeInfo == null) { StoreController storeController = new StoreController(); storeInfo = storeController.GetStoreInfo(PortalId); } //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 3", " ", "", "", "", "", "", ""); NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone(); if (storeInfo.CurrencySymbol != string.Empty) { LocalFormat.CurrencySymbol = storeInfo.CurrencySymbol; } //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 4", " ", "", "", "", "", "", ""); UserController userController = new UserController(); UserInfo userInfo = userController.GetUser(PortalId, orderInfo.CustomerID); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 5", " ", "", "", "", "", "", ""); if (storeInfo != null && orderInfo != null && userInfo != null) { //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 6", " ", "", "", "", "", "", ""); storeEmail = storeInfo.DefaultEmailAddress; customerEmail = userInfo.Membership.Email; OrderController orderController = new OrderController(); ArrayList orderDetails = orderController.GetOrderDetails(orderInfo.OrderID); TabController tabControler = new TabController(); TabInfo tabInfo = tabControler.GetTab(storeInfo.ShoppingCartPageID, storeInfo.PortalID, true); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7", " ", "", "", "", "", "", ""); //Order email header String _Message = Services.Localization.Localization.GetString("OrderEmailHeader", this.LocalResourceFile); textLine = String.Format(_Message, PortalSettings.PortalName, tabInfo.TabName, storeEmail); emailText.Append(textLine + "\r\n\r\n"); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.1", " ", "", "", "", "", "", ""); //Order number and date _Message = Services.Localization.Localization.GetString("OrderNumber", this.LocalResourceFile); emailText.Append(_Message + " " + orderInfo.OrderID.ToString()); emailText.Append("\r\n"); _Message = Services.Localization.Localization.GetString("OrderDate", this.LocalResourceFile); String _DateFormat = Services.Localization.Localization.GetString("OrderDateFormat", this.LocalResourceFile); emailText.Append(_Message + " " + orderInfo.OrderDate.ToString(_DateFormat)); emailText.Append("\r\n"); emailText.Append("\r\n"); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.2", " ", "", "", "", "", "", ""); //Order Contents _Message = Services.Localization.Localization.GetString("OrderContents", this.LocalResourceFile); emailText.Append(_Message); emailText.Append("\r\n"); _Message = Services.Localization.Localization.GetString("OrderItems", this.LocalResourceFile); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.2.1", " ", "", "", "", "", "", ""); foreach (OrderDetailsInfo item in orderDetails) { //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.2.2 " + item.ModelName, " ", "", "", "", "", "", ""); //textLine = String.Format(_Message, item.Quantity, item.ModelName, item.UnitCost.ToString("C", LocalFormat)); textLine = String.Format(_Message, item.Quantity, item.ModelName, item.ProdCost.ToString("C", LocalFormat)); emailText.Append(textLine + "\r\n"); } emailText.Append("\r\n"); _Message = Services.Localization.Localization.GetString("OrderSubTotal", this.LocalResourceFile); emailText.Append(String.Format(_Message, orderInfo.OrderTotal.ToString("C", LocalFormat))); emailText.Append("\r\n"); _Message = Services.Localization.Localization.GetString("OrderShipping", this.LocalResourceFile); emailText.Append(String.Format(_Message, orderInfo.ShippingCost.ToString("C", LocalFormat))); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.2.3", " ", "", "", "", "", "", ""); if (orderInfo.Tax > 0) { emailText.Append("\r\n"); _Message = Services.Localization.Localization.GetString("OrderTax", this.LocalResourceFile); emailText.Append(String.Format(_Message, orderInfo.Tax.ToString("C", LocalFormat))); } emailText.Append("\r\n"); _Message = Services.Localization.Localization.GetString("OrderTotal", this.LocalResourceFile); emailText.Append(String.Format(_Message, orderInfo.GrandTotal.ToString("C", LocalFormat))); emailText.Append("\r\n"); emailText.Append("\r\n"); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.3", " ", "", "", "", "", "", ""); // canadean changed: add information about company and VAT emailText.Append("Company: " + userInfo.Profile.ProfileProperties["Company"].PropertyValue); emailText.Append("\r\n"); emailText.Append("VAT N.: " + userInfo.Profile.ProfileProperties["VATNo"].PropertyValue); emailText.Append("\r\n"); emailText.Append("\r\n"); //Billing Address _Message = Services.Localization.Localization.GetString("OrderBillingAddress", this.LocalResourceFile); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.3.1", " ", "", "", "", "", "", ""); emailText.Append(_Message); emailText.Append("\r\n"); emailText.Append(billingAddress.Name); emailText.Append("\r\n"); emailText.Append(billingAddress.Address1); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.3.2", " ", "", "", "", "", "", ""); if (billingAddress.Address2.Length > 0) { emailText.Append("\r\n"); emailText.Append(billingAddress.Address2); } //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.4", " ", "", "", "", "", "", ""); emailText.Append("\r\n"); emailText.Append(billingAddress.City); emailText.Append("\r\n"); emailText.Append(billingAddress.RegionCode); emailText.Append("\r\n"); emailText.Append(billingAddress.PostalCode); emailText.Append("\r\n"); emailText.Append(billingAddress.CountryCode); emailText.Append("\r\n"); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.5", " ", "", "", "", "", "", ""); //Shipping Address emailText.Append("\r\n"); _Message = Services.Localization.Localization.GetString("OrderShippingAddress", this.LocalResourceFile); emailText.Append(_Message); emailText.Append("\r\n"); emailText.Append(shippingAddress.Name); emailText.Append("\r\n"); emailText.Append(shippingAddress.Address1); emailText.Append("\r\n"); if (shippingAddress.Address2.Length > 0) { emailText.Append(shippingAddress.Address2); emailText.Append("\r\n"); } emailText.Append(shippingAddress.City); emailText.Append("\r\n"); emailText.Append(shippingAddress.RegionCode); emailText.Append("\r\n"); emailText.Append(shippingAddress.PostalCode); emailText.Append("\r\n"); emailText.Append(shippingAddress.CountryCode); emailText.Append("\r\n"); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.6", " ", "", "", "", "", "", ""); //Email body footer emailText.Append("\r\n"); _Message = Services.Localization.Localization.GetString("OrderTermsOfUse", this.LocalResourceFile); emailText.Append(_Message); emailText.Append("\r\n"); emailText.Append("\r\n"); _Message = Services.Localization.Localization.GetString("OrderCannotBeProcessed", this.LocalResourceFile); emailText.Append(_Message); emailText.Append("\r\n"); emailText.Append("\r\n"); _Message = Services.Localization.Localization.GetString("OrderThanks", this.LocalResourceFile); emailText.Append(_Message); emailText.Append("\r\n"); emailText.Append("\r\n"); emailText.Append("http://" + PortalSettings.PortalAlias.HTTPAlias); emailText.Append("\r\n"); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.7", " ", "", "", "", "", "", ""); // send email SmtpClient smtpClient = new SmtpClient((string)DotNetNuke.Common.Globals.HostSettings["SMTPServer"]); DotNetNuke.Services.Mail.Mail mail = new DotNetNuke.Services.Mail.Mail(); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.8", " ", "", "", "", "", "", ""); System.Net.NetworkCredential networkCredential = new System.Net.NetworkCredential((string)DotNetNuke.Common.Globals.HostSettings["SMTPUsername"], (string)DotNetNuke.Common.Globals.HostSettings["SMTPPassword"]); smtpClient.UseDefaultCredentials = false; smtpClient.Port = 25; smtpClient.EnableSsl = false; smtpClient.Credentials = networkCredential; //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 7.9", " ", "", "", "", "", "", ""); MailMessage message = new MailMessage(); try { //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 8", " ", "", "", "", "", "", ""); MailAddress fromAddress = new MailAddress(storeEmail); //From address will be given as a MailAddress Object message.From = fromAddress; // To address collection of MailAddress message.To.Add(customerEmail); _Message = Services.Localization.Localization.GetString("OrderSubject", this.LocalResourceFile); message.Subject = String.Format(_Message, storeInfo.Name); //Body can be Html or text format //Specify true if it is html message message.IsBodyHtml = false; message.BodyEncoding = Encoding.UTF8; // Message body content message.Body = emailText.ToString(); // Send SMTP mail smtpClient.Send(message); _Message = Services.Localization.Localization.GetString("OrderSubjectToAdmin", this.LocalResourceFile); message.Subject = String.Format(_Message, orderInfo.OrderID); message.To.Clear(); message.To.Add(storeEmail); message.Priority = System.Net.Mail.MailPriority.High; smtpClient.Send(message); //Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 9", " ", "", "", "", "", "", ""); } catch (Exception ex) { Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation exception" + ex.Message + " " + ex.StackTrace, " ", "", "", "", "", "", ""); } Mail.SendMail("*****@*****.**", "*****@*****.**", "", "Canadean Payment Processing - generateOrderConfirmation 10 " + orderInfo.OrderID , " ", "", "", "", "", "", ""); } }