出于某种原因,既不是接受的答案,也没有其他工作为我“通过Gmail在。net发送电子邮件”。为什么它们不会起作用?

更新:我已经在另一个问题中尝试了所有的答案(接受的和其他的),但没有一个有效。

我只是想知道它是否适用于其他人,否则谷歌可能已经改变了一些东西(以前发生过)。

当我尝试使用SmtpDeliveryMethod的代码段时。网络,我很快收到一个SmtpException发送(消息)。信息是

SMTP服务器需要安全连接,否则客户端未经过身份验证。

服务器的回复是:

5.5.1认证要求在“<——说真的,它到此结束。

更新:

这是我很久以前问过的一个问题,公认的答案是我在不同的项目中使用过很多很多次的代码。

我采用了这篇文章和其他EmailSender项目中的一些想法,在Codeplex上创建了一个EmailSender项目。它是为可测试性而设计的,支持我最喜欢的SMTP服务,如GoDaddy和Gmail。


当前回答

You can also connect via port 465, but due to some limitations of the System.Net.Mail namespace you may have to alter your code. This is because the namespace does not offer the ability to make implicit SSL connections. This is discussed at http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx, and I have supplied an example of how to use the CDO (Collaborative Data Object) in another discussion (GMail SMTP via C# .Net errors on all ports).

其他回答

这些错误有一个或多个原因。

Log in with your Gmail( or any other if ) in your local system. Also check for Less Secure App and Set it to "Turn On" here is the Link for GMAIL. https://www.google.com/settings/security/lesssecureapps check for EnableSsl in your Email code, and also set it to true. smtp.EnableSsl = true; Also check which port are you using currently. 25 is Global, but you can check it for others too like 587. check here. Does all SMTP communication happen over 25? IF YOU ARE ON REMOTE : Check the answer of Vlad Tamas above.

CVertex,确保检查你的代码,如果没有发现什么,就发布出来。我只是在一个测试ASP上启用这个。我在NET网站上工作,它工作。

实际上,在某种程度上,我的代码出现了问题。直到我在一个控制台程序上有一个更简单的版本,并看到它正在工作(Gmail端没有你所担心的变化),我才发现它。下面的代码就像你提到的例子一样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
                EnableSsl = true
            };
            client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
            Console.WriteLine("Sent");
            Console.ReadLine();
        }
    }
}

我还得到了它的工作使用网络的组合。config, http://msdn.microsoft.com/en-us/library/w355a94k.aspx和code(因为在配置文件中没有匹配的EnableSsl:)。

2021年更新

默认情况下,您还需要在您的gmail设置页面:google.com/settings/security/lesssecureapps中启用“不太安全的应用程序”。这是必要的,如果你得到异常" '服务器的响应是:5.5.1认证需要。-感谢@Ravendarksky

问题不是通过gmail发送的技术能力问题。这在大多数情况下都适用。如果你不能让机器发送,通常是由于机器没有经过至少一次人工控制的验证。

大多数用户面临的问题是,谷歌总是决定更改出站限制。您应该始终在解决方案中添加防御性代码。如果你开始看到错误,放慢你的发送速度,只是停止发送一段时间。如果您继续尝试发送谷歌,有时会在您再次发送之前增加额外的延迟时间。

在我目前的系统中,我所做的是在每条消息之间发送1.5秒的延迟。如果我发现任何错误,停止5分钟,然后重新开始。这通常是有效的,并且允许您发送到帐户的限制(最后我检查了它是每天2000个高级客户登录)。

smtp.Host = "smtp.gmail.com"; //host name
smtp.Port = 587; //port number
smtp.EnableSsl = true; //whether your smtp server requires SSL
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;

如果您的Gmail帐户上有两步验证,您将需要生成一个应用程序密码。 https://support.google.com/accounts/answer/185833?p=app_passwords_sa&hl=en&visit_id=636903322072234863-1319515789&rd=1 选择如何生成应用程序密码选项,并按照提供的步骤进行操作。复制并粘贴生成的应用程序密码的某个地方,因为你将无法恢复它后,你点击完成。