In this section you will add the ability to send new emails from the user's mailbox.
-
Open ./Graph/GraphHelper.cs and add the following function to the GraphHelper class.
public static async Task SendMessageAsync(string subject, string body, string recipientEmail) { try { // GET /me await graphClient!.Me.SendMail(new Message { Subject = subject, Body = new ItemBody() { Content = body, ContentType = BodyType.Text }, ToRecipients = new List<Recipient>() { new Recipient { EmailAddress = new EmailAddress { Address = recipientEmail } } } }) .Request() .PostAsync(); } catch (ServiceException ex) { Console.WriteLine($"Error getting signed-in user: {ex.Message}"); } }
This code initializes an Message object and uses the Graph SDK to send it to the a specified email address.
-
Add the following just after the
// Send mail
comment in theProgram.cs
file.var currentUser = await GraphHelper.GetMeAsync(); await GraphHelper.SendMessageAsync("Hello from Microsoft Graph 🦒", "Welcome to the amazing world of Graph!", currentUser!.Mail); Console.WriteLine($"Sent email as the signed in user\n");
-
Save all of your changes and run the app. Choose the Send an email as the signed in user option. You can now see the email was sent in your Sent items folder.