Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从gmail发送电子邮件时遇到异常_C# - Fatal编程技术网

C# 从gmail发送电子邮件时遇到异常

C# 从gmail发送电子邮件时遇到异常,c#,C#,我正试图用gmail给人们发电子邮件。我不确定这为什么不能正常工作。下面是抛出错误的屏幕截图 下面是设置我的邮件客户端的代码 string body = File.ReadAllText(@"C:\Data\MailTemplates\welcome.html"); MailMessage message = new MailMessage(); SmtpClient client = new SmtpClient("smtp.gmail.com");

我正试图用gmail给人们发电子邮件。我不确定这为什么不能正常工作。下面是抛出错误的屏幕截图

下面是设置我的邮件客户端的代码

string body = File.ReadAllText(@"C:\Data\MailTemplates\welcome.html");
        MailMessage message = new MailMessage();
        SmtpClient client = new SmtpClient("smtp.gmail.com");

        client.Port = 587;
        client.Credentials = new System.Net.NetworkCredential("sean.shydow", "password");
        client.EnableSsl = true;
        message.From = new MailAddress("sean.shydow@gmail.com");
        message.IsBodyHtml = true;
        ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };
        message.To.Add("sean@gmail.com");
        message.Subject = subject.Text;
        message.SubjectEncoding = System.Text.Encoding.Unicode;
        message.Body = body;
        message.BodyEncoding = System.Text.Encoding.Unicode;

端口587用于TLS。使用SSL时,请尝试端口465(有关更多信息,请参阅)


还要确认您的机器可以实际连接到Gmail SMTP端口。一些ISP阻止它们,以避免僵尸机器成为垃圾邮件工厂。请尝试
telnet smtp.google.com端口
查看
端口
s 25、465和587,。如果超时,则很可能连接到特定IP范围之外的这些端口时被防火墙拦截。

要查看异常的控制台图片,请右键单击并在新窗口或选项卡中打开。如果至少将调用堆栈的前几行作为文本包含,则会有所帮助。@Sean,你不能发布异常的文本吗?控制台应用的屏幕截图不是很有用…你的机器真的可以连接到Gmail SMTP端口吗?一些ISP屏蔽它们,以避免僵尸机器成为垃圾邮件工厂。请尝试
telnet smtp.google.com端口
以获取
端口
s 25、465和587(现在已编辑到我的答案中)。请稍候,我将telnet smtp.google.com放在何处,我将如何处理端口25 465和587?很抱歉,我有点困惑
telnet
是一个命令行程序,它只打开一个地址和端口的套接字,让您键入它,同时将从远端发送的任何内容回显到屏幕上。在本例中,它是一个调试辅助工具。在我的平台上搜索
telnet
获取更多信息。是的,我的机器正在阻止ISP,非常感谢您提供的信息!干杯