GMail SMTP通过所有端口上的C#.Net错误

我一直在为此而努力,迄今一直在悲惨地失败。 我最近的尝试是从这个堆栈代码中解除的: 使用C#通过Gmail SMTP服务器发送电子邮件 ,但是我尝试了所有可以在堆栈和其他地方find的语法。 我的代码目前是:

var client = new SmtpClient("smtp.gmail.com", 587) { Credentials = new NetworkCredential("me@gmail.com", "mypass"), EnableSsl = true }; client.Send("me@gmail.com","me@gmail.com","Test", "test message"); 

运行该代码给我一个立即的exception“发送邮件失败”,它有一个“无法连接到远程服务器”的内联。

如果我将端口更改为465(正如gmail文档所build议的那样),我每次都会收到一个超时。

我读过465不是一个很好的端口,所以我想知道这个交易是怎样的,让我失败了。 我的用户和通行证是正确的。 我读过,我必须有POP服务设置在我的Gmail帐户,所以我做到了。 徒劳无功。

我本来是试图让我的品牌GMail帐户工作,但遇到相同的问题后,我认为去我的常规Gmail帐户会更容易…到目前为止,情况并非如此。

谢谢您的帮助。 保罗

我试过你的代码,它与587端口正常工作,但不与465。

你检查过防火墙吗? 尝试从命令行“Telnet smtp.gmail.com 587”如果您收到“220 mx.google.com ESMTP ….”,那么端口是打开的。 如果没有,这是阻止你打电话的东西。

丹尼尔

前段时间我也遇到了这个问题。 问题是SmtpClient不支持隐式SSL连接,但确实支持显式连接( 带SSL的System.Net.Mail可以对端口465进行身份validation )。 以前的类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 = "YourUserName@gmail.com"; 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引用,并添加“Microsoft CDO for Windows 200 Library”,它应该添加两个引用:ADODB和CDO。

我实现了一个电子邮件客户端,可以在587和465上与gmail交谈。

端口25是普通的未encryptionstream行端口; 在Gmail上不可用。

另外两个端口具有encryptionfunction; 587使用TLS,465使用SSL。

要使用587,你应该设置SmtpClient.EnableSsl = true。

465不会与SmtpClient一起使用,它将使用不推荐使用的类SmtpMail。

在服务器上尝试此链接http://www.google.com/accounts/DisplayUnlockCaptcha

商业Gmail帐户(或普通的Gmail帐户不确定这个帐户)需要一个DisplayUnlockCaptcha第一次。

我也有同样的问题,但无法改变我公司的防火墙限制。 基于这个谷歌文档的笔记之一,以及上面erdenetsogt的答案,我尝试使用端口25,它的工作。 (起初我担心使用端口25可能意味着这个消息可能没有被encryption,所以我把EnableSSL设置为false,导致gmail拒绝它,因为StartTLS从来没有被调用过,这使我相信gmail正在执行Explicit SSL,甚至通过端口25)。

您的专用networking的防火墙阻止了端口587和465.您可以使用默认端口25或启用防火墙上的端口