예제 #1
0
        /// <summary>
        /// SendMail
        /// </summary>
        /// <param name="smtpServer">The SMTP server</param>
        public SendMail(SmtpServer smtpServer)
        {
            if (String.IsNullOrEmpty(smtpServer.Host))
            {
                throw new ArgumentException("smtpServer.Host");
            }

            smtpClient = new SmtpClient(smtpServer.Host, smtpServer.Port);
            try
            {
                smtpClient.UseDefaultCredentials = !smtpServer.RequiresAuthentication;
                smtpClient.Credentials           = smtpServer.RequiresAuthentication ?
                                                   new NetworkCredential(smtpServer.Username, smtpServer.Password) :
                                                   CredentialCache.DefaultNetworkCredentials;
            }
            catch
            {
                smtpClient.UseDefaultCredentials = true;
                smtpClient.Credentials           = CredentialCache.DefaultNetworkCredentials;
            }
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl      = smtpServer.Ssl;
            smtpClient.SendCompleted += SendCompletedCallback;
            ForceSmtpAuthentication   = true;
            SmtpAuthentication        = smtpServer.SmtpAuthentication;
            Arguments = null;
        }
예제 #2
0
        public SmtpServer(string host, int port = 25, bool ssl = false, string username = null, string password = null,
                          SmtpAuthentication smtpAuthentication = SmtpAuthentication.Digest)
        {
            Host     = host;
            Port     = port;
            Ssl      = ssl;
            Username = username;
            Password = password;

            RequiresAuthentication = (username != null) || (password != null);
            SmtpAuthentication     = smtpAuthentication;
        }
예제 #3
0
        /// <summary>
        /// SendMail
        /// </summary>
        /// <param name="base64">Base64 encoder</param>
        /// <param name="smtpServer">The SMTP server</param>
        public SendMail(IBase64 base64, SmtpServer smtpServer)
        {
            this.base64 = base64;
            CheckParameter(smtpServer.Host, String.Concat(nameof(smtpServer), ".", nameof(smtpServer.Host)));
            smtpClient = new SmtpClient(smtpServer.Host, smtpServer.Port)
            {
                DeliveryMethod = SmtpDeliveryMethod.Network,
                EnableSsl      = smtpServer.Ssl
            };
            smtpClient.SendCompleted += SendCompletedCallback;

            smtpClient.UseDefaultCredentials = !smtpServer.RequiresAuthentication;
            smtpClient.Credentials           = smtpServer.RequiresAuthentication
                ? new NetworkCredential(smtpServer.Username, smtpServer.Password)
                : CredentialCache.DefaultNetworkCredentials;

            ForceSmtpAuthentication = true;
            SmtpAuthentication      = SmtpAuthentication.Digest;
            Arguments = null;
        }
        public static void ForceSmtpAuthentication(this SmtpClient smtpClient, SmtpAuthentication smtpAuthentication)
        {
            var transport = smtpClient.GetType().GetField("transport", BindingFlags.NonPublic | BindingFlags.Instance);

            if (transport != null)
            {
                var authenticationModules = transport.GetValue(smtpClient).GetType().GetField("authenticationModules", BindingFlags.NonPublic | BindingFlags.Instance);
                if (authenticationModules != null)
                {
                    var modulesArray = authenticationModules.GetValue(transport.GetValue(smtpClient)) as Array;
                    if (modulesArray != null)
                    {
                        var smtpAuthenticationModule = modulesArray.GetValue((byte)smtpAuthentication);
                        modulesArray.SetValue(smtpAuthenticationModule, 0);
                        modulesArray.SetValue(smtpAuthenticationModule, 1);
                        modulesArray.SetValue(smtpAuthenticationModule, 2);
                        modulesArray.SetValue(smtpAuthenticationModule, 3);
                    }
                }
            }
        }