Ejemplo n.º 1
0
        /// <summary>Adds alternate views to the <paramref name="mailMessage"/>.</summary>
        /// <param name="mailMessage">The message to which the alternate views should be added.</param>
        /// <param name="body">The message body.</param>
        /// <param name="bodyEncoding">The encoding of the message body.</param>
        internal static void AddAlternateView(MailMessage mailMessage, string body, Encoding bodyEncoding)
        {
            // added support for multipart html messages
            // add text part as alternate view
            var plainView = AlternateView.CreateAlternateViewFromString(Mail.ConvertToText(body), bodyEncoding, "text/plain");

            mailMessage.AlternateViews.Add(plainView);
            if (mailMessage.IsBodyHtml)
            {
                var htmlView = AlternateView.CreateAlternateViewFromString(body, bodyEncoding, "text/html");
                mailMessage.AlternateViews.Add(htmlView);
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        /// <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);
            }
        }