如何从阅读的WinForms收件箱中的邮件Outlook.com? [英] How to read inbox Outlook.com mails from WinForms?

查看:204
本文介绍了如何从阅读的WinForms收件箱中的邮件Outlook.com?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过开发或C#编写的一个非常简单的应用程序Vb.Net 的WinForms 技术,这将有助于我实现自动化简单的任务,包括在获得我的 Outlook.com 帐户读取收到我的电子邮件的的Youtube ,然后提取视频网址。

I would like to develop a very simple app written in C# or Vb.Net using WinForms tech., that will help me to automate a simple task that consist in access to my Outlook.com account to read my emails received from Youtube then extract the video urls.

我的网络的相关知识都不好,我被堵在最重要的一点,试图找到acchieve该任务(我的意思是.NET或第三方API或其他方式微软官方的API,能够做到这一点)最简单的方法,试图应用需要的的OAuth2 autorizathion访问电子邮件帐户。

My networking related knowledges are not good, I'm stuck at the most important point trying to find the easiest way to acchieve that task (I mean official Microsoft APIs for .Net or 3rd party APIs or other way to be able do this), trying to apply the required OAuth2 autorizathion to access the email account.

我知道下面的代码不集中在正确的方向,因为缺乏授权,但我不知道如何实现,无论是如何读取电子邮件,所以这是我的尝试:

I know that the following code is not focused in the right direction because the lack of authorization, but I don't know how to implement that neither how to read the emails, so this is what I tried:

string url = "https://outlook.office.com/api/v2.0/me/messages";
string result = "";

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";

try {
    using (WebClient wc = new WebClient()) {
        wc.Encoding = Encoding.UTF8;
        result = wc.DownloadString(url);
    }

} catch (Exception ex) {
    MessageBox.Show(ex.Message);

}



问题






我怎么能访问我的 Outlook.com 帐户读取标题和收件箱中的邮件,我有内容?而且,另外而是选择性响应(仅如果可能的话,并没有过多的要求),我怎么能删除的电子邮件?

QUESTION


How I could access to my Outlook.com account to read the title and the content of the inbox emails that I have?. And, additionally but optionally to respond (only if possible and is not too much ask), how I could delete a email?.

请注意,这个问题reffers为 Outlook.com 在线服务,而不是展望的桌面客户端既不是他们的 COM 的库的使用或 Office365

Note that this question reffers to Outlook.com online service, not to Outlook's desktop client neither the usage of their COM libraries or Office365.

我知道我没有人寻求帮助,并把一些必要条件,所有类型的帮助表示赞赏我,但这个时候,我需要把一个特殊的必要,因为我的脑袋疯了试图理解,使用和修改的的OAuth2 解决方案这是从头开始做,它会产生很长的代码,我不明白的话,那是对我来说太多。

I know that I'm no one to ask for help and put some requisites, all kind of help is appreciated for me, but this time I need to put a special requisite, because my head got crazy trying to understand, use and adapt OAuth2 solutions that were made from scratch, it generates very long codes that I don't understand at all, it's too much for me.

出于这个原因,我会接受一个答案在这个问题只有在提供的解决方案是基于在第三pary库的使用,将有助于这一切的任务,因为它将作为在的OAuth2 实施一个完整的抽象,例如 RestSharp CodeScales DotNetOpenAuth 或任何其他(免费)LIB将处理所需要的东西为我而不是需要我自己从头开始开发的的OAuth2 算法

For that reason, I will accept an answer in this question only if the provided solution is based in the usage of a 3rd pary library that will facilitate all this task because it will serve as a complete abstraction of the OAuth2 implementation, like RestSharp, CodeScales or DotNetOpenAuth, or whichever other (free)lib that will handle the required things for me instead of the need to develop the OAuth2 algorithms by myself from scratch.

我的调查开始读这个 微软的Outlook开发。页面,以下为 邮件API参考, REST API文档,这个 Outlook.com API,这个 样的入门文章,并在这个 使用ASP.Net充分说明性的例子。

My investigation started reading this Microsoft's Outlook dev. page, following to this Mail API reference, this REST API documentation, this Outlook.com API, this kind of getting started article, and ending in this fully illustrative example using ASP.Net.

我在明确采取从微软的文章只是......没什么没什么,我已经注册了应用程序并创建了客户端ID和秘密,但微软并没有为提供任何例如的WinForms 让我尝试没有成功其官方的 ASP.NET 示例翻译为的WinForms

What I have taken in clear from the Microsoft articles is just... nothing of nothing, I've registered the app and created the client id and the secret, but Microsoft does not provide any example for Winforms so I tried to translate their official ASP.NET example to WinForms without success.

时间浪费后,我发现的 的OAuth 文档页面提供了一些库.NET,我想象将促进的OAuth2授权,后来我发现在 DotNetOpenAuth 库,这似乎非常完整的,也是我发现的 对Outlook 代码示例 DotNetOpenAuth ,但它再次为 ASP.NET ,也这些 通用和官方的 DotNetOpenAuth 的代码示例,但我不能。寻找能够帮助我什么我想要做的任何事情。

After that waste of time, I found this OAuth documentation page which provides some libraries for .NET that I imagine will facilitate the OAuth2 authorization, then I discovered the DotNetOpenAuth library which seems very complete, and also I found this code sample for Outlook using DotNetOpenAuth but again it is for ASP.NET, and also these generic and official DotNetOpenAuth's code samples but I can't find any thing that could help me for what I want to do.

推荐答案

总的想法是在这里按照教程:< A HREF =https://dev.outlook.com/restapi/tutorial/dotnet相对=nofollow>开始使用电子邮件,日历和联系人REST API的的,但我会尽量简化它,并证明它有一个WinForms样本。

