我正在尝试通过构建基于控制台的应用程序来发送电子邮件,但是每当我尝试运行它时,它都会显示错误消息:“套接字异常:无法建立连接,因为目标计算机主动拒绝了它74.125.130.109:587 ”,有人可以向我解释我的代码有什么问题,或者我的系统设置没有问题吗?
using System.Net;
using System.Net.Mail;
namespace Email
{
class Program
{
static void Main(string[] args)
{
Program obj = new Program();
obj.SendEmail("Just a Mock Message");
}
public void SendEmail(string emailBody)
{
MailMessage mail = new MailMessage("FromEmail.com","ToEmail.com");
mail.Subject = "Testing my Application";
mail.Body = emailBody;
SmtpClient smtp = new SmtpClient("smtp.gmail.com",587);
smtp.Credentials = new NetworkCredential()
{
UserName = "EmailExample.com",
Password = "example123"
};
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
}
答案 0 :(得分:1)
smtp.gmail.com的SSL默认端口为465。尝试
SmtpClient smtp = new SmtpClient("smtp.gmail.com",465);