2012-01-28 151 views
2

當我嘗試與使用Gmail VB.Net或C#我不斷收到以下消息發送電子郵件:不發送電子郵件 - 試圖訪問一個插座以其訪問權限禁止的方式 - 無法訪問遠程服務器。 我一直在使用Gmail的幾個帳戶嘗試包括在過去的工作VB.Net代碼,如下所示:與Gmail和C#發送電子郵件/ VB.Net不再起作用

Message = New MailMessage(Sender, Recipient, Subject, MessageBody) 

SMTPServer = New SmtpClient("smtp.gmail.com", 587)'Port 465 fails as well 
SMTPServer.EnableSsl = True 

SMTPServer.Credentials = New NetworkCredential("[email protected]", "password") 
SMTPServer.Send(Message) 

(據我所知,在web.config可用於很多以上)。

顯然Gmail中必須更改了某些設置或類似的東西?

+1

關閉防火牆並重試。 – Jashwant 2012-01-28 10:56:24

+0

我試圖關閉防火牆,但沒有任何區別。 – Lee 2012-01-28 11:05:35

回答

4

這是McAfee防病毒軟件防止電子郵件的發送。感謝所有的幫助,並且很抱歉浪費大家的時間。

+0

;)我的第一個評論你的問題 – Jashwant 2012-01-30 11:16:08

1

我不知道如何評論這樣:我想你忘記了港口和幾件事情。看看這有助於:

Imports System.Net.Mail 

Protected Sub btnSendEmail_Click(ByVal sender As Object, ByVal e As EventArgs) 
    Dim mail As MailMessage = New MailMessage() 
    mail.To.Add("[email protected]") 
    mail.From = New MailAddress("[email protected]") 
    mail.Subject = "Email using Gmail" 

    String Body = "Sending mail using Gmail's SMTP" 
    mail.Body = Body 

    mail.IsBodyHtml = True 
    Dim smtp As SmtpClient = New SmtpClient() 
    smtp.Host = "smtp.gmail.com" 
    smtp.Credentials = New System.Net.NetworkCredential 
     ("[email protected]","password") 
    smtp.EnableSsl = True 
    smtp.Port = 587 
    smtp.EnableSsl = true 
    smtp.Send(mail) 
End Sub 

試試這個編輯

+0

我試過你的代碼,並得到和以前完全相同的異常,以及'無法連接到遠程服務器'。上次我發送Gmail郵件時,我使用的是Vista Home Premium x32,現在我正在使用Windows 7 Ultimate x64,除此之外,我無法想象任何不同之處。 – Lee 2012-01-28 11:01:20

+0

最新錯誤?因爲當我嘗試它時它工作正常。 – 2012-01-28 11:03:28

+0

嘗試這可能工作 – 2012-01-28 11:10:04

3

此代碼工作正常,我:

try 
    { 
     MailMessage mail = new MailMessage();  //using System.Net.Mail namespace 
     mail.To.Add("[email protected]");    //Enter reciever's email address 
     mail.From = new MailAddress("[email protected]"); //Enter sender's email address 
     mail.Subject = "Testing mail..."; 
     mail.Body = @"Lets-code ! Lets-code to make it simpler"; 
     mail.IsBodyHtml = true;     //Body of mail supports html tags 
     SmtpClient smtp = new SmtpClient(); 
     smtp.Host = "smtp.gmail.com"; 
     smtp.Port = 587; 
     smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "pwd"); 
     // Your gmail username and password 
     smtp.EnableSsl = true;   //Gmail uses a encrypted connection 
     smtp.Send(mail); 
     Response.Write("Mail Sent Successfully"); 
    } 

catch(Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 

如果這沒有幫助,嘗試另一臺機器上。 Windows 7擁有自己的防火牆。也檢查出來。

+0

我停用防火牆後,我想你的代碼以及仍然得到相同的錯誤消息,這似乎是一個機器特定的問題:( – Lee 2012-01-28 11:23:02

0

谷歌有保護您的Gmail帳戶的新程序,使用此代碼,您必須轉到Gmail設置並關閉安全性較低的客戶的阻塞。