2017-02-22 48 views
0

我嘗試使用C#和Yandex的發送郵件,但我得到一個錯誤:C#Yandex的郵件發送錯誤5.5.4錯誤:發送AUTH命令第一

Error 5.5.4 Error: send AUTH command first

這裏是我的代碼。我嘗試使用不同的端口(587,465 ..)和SMTP主機(smtp.yandex.com.tr,smtp.yandex.com,smtp.yandex.ru ...),但我對所有嘗試都得到相同的錯誤。

SmtpClient sc = new SmtpClient("smtp.yandex.com.tr", 587); 
//sc.Port = 587; 
//sc.Host = "smtp.yandex.com"; 

sc.EnableSsl = false; 
sc.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["Email"].ToString(), 
                System.Configuration.ConfigurationManager.AppSettings["Sifre"].ToString()); 
sc.UseDefaultCredentials = false; 
sc.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 

回答

1

Yandex Mail不支持沒有SSL的連接(根據https://yandex.com/support/mail/mail-clients.xml)。嘗試設置EnableSsl爲true,並使用主機:smtp.yandex.com,端口:465。如果它不會工作 - 嘗試端口25

+2

我有SslEnabled = true並嘗試所有端口,但仍然得到該錯誤:( – noobed

1

您必須調用sc.UseDefaultCredentials = false;

UseDefaultCredentials = false後置sc.Credentials = new System.Net.NetworkCredential()將重置Credentialsnull

嘗試線順序切換到:

sc.UseDefaultCredentials = false; 
sc.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["Email"].ToString(), 
                System.Configuration.ConfigurationManager.AppSettings["Sifre"].ToString()); 

也有效設置爲smtp.yandex.ru:

smtpUserName="" 
    enableSsl="true" 
    smtpPassword="" 
    smtpAuthentication="Basic" 
    smtpServer="smtp.yandex.ru" 
    smtpPort="465" 
0

如果您的密碼錯誤會發生此錯誤。至少對我來說是這種情況。

0

我解決了這個問題,我將SMTP主機從smtp.yandex.com更改爲smtp.yandex.ru,並且我使用的是端口25. 完美適用於我。 這裏是代碼:

EmailCredentials credentials = new EmailCredentials(); 
    credentials.Domain = "domain.com"; 
    credentials.SMTPUser = "[email protected]"; 
    credentials.SMTPPassword = "pass"; 
    int SmtpPort = 25; 
    string SmtpServer = "smtp.yandex.ru"; 

    MailMessage EmailMsg = new MailMessage(); 
    EmailMsg.From = new MailAddress("[email protected]", "Domain"); 
    EmailMsg.To.Add(new MailAddress("[email protected]", "[email protected]")); 
    EmailMsg.ReplyToList.Add("[email protected]"); 

    EmailMsg.Subject = "Welcome"; 

    EmailMsg.Body = "HTML body code"; 

    EmailMsg.IsBodyHtml = true; 
    EmailMsg.Priority = MailPriority.Normal; 

    System.Net.Mail.SmtpClient SMTP = new System.Net.Mail.SmtpClient(); 
    SMTP.Host = SmtpServer; 
    SMTP.Port = SmtpPort; 
    SMTP.EnableSsl = true; 
    SMTP.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
    SMTP.UseDefaultCredentials = false; 
    SMTP.Credentials = new System.Net.NetworkCredential(credentials.SMTPUser, credentials.SMTPPassword); 

    SMTP.Send(EmailMsg);