2009-07-04 102 views
10

我一直在爲此嘗試一種方式,迄今一直在失敗。我最近的嘗試從這個堆棧代碼中解除:Sending email through Gmail SMTP server with C#,但我嘗試了所有可以在堆棧和其他位置找到的語法。我的代碼目前是:GMail SMTP通過所有端口上的C#.Net錯誤

var client = new SmtpClient("smtp.gmail.com", 587) 
{ 
    Credentials = new NetworkCredential("[email protected]", "mypass"), 
    EnableSsl = true 
}; 

client.Send("[email protected]","[email protected]","Test", "test message"); 

運行的代碼讓我有一個innerexeption「無法連接到遠程服務器」的即時異常「發送郵件失敗」。

如果我將端口更改爲465(如gmail文檔所示),每次都會收到一次超時。

我讀過465不是一個好的端口使用,所以我想知道什麼交易是w/587給我失敗連接。我的用戶和通行證是正確的。我讀過我必須在我的Gmail帳戶上設置POP服務,所以我這樣做了。徒勞無功。

我原本試圖讓我的品牌GMail帳戶的這個工作,但遇到相同的問題之後,我認爲去我的常規Gmail帳戶會更容易...到目前爲止,這不是案件。

+0

您可以通過標準電子郵件客戶端進行連接嗎?否則,它可能是您防火牆的問題。您是否在帳戶中啓用POP訪問權限? – MiffTheFox 2009-07-04 13:42:57

+0

是的,我可以,而且我也是。 – Paul 2009-07-06 02:26:14

回答

14

我想你的代碼,並將其與端口587工程prefectly,但不能與465

你檢查的防火牆?嘗試從命令行「Telnet smtp.gmail.com 587」 如果您收到「220 mx.google.com ESMTP ....」,則該端口已打開。如果沒有,這是阻止你打電話的東西。

丹尼爾

+0

謝謝,那就是它。我將規則放在防火牆中讓端口587通過,而我的電子郵件客戶端沒有問題使用它,但是我的a/v懷疑我的控制檯測試應用程序,認爲它是一種試圖傳播的蠕蟲。 – Paul 2009-07-06 02:55:48

9

我實現了一個電子郵件客戶端備份的某個時候可能跟Gmail的兩個587和465 ......

端口25是正常的未加密的POP端口;在Gmail上不可用。

另外兩個端口有加密; 587使用TLS,465使用SSL。

要使用587,您應該設置SmtpClient.EnableSsl = true。

465不會與SmtpClient一起使用,它將使用棄用的類SmtpMail代替。

1

你的專用網絡的防火牆阻塞的端口587和465可以使用默認端口25或啓用防火牆

15

我前一陣子在跑這個問題也有端口。問題是SmtpClient不支持隱式SSL連接,但確實支持顯式連接(System.Net.Mail with SSL to authenticate against port 465)。以前的MailMessage類(我相信.Net 1.0)確實支持這一點,但早已過時。

我的回答是使用類似下面直接通過COM調用CDO(協作數據對象)(http://support.microsoft.com/kb/310212):

/// <summary> 
    /// Send an electronic message using the Collaboration Data Objects (CDO). 
    /// </summary> 
    /// <remarks>http://support.microsoft.com/kb/310212</remarks> 
    private void SendTestCDOMessage() 
    { 
     try 
     { 
      string yourEmail = "[email protected]"; 

      CDO.Message message = new CDO.Message(); 
      CDO.IConfiguration configuration = message.Configuration; 
      ADODB.Fields fields = configuration.Fields; 

      Console.WriteLine(String.Format("Configuring CDO settings...")); 

      // Set configuration. 
      // sendusing:    cdoSendUsingPort, value 2, for sending the message using the network. 
      // smtpauthenticate:  Specifies the mechanism used when authenticating to an SMTP service over the network. 
      //         Possible values are: 
      //         - cdoAnonymous, value 0. Do not authenticate. 
      //         - cdoBasic, value 1. Use basic clear-text authentication. (Hint: This requires the use of "sendusername" and "sendpassword" fields) 
      //         - cdoNTLM, value 2. The current process security context is used to authenticate with the service. 

      ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"]; 
      field.Value = "smtp.gmail.com"; 

      field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]; 
      field.Value = 465; 

      field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"]; 
      field.Value = CDO.CdoSendUsing.cdoSendUsingPort; 

      field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]; 
      field.Value = CDO.CdoProtocolsAuthentication.cdoBasic; 

      field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"]; 
      field.Value = yourEmail; 

      field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]; 
      field.Value = "YourPassword"; 

      field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]; 
      field.Value = "true"; 

      fields.Update(); 

      Console.WriteLine(String.Format("Building CDO Message...")); 

      message.From = yourEmail; 
      message.To = yourEmail; 
      message.Subject = "Test message."; 
      message.TextBody = "This is a test message. Please disregard."; 

      Console.WriteLine(String.Format("Attempting to connect to remote server...")); 

      // Send message. 
      message.Send(); 

      Console.WriteLine("Message sent."); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 

