【问题标题】:Sending mail using gmail and .net使用 gmail 和 .net 发送邮件
【发布时间】:2015-08-21 16:49:02
【问题描述】:

获取异常:

发送邮件失败。

使用System.Net.Mail.Smtp时 在C#.NET就行了smtp.Send(message);

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();            
message.To.Add(sendto);    
message.Subject = "CP1 Lab Password";
message.From = new System.Net.Mail.MailAddress("abc@gmail.com");
message.Body = mail_message;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");

smtp.Host = "smtp.gmail.com";
smtp.Port = 465; //(465 for SSL)587 
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "mypassword");    
smtp.Send(message);

编辑 1:

这是错误的详细信息:

连接尝试失败,因为连接方没有 一段时间后正确响应,或建立连接 失败,因为连接的主机没有响应...

使用端口 25 时同样的错误。

几个月前这段代码可以工作,但今天它不能工作

【问题讨论】:

标签: c# asp.net email


【解决方案1】:

从您的异常消息(不完整)和代码来看,很难说您的情况出了什么问题。找不到服务器,端口无法连接,发送邮件失败。这意味着直到现在还没有建立连接。

如果已建立连接但未正确执行身份验证,则会引发异常消息,“SMTP 服务器需要经过身份验证的连接...”。我建议你检查一下PORT、SMTP服务器主机(不要加http://)然后重试。 SMTP 连接(在大多数情况下)需要

  1. SMTP 服务器主机:smtp.gmail.com 就够了!
  2. 要连接的端口:我一直使用 SMTP 的默认 TCP 端口,25。它可以工作。
  3. EnableSsl:大多数都需要它。我建议你总是使用它。 client.EnableSsl = true;
  4. 所有人都需要凭据:没有服务器允许机器人发送电子邮件。在这种情况下,如果您创建一个新帐户。您可能会遇到以编程方式发送电子邮件的问题,我遇到的问题是新帐户无法发送电子邮件,而旧帐户(我的默认帐户)可以毫无问题地发送电子邮件。

以下代码模板(如果填写准确的参数)肯定会发送电子邮件,因为我已经测试和验证了无数次。 :)

// You should use a using statement
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
   // Configure the client
   client.EnableSsl = true;
   client.Credentials = new NetworkCredential("<username>", "<password>");
   // client.UseDefaultCredentials = true;

   // A client has been created, now you need to create a MailMessage object
   MailMessage message = new MailMessage(
                            "from@example.com", // From field
                            "to@example.com", // Recipient field
                            "Hello", // Subject of the email message
                            "World!" // Email message body
                         );

   // Send the message
   client.Send(message);

   /* 
    * Since I was using Console app, that is why I am able to use the Console
    * object, your framework would have different ones. 
    * There is actually no need for these following lines, you can ignore them
    * if you want to. SMTP protocol would still send the email of yours. */

   // Print a notification message
   Console.WriteLine("Email has been sent.");
   // Just for the sake of pausing the application
   Console.Read();
}

有时发送电子邮件可能会让人头疼,因为它还需要对网络有一些基本的了解。我写了一篇文章,介绍了在 .NET 框架中发送电子邮件以及初学者通常会遇到的一些问题,例如这个。你可能也有兴趣阅读那篇文章。 http://www.codeproject.com/Articles/873250/Sending-emails-over-NET-framework-and-general-prob

【讨论】:

  • 是的,我做到了。除了第 2 点,我一直在关注所有要点。在你的帖子之后,我也尝试了 25 端口。但没有奏效。我还检查了“允许不太安全的应用程序”,并按照 user2946329 的建议删除了“yoursmtphost”字样。但它没有用。剩下的就是复制粘贴您提供的代码。你能指出错误在哪里吗?
  • 不清楚可能是什么问题,您可以更新您的问题并发布您自己的代码吗? 您当然可以隐藏密码。发布其余代码。
  • 确保您可以真正 ping 服务器并建立连接,我遇到了网络管理员阻止访问特定端口的问题。
  • "连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应 74.125.200.109:25 " 就是这个错误。当我尝试使用 cmd 执行 ping 74.125.200.109 时,请求超时。
猜你喜欢
  • 2013-02-03
  • 2015-05-28
  • 1970-01-01
  • 2011-11-24
  • 2012-07-14
  • 1970-01-01
  • 2011-04-06
  • 2012-04-13
  • 2014-08-27
相关资源
最近更新 更多