无法以编程方式发送电子邮件

时间:2015-05-09 13:07:35

标签: c#

以下代码抛出异常:

  

SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。

了解更多信息
var fromAddress = new MailAddress(toEmail, "From Name");
var toAddress = new MailAddress(fromEmail, "To Name");
const string fromPassword = "******";
string subject = sub;
try
{
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);
     }
 }
 catch (Exception ex)
 {
   Console.WriteLine(ex.Message);
 }

0 个答案:

没有答案