public async Task <bool> SendEmail()  // string toEmail, string subject, string body)
        {
            var graphClient = new GraphClient();

            var message = new Message
            {
                Subject    = "Hello Test Email",// subject,
                Importance = Importance.Low,
                Body       = new ItemBody
                {
                    ContentType = BodyType.Html,
                    Content     = "Hello World" //body
                },
                ToRecipients = new List <Recipient>()
                {
                    new Recipient
                    {
                        EmailAddress = new EmailAddress
                        {
                            Address = "*****@*****.**"// toEmail
                        }
                    }
                }
            };

            var uri      = "https://graph.microsoft.com/v1.0/me/sendMail";
            var sendable = new Sendable()
            {
                Message = message,
                //saveToSentItems = false
            };
            var json = JsonConvert.SerializeObject(sendable);
            var data = new StringContent(json, Encoding.UTF8, "application/json");

            HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ReadWriteEmailToken);

            var getResult = await HttpClient.PostAsync(uri, data);

            return(true);
        }
Example #2
0
        static void Main(string[] args)
        {
            var  graphClient    = new GraphClient();
            bool anotherRequest = true;

            while (anotherRequest)
            {
                Console.WriteLine("Choose a number:");
                Console.WriteLine("1. Get your info from Microsoft");
                Console.WriteLine("2. Get your last 10 emails");
                Console.WriteLine("3. Write an email");
                ConsoleKeyInfo choice = Console.ReadKey();
                Console.WriteLine();
                Console.WriteLine();
                switch (choice.KeyChar)
                {
                case '1':
                    Task <string> users = graphClient.GetThisUser();
                    users.Wait();
                    Console.WriteLine(users.Result);
                    Console.WriteLine();
                    Console.WriteLine();
                    break;

                case '2':
                    Task <List <SimpleEmail> > emails = graphClient.GetTenEmails();
                    emails.Wait();
                    foreach (SimpleEmail email in emails.Result)
                    {
                        Console.WriteLine("Subject:   " + email.Subject);
                        Console.WriteLine();
                        Console.WriteLine("From:      " + email.FromAddress);
                        Console.WriteLine();
                        Console.WriteLine("Date:      " + email.Date);
                        Console.WriteLine();
                        Console.WriteLine("Email ID:  " + email.Id);
                        Console.WriteLine();
                        Console.WriteLine(email.Body);
                        Console.WriteLine();
                        Console.WriteLine();
                    }

                    Console.ReadLine();
                    Console.WriteLine();
                    Console.WriteLine();
                    break;

                case '3':
                    //Console.Write("To? (enter a valid email address?)");
                    //string toEmail = Console.ReadLine();
                    //Console.Write("Subject?");
                    //string subject = Console.ReadLine();
                    //Console.WriteLine("Message body?");
                    //string body = Console.ReadLine();
                    Task <bool> draftCreated = graphClient.SendEmail();
                    Console.WriteLine();
                    Console.WriteLine();
                    break;
                }
            }
        }