我应该通过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命令发送用户名:



该问题很可能是在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协议(protocol)的系统通信,但他们做错了。因此,我们决定自己进行通信,直到供应商确定其代码为止。幸运的是,它仅用于内部目的,并且沟通是可以预见的。我们正在使用DIY代码进行一些重构和清理。

最佳答案

我也遇到了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);

10-07 21:01
查看更多