2015-05-21 37 views
0

我試圖從我的C#web應用程序中打gmail smtp服務器,但得到以下錯誤:
1)失敗發送消息
2)「操作已超時。
使用C#打開Gmail的smtp服務器,但獲得很少的錯誤。請建議

以下是我的代碼,

MailMessage message = new MailMessage(); 
     SmtpClient smtpClient = new SmtpClient(); 
     string msg = string.Empty; 
     try 
     { 
      MailAddress fromAddress = new MailAddress(from); 
      message.From = fromAddress; 
      message.To.Add(to); 

      message.Subject = Name; 
      message.IsBodyHtml = true; 
      message.Body = "Some Message"; 
      smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client 
      smtpClient.Port = 465; //587 for TLS 465 or 25 SSL 
      smtpClient.EnableSsl = true; 
      smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; 
      smtpClient.UseDefaultCredentials = false; 
      smtpClient.Credentials = new System.Net.NetworkCredential(from, fromPassword); 
      smtpClient.Timeout = 50000; 
      smtpClient.Send(message); 
     } 
     catch (Exception ex) 
     { 
      if (ex.InnerException != null) 
      { 
       Console.WriteLine("InnerException is: {0}", ex.InnerException); 
      } 
     } 

請建議我已經使用端口587,以及嘗試過,但都顯示了同樣的錯誤。下面

回答

0

This環節中最大的細節解釋瞭如何做到這一點(在幾個方面)鏈接的

static void Main(string[] args) 
     { 
      SmtpMail oMail = new SmtpMail("TryIt"); 
      SmtpClient oSmtp = new SmtpClient(); 

      // Your gmail email address 
      oMail.From = "[email protected]"; 

      // Set recipient email address 
      oMail.To = "[email protected]"; 

      // Set email subject 
      oMail.Subject = "test email from gmail account"; 

      // Set email body 
      oMail.TextBody = "this is a test email sent from c# project with gmail."; 

      // Gmail SMTP server address 
      SmtpServer oServer = new SmtpServer("smtp.gmail.com"); 

      // Set 465 port 
      oServer.Port = 465; 

      // detect SSL/TLS automatically 
      oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; 

      // Gmail user authentication 
      // For example: your email is "[email protected]", then the user should be the same 
      oServer.User = "[email protected]"; 
      oServer.Password = "yourpassword"; 

      try 
      { 
       Console.WriteLine("start to send email over SSL ..."); 
       oSmtp.SendMail(oServer, oMail); 
       Console.WriteLine("email was sent successfully!"); 
      } 
      catch (Exception ep) 
      { 
       Console.WriteLine("failed to send email with the following error:"); 
       Console.WriteLine(ep.Message); 
      } 
     } 
+0

後的相關部分,而一個鏈接,以避免downvotes – tharif

相關問題