Ejemplo n.º 1
0
        /// SMTPメールサーバーへLogin認証でログインします。
        /// <summary>
        /// SMTPメールサーバーへLogin認証でログインします。
        /// </summary>
        /// <returns></returns>
        public Boolean AuthenticateByLogin()
        {
            SmtpCommandResult rs = null;

            if (this.EnsureOpen() == SmtpConnectionState.Connected)
            {
                rs = this.Execute("Auth Login");
                if (rs.StatusCode != SmtpCommandResultCode.WaitingForAuthentication)
                {
                    return(false);
                }
                //ユーザー名送信
                rs = this.Execute(Base64Converter.Encode(Encoding.UTF8.GetBytes(this.UserName)));
                if (rs.StatusCode != SmtpCommandResultCode.WaitingForAuthentication)
                {
                    return(false);
                }
                //パスワード送信
                rs = this.Execute(Base64Converter.Encode(Encoding.UTF8.GetBytes(this.Password)));
                if (rs.StatusCode == SmtpCommandResultCode.AuthenticationSuccessful)
                {
                    this._State = SmtpConnectionState.Authenticated;
                }
            }
            return(this._State == SmtpConnectionState.Authenticated);
        }
Ejemplo n.º 2
0
        private SmtpCommandResult ExecuteEhloAndHelo()
        {
            SmtpCommandResult rs = null;

            //サーバーへメールトランザクションの開始コマンドを送信
            rs = this.ExecuteEhlo();
            if (rs.StatusCode != SmtpCommandResultCode.RequestedMailActionOkay_Completed)
            {
                rs = this.ExecuteHelo();
            }
            return(rs);
        }
Ejemplo n.º 3
0
        /// StartTLSコマンドをサーバーに対して送信し、暗号化された通信を開始します。
        /// <summary>
        /// StartTLSコマンドをサーバーに対して送信し、暗号化された通信を開始します。
        /// </summary>
