Example #1
0
        /// <inheritdoc />
        public override string SendMail(MailInfo mailInfo, SmtpInfo smtpInfo = null)
        {
            var(host, port, errorMessage) = ParseSmtpServer(ref smtpInfo);
            if (errorMessage != null)
            {
                return(errorMessage);
            }

            using (var mailMessage = CreateMailMessage(mailInfo, smtpInfo))
            {
                try
                {
                    using (var smtpClient = CreateSmtpClient(smtpInfo, host, port))
                    {
                        smtpClient.Send(mailMessage);
                    }

                    return(string.Empty);
                }
                catch (Exception exc)
                {
                    return(HandleException(exc));
                }
            }
        }
Example #2
0
        /// <inheritdoc />
        public override string SendMail(MailInfo mailInfo, SmtpInfo smtpInfo = null)
        {
            try
            {
                var(host, port, errorMessage) = ParseSmtpServer(ref smtpInfo);
                if (errorMessage != null)
                {
                    return(errorMessage);
                }

                var mailMessage = CreateMailMessage(mailInfo, smtpInfo);

                using (var smtpClient = new SmtpClient())
                {
                    smtpClient.Connect(host, port, SecureSocketOptions.Auto);

                    if (smtpInfo.Authentication == "1" && !string.IsNullOrEmpty(smtpInfo.Username) && !string.IsNullOrEmpty(smtpInfo.Password))
                    {
                        smtpClient.Authenticate(smtpInfo.Username, smtpInfo.Password);
                    }

                    smtpClient.Send(mailMessage);
                    smtpClient.Disconnect(true);
                }

                return(string.Empty);
            }
            catch (Exception exc)
            {
                return(HandleException(exc));
            }
        }
Example #3
0
        private static SmtpClient CreateSmtpClient(SmtpInfo smtpInfo, string host, int?port)
        {
            SmtpClient client = null;

            try
            {
                client      = new SmtpClient();
                client.Host = host;
                if (port != null)
                {
                    client.Port = port.Value;
                }

                SetSmtpClientAuthentication(smtpInfo, client);

                client.EnableSsl = smtpInfo.EnableSSL;

                var returnedClient = client;
                client = null;

                return(returnedClient);
            }
            finally
            {
                client?.Dispose();
            }
        }
Example #4
0
        private static (string host, int?port, string errorMessage) ParseSmtpServer(ref SmtpInfo smtpInfo)
        {
            var errorMessage = ValidateSmtpInfo(smtpInfo);

            if (errorMessage != null)
            {
                return(null, null, errorMessage);
            }

            smtpInfo = GetDefaultSmtpInfo(smtpInfo);

            smtpInfo.Server = smtpInfo.Server.Trim();
            if (!SmtpServerRegex.IsMatch(smtpInfo.Server))
            {
                return(null, null, Localize.GetString("SMTPConfigurationProblem"));
            }

            var smtpHostParts = smtpInfo.Server.Split(':');
            var host          = smtpHostParts[0];

            if (smtpHostParts.Length <= 1)
            {
                return(host, null, null);
            }

            // 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(null, null, Localize.GetString("SmtpInvalidPort"));
            }

            return(host, port, null);
        }
Example #5
0
        /// <inheritdoc />
        public override async Task <string> SendMailAsync(MailInfo mailInfo, SmtpInfo smtpInfo = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var(host, port, errorMessage) = ParseSmtpServer(ref smtpInfo);
            if (errorMessage != null)
            {
                return(errorMessage);
            }

            var mailMessage = CreateMailMessage(mailInfo, smtpInfo);

            try
            {
                using (var smtpClient = new SmtpClient())
                {
                    await smtpClient.ConnectAsync(host, port, SecureSocketOptions.Auto, cancellationToken);

                    if (smtpInfo.Authentication == "1" && !string.IsNullOrEmpty(smtpInfo.Username) && !string.IsNullOrEmpty(smtpInfo.Password))
                    {
                        await smtpClient.AuthenticateAsync(smtpInfo.Username, smtpInfo.Password, cancellationToken);
                    }

                    await smtpClient.SendAsync(mailMessage, cancellationToken);

                    await smtpClient.DisconnectAsync(true, cancellationToken);
                }

                return(string.Empty);
            }
            catch (Exception exc)
            {
                return(HandleException(exc));
            }
        }
