本文介绍了C#SMTP验证失败,但证书是正确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题:我写了下面的磨片程序来测试,如果我可以发送电子邮件:

Here is my problem: I wrote whe following program to test if I can send email:

class Program
{
    static void Main(string[] args)
    {
        try {
            Console.WriteLine("Mail To");
            MailAddress to = new MailAddress("[email protected]");

            Console.WriteLine("Mail From");
            MailAddress from = new MailAddress("[email protected]");

            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,前景​​及Android的应用程序k9mail正常工作,对它们进行测试),程序工作,如果我把Gmail的SMTP设置。
我实在无法理解这个错误的原因。

The credentials are correct (I tested them using telnet, outlook and the app k9mail in android correctly work), the program works if I put gmail smtp setting.I really cannot understand the cause of this error.

使用Wireshark的我已经发现了正在发生的事情:
小号:220 server1.business.it SMTP服务器准备好结果
C:EHLO PC14结果
小号:250 server1.stargatenet.it你好[87.28.219.65] | 250流水线| 250 SIZE 25000000 | 250 8BITMIME | 250 BINARYMIME | 250 CHUNKING | 250 AUTH LOGIN CRAM-MD5 DIGEST-MD5 | 250 OK结果
C:AUTH登陆用户:UsernameBase64结果
小号:334 VXNlcm5hbWU6结果
C:通:PasswordBase64结果
小号:334 UGFzc3dvcmQ6

Using wireshark I have discovered what is happening:S: 220 server1.business.it SMTP Server ready
C: EHLO pc14
S: 250 server1.stargatenet.it Hello [87.28.219.65] | 250 PIPELINING | 250 SIZE 25000000 | 250 8BITMIME | 250 BINARYMIME | 250 CHUNKING | 250 AUTH LOGIN CRAM-MD5 DIGEST-MD5 | 250 Ok
C: AUTH login User: UsernameBase64
S: 334 VXNlcm5hbWU6
C: Pass: PasswordBase64
S: 334 UGFzc3dvcmQ6

好像当问及程序未进入凭据。这怎么可能?

It seems like the program is not entering the credentials when asked. How is that possible?

推荐答案

此链接救过我的命:链接

在这里我经历了很好描述的问题:即使用户名与AUTH LOGIN服务器的响应AUTH_LOGIN_Username_Challenge再次通过

here the problem I experienced is well described: even though the user name is passed with the AUTH LOGIN the server responds with the AUTH_LOGIN_Username_Challenge again.

此问题只发生使用System.Net.Mail。链接TRE提出可能的解决方案:

This problem happens only using System.Net.Mail. The link suggests tre possible solutions:

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

1)Use CDOSYS (Does not send the User Name with the AUTH Login)
2)Use System.Web.Mail (Does not send the User Name with the AUTH Login)
3)Contact the SMTP Server owner and have them fix the server.

不幸的是我不能SMTP服务器的所有者,所以我不得不使用System.Web.Mail。我知道这是德precated但不幸的是在这样的情况下,有没有其他的选择海事组织。

Unfortunately I couldn't the SMTP server owner, so I had to use System.Web.Mail. I know it is deprecated but unfortunately in cases like this there is no other choice IMO.

这是我的工作code:

That's my working code:

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 = "[email protected]";
        msg.Subject = message.Subject;
        msg.BodyFormat = MailFormat.Html;//System.Text.Encoding.UTF8;
        SmtpMail.SmtpServer = smtpServer;
        SmtpMail.Send(msg);

这使用System.Web.Mail帮助了我。

This answer helped me in using System.Web.Mail.

这篇关于C#SMTP验证失败,但证书是正确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 01:47