#if NETFX_CORE
        private Boolean StartTls()
        {
            SmtpCommandResult rs = null;

            if (this.EnsureOpen() == SmtpConnectionState.Connected)
            {
                rs = this.Execute("STARTTLS");
                if (rs.StatusCode != SmtpCommandResultCode.ServiceReady)
                {
                    return(false);
                }

                base.Ssl = true;
                this.EncryptedCommunication = SmtpEncryptedCommunication.Tls;
                var t = this.Socket.UpgradeToSslAsync(SocketProtectionLevel.Ssl, new HostName(this.ServerName)).AsTask();
                t.Wait();
                return(true);
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// SMTPメールサーバーへPlain認証でログインします。
        /// <summary>
        /// SMTPメールサーバーへPlain認証でログインします。
        /// </summary>
        /// <returns></returns>
        public Boolean AuthenticateByPlain()
        {
            SmtpCommandResult rs = null;

            if (this.EnsureOpen() == SmtpConnectionState.Connected)
            {
                rs = this.Execute("Auth Plain");
                if (rs.StatusCode != SmtpCommandResultCode.WaitingForAuthentication)
                {
                    return(false);
                }
                //ユーザー名&パスワードの文字列を送信
                rs = this.Execute(MailParser.ToBase64String(String.Format("{0}\0{0}\0{1}", this.UserName, this.Password)));
                if (rs.StatusCode == SmtpCommandResultCode.AuthenticationSuccessful)
                {
                    this._State = SmtpConnectionState.Authenticated;
                }
            }
            return(this._State == SmtpConnectionState.Authenticated);
        }
Ejemplo n.º 5
0
        /// SMTPメールサーバーへCRAM-MD5認証でログインします。
        /// <summary>
        /// SMTPメールサーバーへCRAM-MD5認証でログインします。
        /// </summary>
        /// <returns></returns>
        public Boolean AuthenticateByCramMD5()
        {
            SmtpCommandResult rs = null;

            if (this.EnsureOpen() == SmtpConnectionState.Connected)
            {
                rs = this.Execute("Auth CRAM-MD5");
                if (rs.StatusCode != SmtpCommandResultCode.WaitingForAuthentication)
                {
                    return(false);
                }
                //ユーザー名+チャレンジ文字列をBase64エンコードした文字列を送信
                String s = SmtpClient.ToCramMd5String(rs.Message, this.UserName, this.Password);
                rs = this.Execute(s);
                if (rs.StatusCode == SmtpCommandResultCode.AuthenticationSuccessful)
                {
                    this._State = SmtpConnectionState.Authenticated;
                }
            }
            return(this._State == SmtpConnectionState.Authenticated);
        }
Ejemplo n.º 6
0
        private Boolean StartTls()
        {
            SmtpCommandResult rs = null;

            if (this.EnsureOpen() == SmtpConnectionState.Connected)
            {
                rs = this.Execute("STARTTLS");
                if (rs.StatusCode != SmtpCommandResultCode.ServiceReady)
                {
                    return(false);
                }

                base.Ssl = true;
                this.EncryptedCommunication = SmtpEncryptedCommunication.Tls;
                SslStream ssl = new SslStream(new NetworkStream(this.Socket), true, this.RemoteCertificateValidationCallback, null);
                ssl.AuthenticateAsClient(this.ServerName);
                this.Stream = ssl;
                return(true);
            }
            return(false);
        }
Ejemplo n.º 7
0
        /// メールを送信し、送信結果となるSendMailListResultを取得します。
        /// <summary>
        /// メールを送信し、送信結果となるSendMailListResultを取得します。
        /// </summary>
        /// <param name="commandList"></param>
        /// <returns></returns>
        public SendMailListResult SendMailList(IEnumerable <SendMailCommand> commandList)
        {
            SmtpCommandResult rs      = null;
            Boolean           HasRcpt = false;

            //接続失敗
            if (this.EnsureOpen() == SmtpConnectionState.Disconnected)
            {
                return(new SendMailListResult(SendMailResultState.Connection));
            }

            //不正な状態でのメソッドの実行
            if (this.State != SmtpConnectionState.Connected &&
                this.State != SmtpConnectionState.Authenticated)
            {
                return(new SendMailListResult(SendMailResultState.InvalidState));
            }
            //認証済みで無い場合
            if (this.State != SmtpConnectionState.Authenticated)
            {
                //サーバーへメールトランザクションの開始コマンドを送信
                rs = this.ExecuteEhloAndHelo();
                if (rs.StatusCode != SmtpCommandResultCode.RequestedMailActionOkay_Completed)
                {
                    return(new SendMailListResult(SendMailResultState.Helo));
                }
                //TLS/SSL通信
                if (this.EncryptedCommunication == SmtpEncryptedCommunication.Tls)
                {
                    if (this.StartTls() == false)
                    {
                        return(new SendMailListResult(SendMailResultState.Tls));
                    }
                    rs = this.ExecuteEhloAndHelo();
                    if (rs.StatusCode != SmtpCommandResultCode.RequestedMailActionOkay_Completed)
                    {
                        return(new SendMailListResult(SendMailResultState.Helo));
                    }
                }
                //ログイン認証が必要とされるかチェック
                if (SmtpClient.NeedAuthenticate(rs.Message) == true)
                {
                    this.Authenticate();
                }
            }

            List <SendMailResult> results = new List <SendMailResult>();

            foreach (var command in commandList)
            {
                //Mail Fromの送信
                if (command.From == null ||
                    command.From.Value == null)
                {
                    rs = this.ExecuteMail("");
                }
                else if (command.From.Value.StartsWith("<") == true)
                {
                    rs = this.ExecuteMail(command.From.Value);
                }
                else
                {
                    rs = this.ExecuteMail("<" + command.From.Value + ">");
                }
                if (rs.StatusCode != SmtpCommandResultCode.RequestedMailActionOkay_Completed)
                {
                    results.Add(new SendMailResult(SendMailResultState.MailFrom, command));
                    continue;
                }
                List <MailAddress> mailAddressList = new List <MailAddress>();
                //Rcpt Toの送信
                StringBuilder sb = new StringBuilder();
                foreach (var m in command.RcptTo)
                {
                    String mailAddress = m.ToString();
                    if (mailAddress.StartsWith("<") == true && mailAddress.EndsWith(">") == true)
                    {
                        rs = this.ExecuteRcpt(mailAddress);
                    }
                    else
                    {
                        rs = this.ExecuteRcpt("<" + mailAddress + ">");
                    }
                    if (rs.StatusCode == SmtpCommandResultCode.RequestedMailActionOkay_Completed)
                    {
                        HasRcpt = true;
                    }
                    else
                    {
                        mailAddressList.Add(m);
                    }
                    sb.AppendLine(rs.Message);
                }
                if (HasRcpt == false)
                {
                    results.Add(new SendMailResult(SendMailResultState.Rcpt, command, sb.ToString(), mailAddressList));
                    continue;
                }
                //Dataの送信
                rs = this.ExecuteData();
                if (rs.StatusCode == SmtpCommandResultCode.StartMailInput)
                {
                    this.Send(command.Data);
                    rs = this.GetResponse();
                    if (rs.StatusCode == SmtpCommandResultCode.RequestedMailActionOkay_Completed)
                    {
                        results.Add(new SendMailResult(SendMailResultState.Success, command, rs.Message, mailAddressList));
                        this.ExecuteRset();
                    }
                    else
                    {
                        results.Add(new SendMailResult(SendMailResultState.Data, command, rs.Message, mailAddressList));
                    }
                }
                else
                {
                    results.Add(new SendMailResult(SendMailResultState.Data, command, rs.Message, mailAddressList));
                }
            }
            rs = this.ExecuteQuit();

            if (results.Exists(el => el.State != SendMailResultState.Success) == true)
            {
                return(new SendMailListResult(SendMailResultState.SendMailData, results));
            }
            return(new SendMailListResult(SendMailResultState.Success, results));
        }
Ejemplo n.º 8
0
        /// SMTPメールサーバーへログインします。
        /// <summary>
        /// SMTPメールサーバーへログインします。
        /// </summary>
        /// <returns></returns>
        public Boolean TryAuthenticate()
        {
            SmtpCommandResult rs = null;

            if (this._Mode == SmtpAuthenticateMode.Auto)
            {
                if (this.EnsureOpen() == SmtpConnectionState.Connected)
                {
                    rs = this.ExecuteEhlo();
                    String s = rs.Message.ToUpper();
                    //SMTP認証に対応している場合
                    if (s.Contains("AUTH") == true)
                    {
                        if (s.Contains("LOGIN") == true)
                        {
                            return(this.AuthenticateByLogin());
                        }
                        if (s.Contains("PLAIN") == true)
                        {
                            return(this.AuthenticateByPlain());
                        }
                        if (s.Contains("CRAM-MD5") == true)
                        {
                            return(this.AuthenticateByCramMD5());
                        }
                    }
                    else
                    {
                        rs = this.ExecuteEhlo();
                        return(rs.StatusCode == SmtpCommandResultCode.ServiceReady);
                    }
                    //TLS認証
                    if (this.EncryptedCommunication == SmtpEncryptedCommunication.Tls)
                    {
                        if (s.Contains("STARTTLS") == false)
                        {
                            throw new MailClientException("TLS is not allowed.");
                        }
                        this.StartTls();
                        rs = this.ExecuteEhlo();
                        return(rs.StatusCode == SmtpCommandResultCode.ServiceReady);
                    }
                }
            }
            else
            {
                switch (this._Mode)
                {
                case SmtpAuthenticateMode.None: return(true);

                case SmtpAuthenticateMode.Plain: return(this.AuthenticateByPlain());

                case SmtpAuthenticateMode.Login: return(this.AuthenticateByLogin());

                case SmtpAuthenticateMode.Cram_MD5: return(this.AuthenticateByCramMD5());

                case SmtpAuthenticateMode.PopBeforeSmtp:
                {
                    Boolean bl = this._Pop3Client.TryAuthenticate();
                    this._Pop3Client.Close();
                    return(bl);
                }
                }
                throw new InvalidOperationException();
            }
            return(false);
        }