Example #6
0
        /// <inheritdoc />
        public override async Task <string> SendMailAsync(MailInfo mailInfo, SmtpInfo smtpInfo = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var(host, port, errorMessage) = ParseSmtpServer(ref smtpInfo);
            if (errorMessage != null)
            {
                return(errorMessage);
            }

            using (var mailMessage = CreateMailMessage(mailInfo, smtpInfo))
            {
                try
                {
                    using (var smtpClient = CreateSmtpClient(smtpInfo, host, port))
                    {
                        await smtpClient.SendMailAsync(mailMessage);
                    }

                    return(string.Empty);
                }
                catch (Exception exc)
                {
                    return(HandleException(exc));
                }
            }
        }
Example #7
0
        public static string SendMail(string mailFrom, string mailSender, string mailTo, string cc, string bcc, string replyTo, MailPriority priority, string subject, MailFormat bodyFormat, Encoding bodyEncoding,
                                      string body, ICollection <MailAttachment> attachments, string smtpServer, string smtpAuthentication, string smtpUsername, string smtpPassword, bool smtpEnableSSL)
        {
            var smtpInfo = new SmtpInfo
            {
                Server         = smtpServer,
                Authentication = smtpAuthentication,
                Username       = smtpUsername,
                Password       = smtpPassword,
                EnableSSL      = smtpEnableSSL,
            };

            var mailInfo = new MailInfo
            {
                From         = mailFrom,
                Sender       = mailSender,
                To           = mailTo,
                CC           = cc,
                BCC          = bcc,
                ReplyTo      = replyTo,
                Priority     = priority,
                BodyEncoding = bodyEncoding,
                BodyFormat   = bodyFormat,
                Body         = body,
                Subject      = subject,
                Attachments  = attachments,
            };

            if (PortalSettings.Current != null && UserController.GetUserByEmail(PortalSettings.Current.PortalId, mailFrom) != null)
            {
                mailInfo.FromName = UserController.GetUserByEmail(PortalSettings.Current.PortalId, mailFrom).DisplayName;
            }

            return(MailProvider.Instance().SendMail(mailInfo, smtpInfo));
        }
Example #8
0
        private static string ValidateSmtpInfo(SmtpInfo smtpInfo)
        {
            if (smtpInfo != null && !string.IsNullOrEmpty(smtpInfo.Server))
            {
                return(null);
            }

            if (!string.IsNullOrWhiteSpace(Host.SMTPServer))
            {
                return(null);
            }

            return("SMTP Server not configured");
        }
Example #9
0
        private static (string host, int port, string errorMessage) ParseSmtpServer(ref SmtpInfo smtpInfo)
        {
            var port = 25;

            if (smtpInfo == null || string.IsNullOrEmpty(smtpInfo.Server))
            {
                if (string.IsNullOrWhiteSpace(Host.SMTPServer))
                {
                    return(null, port, "SMTP Server not configured");
                }

                smtpInfo = new SmtpInfo
                {
                    Server         = Host.SMTPServer,
                    Authentication = Host.SMTPAuthentication,
                    Username       = Host.SMTPUsername,
                    Password       = Host.SMTPPassword,
                    EnableSSL      = Host.EnableSMTPSSL,
                };
            }

            if (smtpInfo.Authentication == "2")
            {
                throw new NotSupportedException("NTLM authentication is not supported by MailKit provider");
            }

            smtpInfo.Server = smtpInfo.Server.Trim();
            if (!SmtpServerRegex.IsMatch(smtpInfo.Server))
            {
                return(null, port, Localize.GetString("SMTPConfigurationProblem"));
            }

            var smtpHostParts = smtpInfo.Server.Split(':');
            var host          = smtpHostParts[0];

            if (smtpHostParts.Length <= 1)
            {
                return(host, port, null);
            }

            // 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(host, port, Localize.GetString("SmtpInvalidPort"));
            }

            return(host, port, null);
        }
Example #10
0
        private static SmtpInfo GetDefaultSmtpInfo(SmtpInfo smtpInfo)
        {
            if (smtpInfo != null && !string.IsNullOrEmpty(smtpInfo.Server))
            {
                return(smtpInfo);
            }

            return(new SmtpInfo
            {
                Server = Host.SMTPServer,
                Authentication = Host.SMTPAuthentication,
                Username = Host.SMTPUsername,
                Password = Host.SMTPPassword,
                EnableSSL = Host.EnableSMTPSSL,
            });
        }
Example #11
0
        private static void SetSmtpClientAuthentication(SmtpInfo smtpInfo, SmtpClient smtpClient)
        {
            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;
            }
        }
        /// <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);
            }
        }
Example #13
0
        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);
        }
Example #14
0
        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);
        }
Example #15
0
 public abstract string SendMail(MailInfo mailInfo, SmtpInfo smtpInfo = null);
Example #16
0
 /// <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);
        }