ActiveUp.Pop3Client将结果验证为-ERR协议错误。 14

时间:2014-07-23 23:51:52

标签: c# .net c#-4.0 mailsystem.net

我正在使用MailSystem.NET库发送和接收电子邮件。当用户名中有反斜杠时,除了使用SSL的Pop3Client身份验证外,一切正常。

假设我有以下代码:

var client = new Pop3Client();
var username = @"xxx\admin";
var password = @"passW0rd";
var host = @"abc.def.gh";
var port = 995;

var result = client.ConnectSsl(host, port, true);
Console.WriteLine(result);

result = client.Authenticate(username, password, SaslMechanism.Login);
Console.WriteLine(result);

输出是:

+OK The Microsoft Exchange POP3 service is ready.
Command "auth login" failed : -ERR Protocol error. 14

那么,到底是什么?当我尝试连接和验证时,例如用户名为johnnyboy@gmail.com的谷歌,它的工作原理。但是如果它有反斜杠并且我反对MS Exchange,它就不起作用。

凭据没问题,我使用PegasusMail对它们进行了双重检查。 有人可以解释可能出错的地方吗?

1 个答案:

答案 0 :(得分:1)

好的,答案很简单。

自2003年以来,Exchange不支持过时的SASL机制AUTH LOGIN。必须至少使用AUTH PLAIN。但要做到这一点,必须重新进行整个身份验证。

在AUTH PLAIN之后,一个命令中应该有用户名和密码,其中\ 000 char作为前导和分隔符。因此,结果命令应该是base64编码的字符串,如:

\000username\000password

请参阅Connecting to POP/SMTP Server via Telnet

所以,我做的很简单。我扩展了Pop3Client类并创建了一个没有SaslMechanism的新方法Authenticate(字符串用户名,字符串密码)。

public class Pop3ClientExt : Pop3Client
{
    public string Authenticate(string username, string password)
    {
        var nullchar = '\u0000';
        var auth = nullchar + username + nullchar + password;

        this.Command("auth plain");
        string response = this.Command(System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(auth)));

        return response;
    }
}

现在,对于Microsoft Exchange服务器,我将调用此Authenticate方法而不是旧方法。