//AUTHコマンド後 private String After(String recvStr) { var pass = ""; if (_authType == AuthType.Login) { if (_user == null) { _user = Encoding.ASCII.GetString(Convert.FromBase64String(recvStr)); return("334 UGFzc3dvcmQ6"); } pass = Encoding.ASCII.GetString(Convert.FromBase64String(recvStr)); goto auth; } if (_authType == AuthType.Plain) { recvStr = Encoding.ASCII.GetString(Convert.FromBase64String(recvStr)); string[] tmp = recvStr.Split('\0'); if (tmp.Length == 3) { _user = tmp[1]; pass = tmp[2]; } goto auth; } if (_authType == AuthType.CramMd5) { recvStr = Encoding.ASCII.GetString(Convert.FromBase64String(recvStr)); var tmp = recvStr.Split(new[] { ' ' }, 2); if (tmp.Length == 2) { _user = tmp[0]; pass = _smtpAuthUserList.GetPass(_user); if (pass != null) { var ret = Md5.Hash(pass, _timestamp); if (ret == tmp[1]) { IsFinish = true; return("235 Authentication successful."); } } } } return(null); auth: if (_smtpAuthUserList.Auth(_user, pass)) { //認証OK IsFinish = true; return("235 Authentication successful."); } return(null); }
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); }
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); }