2012-02-09 85 views
2

我想從C#winform應用程序發送電子郵件,我的Internet連接使用代理。使用CDO通過代理髮送電子郵件

這是我迄今所做

 WebProxy proxy = WebProxy.GetDefaultProxy(); 
     Console.WriteLine(proxy.Address); 
     if (proxy.Address!=null) 
     { 
      try 
      { 
       MailMessage oMsg = new MailMessage(); 
       // TODO: Replace with sender e-mail address. 
       oMsg.From = fromGmailAddress; 
       // TODO: Replace with recipient e-mail address. 
       oMsg.To = toAddress; 
       oMsg.Subject = "Send Using Web Mail"; 

       // SEND IN HTML FORMAT (comment this line to send plain text). 
       oMsg.BodyFormat = MailFormat.Html; 

       // HTML Body (remove HTML tags for plain text). 
       oMsg.Body = "<HTML><BODY><B>Hello World!</B></BODY></HTML>"; 
       oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465); 
       oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com"); 
       oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2); 
       oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/urlproxyserver", proxy.Address.Host); 

       oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/proxyserverport", proxy.Address.Port); 
       oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true); 

       oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); 
       oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", gmailUsername); 
       oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", gmailPassword); 

       SmtpMail.SmtpServer.Insert(0,"smtp.gmail.com"); 
       SmtpMail.Send(oMsg); 

       oMsg = null; 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("{0} Exception caught.", e); 
      } 

它拋出一個異常,運輸未能連接到服務器。 我已經嘗試過465,587和25端口的Gmail的SMTP服務器。什麼都沒有

我一直在通過網絡閱讀有關代理服務器可能無法啓用電子郵件發送的可能性。如果我閱讀或理解錯誤,請糾正我的錯誤?由於此代理允許我在使用瀏覽器時登錄gmail帳戶。

任何幫助將不勝感激。

問候

+0

我沒想到網絡代理通常支持的非HTTP通信。你確定你不想在這裏使用SOCKS代理嗎? – 2012-02-09 15:06:41

+0

@ M.Babcock任何指向該教程的鏈接?我怎樣才能做到這一點? – nightWatcher 2012-02-09 15:11:35

+0

可能的重複[是否有.NET庫通過PROXY發送電子郵件?](http://stackoverflow.com/questions/3156753/is-there-net-library-for-email-sending-via-proxy) – 2012-02-09 15:16:26

回答

1

//使用了以下功能,將背後的代理工作,即使

using System.Net.Mail; 
public int SendMailUsingGMAIL(string fromAddress, string toAddress, string tocc, string mailsubject, string msgContent, string strAttachment, bool isBodyHTML) 
{ 
    int retvar = 0; 
    try 
    { 

     MailMessage mailMessage = new MailMessage(new MailAddress(fromAddress) 
              , new MailAddress(toAddress)); 

     mailMessage.Subject = mailsubject; 
     mailMessage.IsBodyHtml = isBodyHTML; 
     mailMessage.Body = msgContent; 
     if (tocc != "") 
     { 
      mailMessage.CC.Add(tocc); 
     } 
     System.Net.NetworkCredential networkCredentials = new 
     System.Net.NetworkCredential("smtpUserName", "smtpPassword");//key="smtpUserName" value="[email protected]";key="smtpPassword" value="your password" 
     SmtpClient smtpClient = new SmtpClient(); 
     smtpClient.EnableSsl = true; 
     smtpClient.UseDefaultCredentials = false; 
     smtpClient.Credentials = networkCredentials; 
     smtpClient.Host = "smtp.gmail.com"; 
     smtpClient.Port = 587; 
     smtpClient.Send(mailMessage); 

    } 
    catch (Exception ex) 
    { 

     retvar = -1; 

    } 


    return retvar; 

} 
+0

nopes,它不適合我的工作:( – nightWatcher 2012-02-14 10:38:02

+0

什麼異常你得到PLZ份額! – Zia 2012-02-15 15:04:01

相關問題