//  
//   Rebex Sample Code License
// 
//   Copyright 2026, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using Rebex.Net;

namespace Rebex.Samples
{
    class Program
    {
        static int Main(string[] args)
        {
            // set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key;


            const int MaxMessageCount = 20;

            // Please note that before you can access Microsoft 365 mailboxes using this sample
            // (or your own application), you have to:
            //
            // 1. Register your application at Microsoft Azure Portal.
            // 2. Retrieve a suitable OAuth 2.0 access token to authorize the Graph client connection.
            //
            // These steps are described in details in the following articles:
            // https://blog.rebex.net/oauth2-office365-rebex-mail
            // https://blog.rebex.net/office365-ews-oauth-unattended
            string accessToken = "TODO";

            // connect to a mailbox at Microsoft 365 server and retrieve list of messages
            using (var client = new GraphClient())
            {
                // debug log (enable if needed)
                //client.LogWriter = new FileLogWriter("graph.log", LogLevel.Debug);

                // connect to the server (https://graph.microsoft.com is used by default)
                Console.WriteLine("Connecting...");
                client.Connect();

                // authenticate using the OAuth 2.0 access token
                Console.WriteLine("Authenticating...");
                client.Login(accessToken);

                // get list of recent messages in the 'Inbox' folder
                Console.WriteLine("Listing folder contents...");
                GraphMessageCollection messageList = client.GetMessageList(
                    GraphFolderId.Inbox,
                    GraphMessageFields.Default,
                    new GraphPageView(0, MaxMessageCount));

                // write message list to the console
                foreach (GraphMessageInfo message in messageList)
                {
                    Console.WriteLine("{0:yyyy-MM-dd} {1}: {2}",
                        message.ReceivedDate, message.From, message.Subject);
                }
            }

            return 0;
        }
    }
}
