2016-06-23 89 views
1

我試圖用C#中的Windows應用程序發送郵件。我的代碼如下,並嘗試,但它不工作使用gmail發送郵件smtp - 無法連接遠程服務器

它給以下錯誤:

Unable to connect the remove server

代碼:

MailAddress sendd = new MailAddress("[email protected]","Sethu",Encoding.UTF8); 
MailAddress receivee = new MailAddress("[email protected]"); 
MailMessage mail = new MailMessage(sendd, receivee); 
SmtpClient client = new SmtpClient(); 
client.Port = 587; 
client.DeliveryMethod = SmtpDeliveryMethod.Network; 
client.UseDefaultCredentials = false; 
client.Host = "smtp.gmail.com"; 
client.EnableSsl = true; 
client.UseDefaultCredentials = false; 
client.Credentials = new System.Net.NetworkCredential("[email protected]", "yyyyyyy"); 
mail.Subject = "this is a test email."; 
mail.Body = "this is my test email body"; 
client.Send(mail); 

任何幫助找出我做了什麼錯?

+0

檢查這個的NetworkCredential(「[email protected]」,「 YYYYYYY「);電子郵件,你有收到電子郵件谷歌或雅虎允許訪問發送電子郵件到其他 –

+0

它似乎你沒有權利發送郵件從這個憑據 –

+0

在你的代碼中添加'EnableSsl = true'。 –

回答

0

可以嘗試讓你的代碼到類似下面的

var smptClient = new SmtpClient("smtp.gmail.com", 587) 
{ 
    Credentials = new NetworkCredential("[email protected]", "123456789"), 
    EnableSsl = true 
}; 
    smptClient.Send("[email protected]", "[email protected]", "Testing Email", "testing the email"); 

此代碼正確地與網絡爲我自己測試..

+0

代碼中的所有內容都是正確的。是否有權利或需要更改任何smtp設置的問題。 –

+0

因此可能是您的NetworkCredentials中存在問題,請先檢查這些配置,因爲您的代碼工作正常 –

0

下面的代碼工作對我來說

var client = new SmtpClient("smtp.gmail.com", 587) 
{ 
    Credentials = new NetworkCredential("[email protected]", "yyyyyyy"), 
    EnableSsl = true 
}; 
client.Send("[email protected]", "[email protected]", "this is a test email.", "this is my test email body"); 

這與您的差不多,它不使用MailMessage對象幾乎相同。這可能不是問題,但你可以試試看。

我還要檢查是否有端口587服務器上啓用

你也應該通過this topic,你發現很多有用的提示

+0

獲取相同的錯誤 –

+0

如果您嘗試使用此端口連接到任何其他SMTP服務器?它可能在防火牆上被拒絕。 –

+0

我在內部異常中發現消息「消息=嘗試以訪問權限的方式訪問套接字74.125.200.109:25」 –

相關問題