不要忘記通過你的COM引用,瀏覽並添加「微軟CDO對於Windows 200庫「,它應該添加兩個引用:ADODB和CDO。

1

我有同樣的問題,但無法改變我公司的防火牆限制。根據this Google doc的註釋之一,以及上面的erdenetsogt的答案,我嘗試使用端口25,它的工作。(起初我擔心使用端口25可能意味着該消息可能沒有被加密;所以我將EnableSSL設置爲false,導致gmail拒絕它,因爲StartTLS從未被調用過,這使我相信gmail正在執行Explicit SSL,甚至通過端口25)。

0

有兩種方法可以通過SSL執行SMTP:顯式和隱式。顯式表示您以明文形式連接到普通SMTP端口(通常爲25或587),然後發出「starttls」命令切換到SSL模式。隱式意味着你連接到一個希望所有東西都是SSL的端口(通常是465)。

Asp.net使用「System.Net.Mail.SmtpClient()」發送電子郵件。主要問題是SmtpClient不支持隱式SSL連接,但確實支持顯式連接(帶SSL的System.Net.Mail根據端口465進行身份驗證)。因此,如果郵件服務器(SMTP)不支持顯式連接,它將無法發送電子郵件和顯示消息,如「連接超時」,「郵件無法發送到SMTP服務器。傳輸錯誤代碼是0x80040217。服務器響應不可用「等。

要在ASP.net中解決此問題,我們可以使用用於Windows 2000庫(Cdosys.dll)的協作數據對象(CDO)發送帶附件的電子郵件。 Microsoft Outlook使用此DLL發送電子郵件。在您的ASP.net解決方案中,您必須添加參考「Windows 2000庫的Microsoft CDO」。它會在Bin文件夾中添加兩個標記爲dll的文件。

image to add referance

現在做婁代碼在C#.net:

public static void SendMail(string FromName, string FromEmail, string ReceiverEmail, string CC, string BCC, string subj, string Mssg) 
{ 
const var cdoSendUsingPort = 2; 
const var cdoBasicAuth = 1; 
const var cdoTimeout = 60; 
var mailServer = "mail.XXXXXXX.net"; 
var SMTPport = 465; 
var mailusername = "[email protected]"; 
var mailpassword = "PPPPXXXX"; 
var objEmail = CreateObject("CDO.Message"); 
var objConf = objEmail.Configuration; 
var objFlds = objConf.Fields; 
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort; 
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer; 
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPport; 
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true; 
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = cdoTimeout; 
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasicAuth; 
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailusername; 
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailpassword; 
objFlds.Update(); 
objEmail.To = ReceiverEmail; 
objEmail.From = FromEmail; 
objEmail.CC = CC; 
objEmail.BCC = BCC; 
objEmail.Subject = subj; 
objEmail.HTMLBody = Mssg; 
objEmail.Send(); 
} 

在VB.net

Public Shared Sub SendMail(ByVal FromName As String, ByVal FromEmail As String, ByVal ReceiverEmail As String, ByVal CC As String, ByVal BCC As String, ByVal subj As String, ByVal Mssg As String) 

''#################Sending Email########################## 

Const cdoSendUsingPort = 2 ' Send the message using SMTP 
Const cdoBasicAuth = 1 ' Clear-text authentication 
Const cdoTimeout = 60 ' Timeout for SMTP in seconds 

Dim mailServer = "mail.XXXXXXX.net" 
Dim SMTPport = 465 
Dim mailusername = "[email protected]" 
Dim mailpassword = "PPPPXXXX" 




Dim objEmail = CreateObject("CDO.Message") 
Dim objConf = objEmail.Configuration 
Dim objFlds = objConf.Fields 

With objFlds 
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort 
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer 
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPport 
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True 
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = cdoTimeout 
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasicAuth 
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailusername 
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailpassword 
.Update() 
End With 

objEmail.To = ReceiverEmail 
objEmail.From = FromEmail 
objEmail.CC = CC 
objEmail.BCC = BCC 
objEmail.Subject = subj 
objEmail.HTMLBody = Mssg 
'objEmail.AddAttachment "C:\report.pdf" 
objEmail.Send() 
End Sub 

全球化志願服務青年: Original post 隱&明確SMTP http://help.fogcreek.com/9002/using-an-smtp-server-with-ssl 使用Cdosys.dll庫發送帶附件的電子郵件https://support.microsoft.com/en-us/help/310212/how-to-use-the-cdosys-dll-library-to-send-an-e-mail-message-with-attac