不确定为什么会这样。我搜索过的每个地方都告诉我,我正确地做到了这一点。但每次我尝试发送邮件时,它都会在smtpserver.Send(mail)
private void emailReport(string email_address,int begDatabaseCount, int endDatabaseCount)
{
SmtpClient smtpserver = new SmtpClient();
MailMessage mail = new MailMessage();
smtpserver.EnableSsl = true;
smtpserver.Port = 465;
smtpserver.Host = "smtp.gmail.com";
smtpserver.Credentials = new NetworkCredential("mtaylor@atr.com", "password");
smtpserver.UseDefaultCredentials = false;
mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("mtaylor@atr.com", "ATR Reports");
mail.To.Add(email_address);
mail.Subject = "FNAS Report - " + DateTime.Now;
mail.Body += "<u><b>FNAS Report for " + DateTime.Now + "</u></b>" + "\r\n \r\n";
mail.Body += "Beginning Database Count - " + begDatabaseCount + "\r\n" + "\r\n";
mail.Body += "End Database Count - " + endDatabaseCount + "\r\n" + "\r\n";
mail.Body += "<b>Total Imported Orders = " + (endDatabaseCount - begDatabaseCount) + "<b>" + "\r\n" + "\r\n";
mail.IsBodyHtml = true;
smtpserver.Send(mail);
}
端口465 = 1分钟后超时
端口587 =&#34; SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。 &#34;
答案 0 :(得分:10)
正如@kostyan所说,正确的端口是587,但要进行身份验证,您需要允许从您的Gmail帐户中安全性较低的应用程序进行访问。 试一试here
它对我有用,希望它有帮助..
答案 1 :(得分:5)
你确定端口,在我的代码中我把它作为587,否则看起来很相似并且有效。
答案 2 :(得分:4)
这个帖子帮助了我。我不确定为什么这个代码有效,而我的代码没有。
Sending email in .NET through Gmail
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
答案 3 :(得分:1)
我发现当我尝试使用google smtp欺骗发件人地址时(例如,使用FromAddress作为我的gmail帐户名称之外的其他内容)我收到了身份验证错误消息或只是超时。