使用不兼容的SMTP服务器进行身份验证

时间:2015-10-22 14:13:05

标签: c# .net smtpclient

我应该通过SMTP服务器发送电子邮件。此SMTP服务器需要身份验证。但是,如果我使用.NET SmtpClient的默认身份验证机制,则身份验证将失败并显示504 Unrecognized Authentication Type

使用PuTTy时,我能够成功进行身份验证:

250 AUTH LOGIN PLAIN
AUTH LOGIN
334 VXNlcm5hbWU6
Some base64 encoded username
334 UGFzc3dvcmQ6
Some base64 encoded password
235 Authentication Successful

SmtpClient使用稍微不同的方法,其中用户名通过AUTH LOGIN命令传递:

250 AUTH LOGIN PLAIN
AUTH LOGIN Some base64 encoded username
504 Unrecognized Authentication
MAIL FROM: ...

似乎忽略了504,因为它正在发送MAIL FROM命令。 读取internet上的一些来源,允许使用AUTH LOGIN命令发送用户名:

  

但是,此行为存在不同的RFC兼容版本,其中客户端最初使用AUTH LOGIN方法发送userid

问题很可能出在SMTP服务器端,但我无法控制它。

是否可以让SmtpClient单独发送命令?

示例代码

using (var client = new SmtpClient(_configuration.Host, _configuration.Port))
{
    client.UseDefaultCredentials = false;
    client.ServicePoint.MaxIdleTime = 1;
    client.EnableSsl = false;
    client.Credentials = new NetworkCredential(_configuration.Username, _configuration.Password, _configuration.Domain);

    var from = new MailAddress(string.Format("{0}@{1}", _configuration.Sender, _configuration.Domain));
    var to = new MailAddress(string.Format("{0}@{1}", to, _configuration.Domain));

    var message = new MailMessage(from, to)
    {
        Body = body
    };

    client.Send(message);
}

DIY

只是为了好玩(而不是制作)我试图做自己的沟通:

using (var connection = new TcpClient(_configuration.Host, _configuration.Port))
using (NetworkStream stream = connection.GetStream())
{
    string header = Read(stream);
    Reply(stream, "EHLO " + Environment.MachineName);

    while (!Read(stream).ToUpperInvariant().Contains("AUTH LOGIN PLAIN"))
    {
    }
    Reply(stream, "AUTH LOGIN");

    string requestUsername = Read(stream);
    Reply(stream, Convert.ToBase64String(Encoding.ASCII.GetBytes(_configuration.Username)));

    string requestPassword = Read(stream);
    Reply(stream, Convert.ToBase64String(Encoding.ASCII.GetBytes(_configuration.Password)));
    string authSucces = Read(stream);

    Reply(stream, string.Format("MAIL FROM: <{0}@{1}>", _configuration.Sender, _configuration.Domain));
    string senderOk = Read(stream);

    Reply(stream, string.Format("RCPT TO: <{0}@{1}>", to, _configuration.Domain));
    string rcptOk = Read(stream);

    Reply(stream, "DATA");
    string sendData = Read(stream);

    Reply(stream, body);
    Reply(stream, string.Empty);
    Reply(stream, ".");
    Reply(stream, string.Empty);

    stream.Close();
    connection.Close();
}

private string Read(NetworkStream stream)
{
    var data = new byte[1024];
    int received = stream.Read(data, 0, data.Length);
    string contents = Encoding.ASCII.GetString(data, 0, received);

    return contents;
}

private void Reply(NetworkStream stream, string reply)
{
    reply = reply + Environment.NewLine;
    stream.Write(Encoding.ASCII.GetBytes(reply), 0, reply.Length);
    stream.Flush();
}

这很有效。所以它肯定是关于SmtpClient和实际的Smtp服务器如何通信。

变通

事实证明,我们一直在与一个单独实施SMTP协议的系统进行通信,但他们做错了。所以我们决定自己进行沟通,直到供应商修改了他们的代码。幸运的是,它仅用于内部目的,并且通信非常可预测。我们正在使用DIY代码进行一些重构和清理。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题Here is my solution/workaround:使用System.Web.Mail而不是System.Net.Mail发送电子邮件。我知道这不是最好的解决方案,因为该库已弃用,但在供应商修复代码之前可能会有所帮助。

System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
    msg.Body = message.Body;

    string smtpServer = "mail.business.it";
    string userName = "username";
    string password = "password";
    int cdoBasic = 1;
    int cdoSendUsingPort = 2;
    if (userName.Length > 0)
    {
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
    }
    msg.To = message.Destination;
    msg.From = "me@domain.it";
    msg.Subject = message.Subject;
    msg.BodyFormat = MailFormat.Html;//System.Text.Encoding.UTF8;
    SmtpMail.SmtpServer = smtpServer;
    SmtpMail.Send(msg);