예제 #1
0
        public static SmtpSettings Deserialize(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return null;
            }

            var props = value.Split(new[] { '#' }, StringSplitOptions.None);
            props = Array.ConvertAll(props, p => !string.IsNullOrEmpty(p) ? p : null);

            var host = HttpUtility.UrlDecode(props[3]);
            var port = !string.IsNullOrEmpty(props[4]) ? Int32.Parse(props[4]) : DefaultSmtpPort;
            var senderAddress = HttpUtility.UrlDecode(props[5]);
            var senderDisplayName = HttpUtility.UrlDecode(props[6]) ?? DefaultSenderDisplayName;

            var settings = new SmtpSettings(host, port, senderAddress, senderDisplayName)
                {
                    EnableSSL = 7 < props.Length && !string.IsNullOrEmpty(props[7]) && Convert.ToBoolean(props[7])
                };

            var credentialsUserName = HttpUtility.UrlDecode(props[1]);
            var credentialsUserPassword = HttpUtility.UrlDecode(props[2]);
            var credentialsDomain = HttpUtility.UrlDecode(props[0]);
            if (!string.IsNullOrEmpty(credentialsUserName) && !string.IsNullOrEmpty(credentialsUserPassword))
            {
                settings.SetCredentials(credentialsUserName, credentialsUserPassword, credentialsDomain);
            }
            
            return settings;
        }
        private SmtpSettings GetSmtpSettingsFromConfig()
        {
            var smtpClient = new SmtpClient();
            if (!string.IsNullOrEmpty(smtpClient.Host))
            {
                // section /configuration/system.net/mailSettings/smtp not empty.
                var smtpSettings = new SmtpSettings();

                smtpSettings.Host = smtpClient.Host;
                smtpSettings.Port = smtpClient.Port;
                smtpSettings.EnableSSL = 400 < smtpClient.Port;

                var credentials = smtpClient.Credentials as NetworkCredential;
                if (credentials != null)
                {
                    smtpSettings.CredentialsDomain = credentials.Domain;
                    smtpSettings.CredentialsUserName = credentials.UserName;
                    smtpSettings.CredentialsUserPassword = credentials.Password;
                }

                var mailMessage = new MailMessage();
                if (mailMessage.From != null)
                {
                    smtpSettings.SenderAddress = mailMessage.From.Address;
                    smtpSettings.SenderDisplayName = mailMessage.From.DisplayName;
                }

                return smtpSettings;
            }
            else
            {
                return null;
            }
        }
 public ClientConfiguration(ITenantService service)
 {
     this.tenantService = service;
     this.configSmtpSettings = GetSmtpSettingsFromConfig();
 }
 private string Serialize(SmtpSettings smtp)
 {
     if (smtp == null) return null;
     return string.Join("#",
                        new[]
                            {
                                smtp.CredentialsDomain,
                                smtp.CredentialsUserName,
                                smtp.CredentialsUserPassword,
                                smtp.Host,
                                smtp.Port.HasValue ? smtp.Port.Value.ToString() : string.Empty,
                                smtp.SenderAddress,
                                smtp.SenderDisplayName,
                                smtp.EnableSSL.ToString()
                            });
 }