Example #1
0
        public EmailMessageSettingsRecord GetEmailMessageSettings()
        {
            // work with Orchard
            var emailMessageSettings = new EmailMessageSettingsRecord
            {
                // defaul value.
                Id = 1,
                Enable = true,
                Address = "CatShitOne",
                DeliveryMethod = "Network",
                EnableSsl = true,
                Host = "smtp.163.com",
                Port = 465,
                RequireCredentials = false,
                UserName = "******",
                Password = "******"
            };

            return emailMessageSettings;
        }
Example #2
0
        private static MailMessage MailMessagePreprocess(string from, string[] to, string subject, string body, bool isBodyHtml = true, EmailMessageSettingsRecord smtpSettings = null, SmtpClient smtpClient = null, string[] cc = null)
        {
            if (smtpSettings != null && smtpClient != null)
            {
                smtpClient.UseDefaultCredentials = !smtpSettings.RequireCredentials;
                if (!smtpClient.UseDefaultCredentials && !String.IsNullOrWhiteSpace(smtpSettings.UserName))
                {
                    smtpClient.Credentials = new NetworkCredential(smtpSettings.UserName, smtpSettings.Password);
                }

                if (!string.IsNullOrEmpty(smtpSettings.Host))
                {
                    smtpClient.Host = smtpSettings.Host;
                }

                smtpClient.Port = smtpSettings.Port;
                smtpClient.EnableSsl = smtpSettings.EnableSsl;
                smtpClient.DeliveryMethod = (SmtpDeliveryMethod)Enum.Parse(typeof(SmtpDeliveryMethod), smtpSettings.DeliveryMethod);
            }

            MailMessage mailMessage = new MailMessage();

            mailMessage.From = new MailAddress(smtpSettings.Address, from);
            foreach (var item in to)
            {
                mailMessage.To.Add(item);
            }

            mailMessage.Subject = subject;
            mailMessage.IsBodyHtml = isBodyHtml;
            mailMessage.Body = body;
            mailMessage.BodyEncoding = Encoding.UTF8;

            if (cc != null)
            {
                foreach (string c in cc)
                {
                    mailMessage.CC.Add(new MailAddress(c));
                }
            }

            return mailMessage;
        }