我正在尝试使用 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("[email protected]", "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 requires you to use 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(服务可能已关闭),请尝试使用 SSL 的 465 或使用 SSL 的 25。见 here for Gmail help on sending email 。
关于c# - 发送电子邮件 - 错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5730867/