2014-09-19 171 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