2009-07-04 105 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