The general idea is to follow the tutorial here: Get Started with Mail, Calendar, and Contacts REST APIs but I will try to simplify it and demonstrate it with a Winforms sample.

首先第一件事情,你需要创建和注册应用程序到应用程序注册门户(你应该只需要对课程给定的应用做一次):

First things first, you need to create and register an application to the Application Registration Portal (you should only have to do this once for a given application of course):


  1. 创建一个应用程序

  2. 生成新密码

  3. 添加了一个平台,并选择移动(手机在这里的意思是任何设备或不是一个浏览器.. 。)

  4. 请不要忘记点击保存!

  1. create an app
  2. generate a new password
  3. add a platform, and choose mobile ("mobile" here means "any device", or "not for a browser"...)
  4. don't forget to click on save!

现在,在您的任何设备的代码(包括WinForms的),你需要进行身份验证。最简单的方法是使用ADAL(Active Directory验证库)。该人士可以在这里 https://github.com/AzureAD/azure-activedirectory-库换DOTNET 和二进制可作为的NuGet。

Authentication

Now, in your "any device" code (including winforms), you'll need to authenticate. The simplest way is to use ADAL ("Active Directory Authentication Library"). The source is available here https://github.com/AzureAD/azure-activedirectory-library-for-dotnet and the binary is available as a nuget.

不幸的是,你可以得到的NuGet最新版本命名为今天的微软.IdentityModel.Clients.ActiveDirectory 不为我工作(它有我在这里报告了错误的 https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/412 这已经被修复,但现在的服务器抱怨一些应用。VS服务器不兼容)

Unfortunately, the latest version which you can get on nuget today named Microsoft.IdentityModel.Clients.ActiveDirectory does not work for me (It had a bug I've reported here https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/412 that's already been fixed, but now the server complains about some app vs server incompatibility).

所以,你必须使用旧的实验一(牢记这一点在未来的某一天作为,我们将不得不切换):< A HREF =https://www.nuget.org/packages/Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory/相对=nofollow> Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory

So you must use the old "Experimental" one (keep that in mind as someday in the future, we'll have to switch): Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory

您要确保当你获得一个身份验证令牌使用正确的作用域。作用域定义如下: Outlook邮件,日历和联系人作用域以及表示许可的区域。 。如果不指定范围,你可以什么也不做,但验证

You want to make sure you use the proper scopes when you do acquire an auth token. Scopes are defined here: Outlook mail, calendar, and contacts scopes and represent an area of permission. Without specifying scope, you can do nothing else but authenticate.

所以,如果你想读邮件,使用的 https://outlook.office.com/mail.read 范围。

So, if you want to read mails, use the "https://outlook.office.com/mail.read" scope.

当您尝试应用程序时,身份验证对话框后,它应该显示给用户的同意画面(在这里我们看到,我们要求邮寄范围:阅读邮件):

When you try the application, after the authentication dialog, it should display to the user the consent screen (here we see we asked for mail scope: "Read Your Mail"):

一旦认证作品中,您可以直接使用的REST API这是不那么容易,还是偷懒,使用另一个包:的 Microsoft.Office365.OutlookServices-V2.0 ,会做所有底层REST / OData的魔术给你。好消息是,这个API是相当完整的,所以应该让你做其他事情一样,消息的创建,删除等。

Once authentication works, you can use the REST api directly which is not that easy, or be lazy and use another package: Microsoft.Office365.OutlookServices-V2.0 that will do all underlying REST/OData magic for you. The good news is this API is quite complete so it should allow you to do other things like, message creation, delete, etc.

有对于后市的重要言论.COM案例:不是所有的帐户都该全REST API启用(勾选REST API可用性一章中的位置:的 Outlook邮件),所以你可能要创建用于测试一个新的。

There is an important remark for the outlook.com case: not all accounts are enabled for this whole REST API (check the "REST API availability" chapter here: Outlook Mail), so you might want to create a new one for testing.

下面是一个示例应用程序,将查询10的WinForms代码。信息并将它们添加到列表框

Here is the winforms code for a sample app that will query 10 messages and add them to a listbox.

using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Office365.OutlookServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private const string authority = "https://login.microsoftonline.com/common";
        private const string clientId = "blablabl-abla-blab-abla-blablablab"; // TODO: put your application id here
        private const string redirectUri = "urn:ietf:wg:oauth:2.0:oob"; // put your redirect uri here (should be the same)

        // we cache the token for the duration of this form
        // you could/should use the FileCache class provided in the sample here https://dev.outlook.com/restapi/tutorial/dotnet
        private TokenCache _cache = new TokenCache();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // since all packages force async,
            // we have to avoid threading issues
            BeginInvoke((Action)(() => GetMessages()));
        }

        private async void GetMessages()
        {
            // use the Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory nuget package for auth
            var authContext = new AuthenticationContext(authority, _cache);
            var result = await authContext.AcquireTokenAsync(
                new[] { "https://outlook.office.com/mail.read" },
                null,
                clientId,
                new Uri(redirectUri),
                new PlatformParameters(PromptBehavior.Always, this));

            // use the Microsoft.Office365.OutlookServices-V2.0 nuget package from now on
            var client = new OutlookServicesClient(new Uri("https://outlook.office.com/api/v2.0"), () => Task.FromResult(result.Token));

            var messages = await client.Me.Messages
                                        .Take(10) // get only 10 messages
                                        .ExecuteAsync();

            // fill some list box
            // (beware, some messages have a null subject)
            foreach (var msg in messages.CurrentPage)
            {
                listBox1.Items.Add(msg.Subject);
            }
        }
    }
}

这篇关于如何从阅读的WinForms收件箱中的邮件Outlook.com?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