public static string SendEmail(string fromAddress, string senderAddress, string toAddress, string subject, string body, ICollection <MailAttachment> attachments) { var mailInfo = new MailInfo { From = fromAddress, Sender = senderAddress, To = toAddress, Subject = subject, Body = body, Priority = MailPriority.Normal, BodyFormat = HtmlUtils.IsHtml(body) ? MailFormat.Html : MailFormat.Text, BodyEncoding = Encoding.UTF8, Attachments = attachments, }; return(MailProvider.Instance().SendMail(mailInfo)); }
public static void SendEmail(string fromAddress, string senderAddress, string toAddress, string subject, string body) { if (string.IsNullOrWhiteSpace(Host.SMTPServer) || string.IsNullOrEmpty(fromAddress) || string.IsNullOrEmpty(senderAddress) || string.IsNullOrEmpty(toAddress)) { return; } var mailInfo = new MailInfo { From = fromAddress, Sender = senderAddress, To = toAddress, Subject = subject, Body = body, Priority = MailPriority.Normal, BodyFormat = HtmlUtils.IsHtml(body) ? MailFormat.Html : MailFormat.Text, BodyEncoding = Encoding.UTF8, }; MailProvider.Instance().SendMail(mailInfo); }
private static MimeMessage CreateMailMessage(MailInfo mailInfo, SmtpInfo smtpInfo) { 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 = ToMessagePriority(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(); return(mailMessage); }
public abstract string SendMail(MailInfo mailInfo, SmtpInfo smtpInfo = null);
/// <summary>Sends an email.</summary> /// <param name="mailInfo">Information about the message to send.</param> /// <param name="smtpInfo">Information about the SMTP server via which to send the message.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns><see cref="string.Empty"/> if the message send successfully, otherwise an error message.</returns> public virtual async Task <string> SendMailAsync(MailInfo mailInfo, SmtpInfo smtpInfo = null, CancellationToken cancellationToken = default(CancellationToken)) { return(this.SendMail(mailInfo, smtpInfo)); }
/// <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, }; } // 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(";", ","); } if (!string.IsNullOrEmpty(mailInfo.CC)) { mailInfo.CC = mailInfo.CC.Replace(";", ","); } if (!string.IsNullOrEmpty(mailInfo.BCC)) { mailInfo.BCC = mailInfo.BCC.Replace(";", ","); } var retValue = string.Empty; var mailMessage = new MailMessage(mailInfo.From, mailInfo.To); if (!string.IsNullOrEmpty(mailInfo.Sender)) { mailMessage.Sender = new MailAddress(mailInfo.Sender); } mailMessage.Priority = (System.Net.Mail.MailPriority)mailInfo.Priority; mailMessage.IsBodyHtml = mailInfo.BodyFormat == MailFormat.Html; // 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 = new MailAddress(senderAddress, senderDisplayName); } } else if (smtpInfo.Username.Contains("@")) { mailMessage.Sender = new MailAddress(smtpInfo.Username, Host.SMTPPortalEnabled ? PortalSettings.Current.PortalName : Host.HostTitle); } } // attachments if (mailInfo.Attachments != null) { foreach (var attachment in mailInfo.Attachments.Where(attachment => attachment.Content != null)) { mailMessage.Attachments.Add( new Attachment( new MemoryStream(attachment.Content, writable: false), attachment.Filename, attachment.ContentType)); } } // message mailMessage.Subject = HtmlUtils.StripWhiteSpace(mailInfo.Subject, true); mailMessage.Body = mailInfo.Body; smtpInfo.Server = smtpInfo.Server.Trim(); if (SmtpServerRegex.IsMatch(smtpInfo.Server)) { try { // to workaround problem in 4.0 need to specify host name using (var smtpClient = new SmtpClient()) { var smtpHostParts = smtpInfo.Server.Split(':'); smtpClient.Host = smtpHostParts[0]; if (smtpHostParts.Length > 1) { // port is guaranteed to be of max 5 digits numeric by the RegEx check var port = int.Parse(smtpHostParts[1]); if (port < 1 || port > 65535) { return(Localize.GetString("SmtpInvalidPort")); } smtpClient.Port = port; } switch (smtpInfo.Authentication) { case "": case "0": // anonymous break; case "1": // basic if (!string.IsNullOrEmpty(smtpInfo.Username) && !string.IsNullOrEmpty(smtpInfo.Password)) { smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential( smtpInfo.Username, smtpInfo.Password); } break; case "2": // NTLM smtpClient.UseDefaultCredentials = true; break; } smtpClient.EnableSsl = smtpInfo.EnableSSL; smtpClient.Send(mailMessage); smtpClient.Dispose(); } } catch (Exception exc) { 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); } } finally { mailMessage.Dispose(); } } else { retValue = Localize.GetString("SMTPConfigurationProblem"); } return(retValue); }
private static MailMessage CreateMailMessage(MailInfo mailInfo, SmtpInfo smtpInfo) { // 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(";", ","); } var mailMessage = new MailMessage(mailInfo.From, mailInfo.To); if (!string.IsNullOrEmpty(mailInfo.Sender)) { mailMessage.Sender = new MailAddress(mailInfo.Sender); } if (!string.IsNullOrEmpty(mailInfo.CC)) { mailInfo.CC = mailInfo.CC.Replace(";", ","); mailMessage.CC.Add(mailInfo.CC); } if (!string.IsNullOrEmpty(mailInfo.BCC)) { mailInfo.BCC = mailInfo.BCC.Replace(";", ","); mailMessage.Bcc.Add(mailInfo.BCC); } if (!string.IsNullOrEmpty(mailInfo.ReplyTo)) { mailInfo.ReplyTo = mailInfo.ReplyTo.Replace(";", ","); mailMessage.ReplyToList.Add(mailInfo.ReplyTo); } mailMessage.Priority = (System.Net.Mail.MailPriority)mailInfo.Priority; mailMessage.IsBodyHtml = mailInfo.BodyFormat == MailFormat.Html; // 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 = new MailAddress(senderAddress, senderDisplayName); } } else if (smtpInfo.Username.Contains("@")) { mailMessage.Sender = new MailAddress( smtpInfo.Username, Host.SMTPPortalEnabled ? PortalSettings.Current.PortalName : Host.HostTitle); } } // attachments if (mailInfo.Attachments != null) { foreach (var attachment in mailInfo.Attachments.Where(attachment => attachment.Content != null)) { mailMessage.Attachments.Add( new Attachment( new MemoryStream(attachment.Content, writable: false), attachment.Filename, attachment.ContentType)); } } // message mailMessage.SubjectEncoding = mailInfo.BodyEncoding; mailMessage.Subject = HtmlUtils.StripWhiteSpace(mailInfo.Subject, true); mailMessage.BodyEncoding = mailInfo.BodyEncoding; mailMessage.Body = mailInfo.Body; AddAlternateView(mailMessage, mailInfo.Body, mailInfo.BodyEncoding); return(mailMessage); }
/// <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); } }