C#SMTP身份validation失败,但凭据正确无误

这是我的问题:我写了以下程序来测试我是否可以发送电子邮件:

class Program { static void Main(string[] args) { try { Console.WriteLine("Mail To"); MailAddress to = new MailAddress("myemail@gmail.com"); Console.WriteLine("Mail From"); MailAddress from = new MailAddress("me@businessdomain.it"); MailMessage mail = new MailMessage(from, to); Console.WriteLine("Subject"); mail.Subject = "test"; Console.WriteLine("Your Message"); mail.Body = "test"; SmtpClient smtp = new SmtpClient(); smtp.Host = "mail.domain.it"; smtp.Port = 25; smtp.Credentials = new NetworkCredential( "username", "password"); smtp.EnableSsl = false; Console.WriteLine("Sending email..."); smtp.Send(mail); }catch(Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } } } 

凭据是正确的(我使用telnet测试它们,outlook和android中的app k9mail正确工作),如果我把gmail smtp设置,该程序可以工作。 我真的无法理解这个错误的原因。

使用wireshark我发现了正在发生的事情:S:220 server1.business.it SMTP服务器就绪
C:EHLO pc14
S:250 server1.stargatenet.it你好[87.28.219.65] | 250 PIPELINING | 250 SIZE 25000000 | 250 8BITMIME | 250 BINARYMIME | 250 CHUNKING | 250 AUTH LOGIN CRAM-MD5 DIGEST-MD5 | 250好的
C:AUTH登录用户:UsernameBase64
S:334 VXNlcm5hbWU6
C:通过:PasswordBase64
S:334 UGFzc3dvcmQ6

似乎程序在被问到时没有输入凭据。 怎么可能?

这个链接救了我的命: 链接

这里我遇到的问题很好描述:即使用AUTH LOGIN传递用户名,服务器也会再次响应AUTH_LOGIN_Username_Challenge。

仅使用System.Net.Mail发生此问题。 链接建议了可能的解决方案:

1)使用CDOSYS(不通过AUTH登录发送用户名)
2)使用System.Web.Mail(不通过AUTH登录发送用户名)
3)联系SMTP服务器所有者并让他们修复服务器。

不幸的是我不能SMTP服务器所有者,所以我不得不使用System.Web.Mail。 我知道它已被弃用但不幸的是在这样的情况下,没有其他选择IMO。

这是我的工作代码:

 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); 

这个答案帮助我使用System.Web.Mail。