发送电子邮件 – 错误

我正在尝试使用Winforms应用程序中的以下代码发送电子邮件。 用户名和密码正确且真实,但在代码中已屏蔽。

我不知道我的本地机器是否有能力发送电子邮件?

但请告诉我,我需要查看我的机器配置(在IIS中)?

错误:“由于目标机器主动拒绝它,因此无法建立连接209.85.143.109:587”

//All Variable Declarations String senderAddress = string.Empty; String receiveraddress = string.Empty; String emailSubject = string.Empty; String emailMessageText = string.Empty; MailMessage mail = new MailMessage(); SmtpClient client = new SmtpClient("smtp.gmail.com",587); private void btnEmail_Click(object sender, EventArgs e) { try { //Get the Values of Controls senderAddress = txtSenderEmail.Text; receiveraddress = txtReceiverEmail.Text; emailSubject = txtSubject.Text; emailMessageText = txtMessageBody.Text; //Pass the Values mail.From = new MailAddress(senderAddress, "xxxxxxxxxx", System.Text.Encoding.UTF8); mail.To.Add(receiveraddress); mail.Subject = emailSubject; mail.Body = emailMessageText; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxx"); client.EnableSsl = true; client.Send(mail); System.Windows.Forms.MessageBox.Show("Email Sent Successfully"); } catch (Exception mailException) { System.Windows.Forms.MessageBox.Show("Message Not Sent ,Error: " + mailException.Message); throw; } finally { } } 

我为这两个端口做了一个telnet,错误来自:无法打开与主机的连接。

请看下面的图片,它应该给你更清晰,

在此处输入图像描述

GMail要求您使用SSL 。 您需要用于TLS / STARTTLS的端口是587.对于SSL,它是465.以下是可用于通过命令行发送电子邮件的代码。 CommandLine.Utility可以在CodeProject下载。

 using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.ComponentModel; using CommandLine.Utility; namespace SmtpClientProgram { class Program { static void Main(string[] args) { Arguments cmdLine = new Arguments(args); SmtpClient mailer = new SmtpClient(); // --ssl means we are using SSL/TLS mailer.EnableSsl = Convert.ToBoolean(cmdLine["ssl"]); // -host=smtp.gmail.com if (cmdLine["host"] != null) mailer.Host = cmdLine["host"]; else { Console.WriteLine("Hostname must be specified"); return; } // -port=25 if (cmdLine["port"] != null) try { mailer.Port = Convert.ToInt32(cmdLine["port"]); } catch (Exception) { Console.WriteLine("Port must be a number between 1 and 65535"); } else mailer.Port = 25; // -user= -password= if (cmdLine["user"] != null && cmdLine["password"] != null) { mailer.Credentials = new NetworkCredential(cmdLine["user"], cmdLine["password"]); } try { MailMessage mail = new MailMessage(); mail.From = new MailAddress(cmdLine["from"], cmdLine["name"]); if (cmdLine["to1"] != null) mail.To.Add(cmdLine["to1"]); else Console.WriteLine("Must specify first TO address"); if (cmdLine["to2"] != null) mail.To.Add(cmdLine["to2"]); if (cmdLine["to3"] != null) mail.To.Add(cmdLine["to3"]); if (cmdLine["subject"] != null) mail.Subject = cmdLine["subject"]; if (cmdLine["body"] != null) mail.Body = cmdLine["body"]; mailer.Send(mail); //mailer.Send(cmdLine["from"], cmdLine["to"], cmdLine["subject"], cmdLine["body"]); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } Console.WriteLine("Success"); } } } 

编辑:如果您无法连接到端口587(服务可能已关闭),请尝试465使用SSL或25使用SSL。 有关发送电子邮件的Gmail帮助,请参阅此处

似乎问题不在于您的代码,而在于您要使用的SMTP服务器。 检查计算机209.85.143.109上的端口587是否已打开并接受SMTP命令。

这通常意味着在指定位置没有运行邮件服务器,或者您需要配置防火墙以通过连接并设置路由器以将587的连接转发到网络上的正确服务器IP。

看起来它会转到某些SMTP服务器,您的IIS正在转发到您的组织SMTP服务器。

如果它只是用于开发,您可以使用PickUP目录function,这将使您可以测试没有任何SMTP设置。 谷歌上存在大量信息

编辑

删除了谷歌链接。 有些人不喜欢它。 虽然我并不是要冒犯任何人,但我仍然会删除,但我的意图仍然是帮助