2014-09-19 195 views
1

我想通过gmail发送电子邮件,但此代码不起作用,导致连接超时错误。如果我做端口“587”它给了这个错误:Gmail发送电子邮件超时

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

 string email = // email 
     string password = // password 
     string smtp = // smtp.gmail.com 
     int port = // 465 

     var from = new MailAddress(email, ""); 
     var to = new MailAddress(message.Destination); 

     var client = new SmtpClient() 
     { 
      Host = smtp, 
      Port = port, 
      EnableSsl = true, 
      DeliveryMethod = SmtpDeliveryMethod.Network, 
      UseDefaultCredentials = false, 
      Credentials = new NetworkCredential(email, password) 
     }; 

     var mail = new MailMessage(from, to) 
     { 
      Subject = subject, 
      Body = body, 
      IsBodyHtml = true 
     }; 

     return client.SendMail(mail); 
    } 
+0

您是否能够从您正在运行程序的计算机以其他方式连接到smtp.gmail.com:465?例如,通过telnet到这个地址/端口。 – 2014-09-19 12:42:30

+0

它也不适用于Azure。 – Barte 2014-09-19 12:46:29

回答

0

我已经在过去使用非常相似的代码到你,它的工作,但我使用的端口号为587 这是我的代码:

SmtpClient client = new SmtpClient 
            { 
             Host = "smtp.gmail.com", 
             Port = 587, 
             EnableSsl = true, 
             DeliveryMethod = SmtpDeliveryMethod.Network, 
             UseDefaultCredentials = false, 
             Credentials = new NetworkCredential("gmail_login", "gmail_password") 
            }; 
using(var message = new MailMessage("your_emailAddress", "destination_Email") 
           { 
            Subject ="subject", 
            Body = "body" 
           }) 
client.Send(message); 

如果从一个新的目的地/时区登录你应该先通过网络浏览器登录并确认它是你:)

0

我最近公布的另一answer可以解决这种情况:

You need to make sure that the email account that you're using allows access from less secure apps.

Simply change a security setting from your account.

Try it here

相关问题