Outlook VSTO C# Make post to html url

2019-06-03 07:25发布

I am making a plugin for Outlook 2010 using VS2010 C#.

The objective of my plugin is to grab the To, CC, BC from the new email when a custom button is pressed from the ribbon and post it to an external url (which takes in a post request). Similar to how forms in html/jsp can post inputs to a different page(url).

So far, I can grab the To,CC,BC and store it in a string variable. But I don't know how to make a post to the external url.

Any help would be greatly appreciated. Thanks.

Here is my code for my function so far:

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.
        }
    }
}

1条回答
爷的心禁止访问
2楼-- · 2019-06-03 07:30

You need to use WebRequest / HttpWebRequest class, for example:

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;
查看更多
登录 后发表回答