2013-07-10 57 views
1

我正在爲使用VS2010 C#的Outlook 2010製作一個插件。展望VSTO C#發佈到HTML網址

我的插件的目標是當從功能區按下 自定義按鈕並將其發佈到外部URL(其中 接受發佈請求)時,從新電子郵件中獲取To,CC和BC。類似於html/jsp中的表單如何將輸入發佈到 不同的頁面(url)。

到目前爲止,我可以抓住To,CC,BC並將它存儲在一個字符串變量中。但我不知道如何發佈到外部網址的 。

任何幫助將不勝感激。謝謝。

這裏是我的代碼爲我的功能至今:

public void makePost(object Item, ref bool Cancel) 
{ 
    Outlook.MailItem myItem = Item as Outlook.MailItem; 

    if (myItem != null) 
    { 
     string emailTo = myItem.To; 
     string emailCC = myItem.CC; 
     string emailBCC = myItem.BCC; 

     if (emailTo == null && emailCC == null && emailBCC == null) 
     { 
      MessageBox.Show("There are no recipients to check."); 
     } 
     else 
     { 
      string emailAdresses = string.Concat(emailTo, "; ", emailCC, "; ", emailBCC); 

      //do something here to post the string(emailAddresses) to some url. 
     } 
    } 
} 

回答

2

您需要使用WebRequest/HttpWebRequest類,例如:

HttpWebRequest request = HttpWebRequest.Create("http://google.com/postmesmth") as HttpWebRequest; 
request.Method = WebRequestMethods.Http.Post; 
request.Host = "google.com"; 
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"; 
string data = "myData=" + HttpUtility.UrlEncode("Hello World!"); 


StreamWriter writer = new StreamWriter(request.GetRequestStream()); 
writer.Write(data); 
writer.Close(); 

HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
+0

非常感謝!我明白了。但我在一個問題上運行。我在行 'string data = HttpUtility.UrlEncode(emailAdresses);' 它說HttPUtility不存在於當前的上下文中。我做了許多進口,包括:使用System.Net;使用System.Web;仍然沒有運氣。 謝謝 – Polzi

+0

您需要添加對System.Web.dll的引用或使用:Uri.EscapeUriString(「Hello World!」);而不是 – tinamou

+0

謝謝。 Uri.EscapeUriString工作。 現在我收到一行異常錯誤: 'HttpWebResponse response = request.GetResponse()as HttpWebResponse;' 調試器顯示響應獲得空值。可能與我發佈它的HTML頁面有關。 謝謝。 – Polzi