2016-02-02 128 views
0

以下是我通過http代理髮送郵件的代碼。我已經將我的代理設置爲與上述相同。現在,當我嘗試運行程序時,經過一段時間它說* System.Net.WebException:無法解析遠程名稱。我可否知道我要去哪裏?從c#發送郵件失敗代理

protected void mailto(string message) 
    { 
     WebRequest.DefaultWebProxy = new WebProxy("10.1.1.4", 8080); 
     try 
     { 
      MailMessage mail = new MailMessage(); 
      SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 
      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add("to_address"); 
      mail.Subject = "For Data Using"; 
      mail.Body = message; 
      SmtpServer.Port = 465;// Tried even with 587, but no luck 
      SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword"); 
      SmtpServer.EnableSsl = true; 
      SmtpServer.Send(mail); 
      Debug.WriteLine(Environment.NewLine + "message sent"); 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine("Message not sent" + ex.ToString()); 
     } 
    } 

回答

1

我遇到了使用Gmail SMTP從c#發送郵件的問題,問題是端口。 嘗試使用的端口:587

  SmtpServer.Port = 587; 

而且不要忘了啓用您的Gmail帳戶的POP & IMAP訪問,去設置,可供選擇的選項卡「轉發和POP/IMAP」,並啓用POP & IMAP

+0

是的,我試過了。但結果仍然是一樣的。無法發送郵件。 –

+0

請幫忙。它現在工作 –