Esempio n. 1
0
        public bool Auth(SmtpClientAuthKind kind, String user, String pass)
        {
            //トランザクションでない場合エラー
            if (Status != SmtpClientStatus.Transaction)
            {
                SetLastError("Auth() Status != Transaction");
                return(false);
            }
            //AUTH送信
            var authStr = "";

            switch (kind)
            {
            case SmtpClientAuthKind.Login:
                authStr = "AUTH LOGIN";
                break;

            case SmtpClientAuthKind.Plain:
                authStr = "AUTH PLAIN";
                break;

            case SmtpClientAuthKind.CramMd5:
                authStr = "AUTH CRAM-MD5";
                break;
            }
            if (!SendCmd(authStr))
            {
                return(false);
            }
            //334受信
            if (!RecvStatus(334))
            {
                return(false);
            }
            //ユーザ・パスワード送信
            switch (kind)
            {
            case SmtpClientAuthKind.Plain:
                //ユーザ名送信
                var str = String.Format("{0}\0{1}\0{2}", user, user, pass);
                if (!SendCmd(String.Format(Base64.Encode(str))))
                {
                    return(false);
                }
                break;

            case SmtpClientAuthKind.Login:
                //ユーザ名送信
                if (!SendCmd(String.Format(Base64.Encode(user))))
                {
                    return(false);
                }
                //334受信
                if (!RecvStatus(334))
                {
                    return(false);
                }
                //パスワード送信
                if (!SendCmd(String.Format(Base64.Encode(pass))))
                {
                    return(false);
                }
                break;

            case SmtpClientAuthKind.CramMd5:
                //MD5送信
                var timestamp = _recvStr.Split(' ')[1];
                var s         = string.Format("{0} {1}", user, Md5.Hash(pass, Base64.Decode(timestamp)));
                if (!SendCmd(Base64.Encode(s)))
                {
                    return(false);
                }
                break;
            }
            //235受信
            if (!RecvStatus(235))
            {
                return(false);
            }
            Status = SmtpClientStatus.Transaction;
            return(true);
        }
Esempio n. 2
0
        public string Set(string recvStr)
        {
            if (_finish || _authType == AuthType.Unknown)//認証動作 もしくは 認証モードが未対応
            {
                return(null);
            }
            int response = 0;

            if (recvStr.IndexOf(' ') == 3)
            {
                response = Convert.ToInt32(recvStr.Substring(0, 3));
            }
            if (_authType == AuthType.Plain)
            {
                switch (_mode)
                {
                case 0:
                    _mode++;
                    return("AUTH PLAIN " + Base64.Encode(string.Format("\0{0}\0{1}", _user, _pass)));

                case 1:
                    if (response == 235)
                    {
                        _mode++;
                        _finish = true;
                        return(null);
                    }
                    if (response == 334)
                    {
                        return(Base64.Encode(string.Format("\0{0}\0{1}", _user, _pass)));
                    }
                    break;
                }
            }
            if (_authType == AuthType.Login)
            {
                switch (_mode)
                {
                case 0:
                    _mode++;
                    return("AUTH LOGIN");

                //break;
                case 1:
                    if (response == 334)
                    {
                        _mode++;
                        return(Base64.Encode(_user));
                    }
                    break;

                case 2:
                    if (response == 334)
                    {
                        _mode++;
                        return(Base64.Encode(_pass));
                    }
                    break;

                case 3:
                    if (response == 235)
                    {
                        _mode++;
                        _finish = true;
                        return(null);
                    }
                    break;
                }
            }
            if (_authType == AuthType.CramMd5)
            {
                switch (_mode)
                {
                case 0:
                    _mode++;
                    return("AUTH CRAM-MD5");

                //break;
                case 1:
                    if (response == 334)
                    {
                        _mode++;
                        if (recvStr.Length > 5)
                        {
                            string timestamp = recvStr.Substring(4);    //AUTH CRAM-MD5用のタイムスタンプ
                            string str       = string.Format("{0} {1}", _user, Md5.Hash(_pass, Base64.Decode(timestamp)));
                            return(Base64.Encode(str));
                        }
                    }
                    break;

                case 2:
                    if (response == 235)
                    {
                        _mode++;
                        _finish = true;
                        return(null);
                    }
                    break;
                }
            }

            return(null);
        }