簡體   English   中英

C# SMTP 身份驗證失敗但憑據正確

[英]C# SMTP authentication failed but credentials are correct

這是我的問題:我編寫了以下程序來測試我是否可以發送電子郵件:

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 中的應用程序 k9mail 測試它們是否正常工作),如果我設置了 gmail smtp 設置,程序就可以工作。 我真的無法理解這個錯誤的原因。

使用wireshark,我發現了正在發生的事情:S: 220 server1.business.it SMTP Server ready
C: EHLO pc14
S: 250 server1.stargatenet.it 你好 [87.28.219.65] | 250 流水線 | 250 尺寸 25000000 | 250 8BITMIME | 第250話第250話 250 AUTH 登錄 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。

雖然我遇到了同樣的問題,但我在這里找到了該問題的解決方案

簡而言之,你必須使用

smtp.UseDefaultCredentials = false;

smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["email.username"].ToString(), 
                                         ConfigurationManager.AppSettings["email.password"].ToString());

如此快速的總結,看起來更像這樣的東西應該可以工作:

        SmtpClient smtp = new SmtpClient();
        smtp.Host = ConfigurationManager.AppSettings["email.smtp"].ToString();
        smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["email.port"]);
        smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["email.username"].ToString(), 
                                                 ConfigurationManager.AppSettings["email.password"].ToString());
        smtp.UseDefaultCredentials = false;
        smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["email.enablessl"]);
        Console.WriteLine("Sending email..." + (i+1));
        smtp.Send(mail);

對於任何有興趣的人。 我今天遇到了這個問題。 並發現我的解決方案是訂單。

SMTPClient client = new SMTPClient();
client.UseDefaultCredentials = false; // first after declare and needs to be false.

然后:

client.Credentials = new System.Net.NetworkCredential(sMTPSettings.SMTPLogin, sMTPSettings.SMTPPassword);

然后剩下的。

希望它可以幫助某人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM