Forum Discussion
Create and send an email programmatically with the new Outlook
There is limited VSTO support for Outlook, please refer on C#:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
class Program
{
private static async Task Main(string[] args)
{
var accessToken = "YOUR_ACCESS_TOKEN"; // Obtain this from the OAuth2 flow
var graphClient = new HttpClient();
graphClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var email = new JObject
{
["message"] = new JObject
{
["subject"] = "Test Email with Attachment",
["body"] = new JObject
{
["contentType"] = "Text",
["content"] = "This is a test email with an attachment."
},
["toRecipients"] = new JArray
{
new JObject
{
["emailAddress"] = new JObject
{
["address"] = "email address removed for privacy reasons"
}
}
},
["attachments"] = new JArray
{
new JObject
{
["@odata.type"] = "#microsoft.graph.fileAttachment",
["name"] = "test.txt",
["contentBytes"] = Convert.ToBase64String(System.IO.File.ReadAllBytes("path/to/your/file.txt"))
}
}
},
["saveToSentItems"] = "true"
};
var response = await graphClient.PostAsync("https://graph.microsoft.com/v1.0/me/sendMail", new StringContent(email.ToString(), System.Text.Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Email sent successfully.");
}
else
{
Console.WriteLine($"Error sending email: {response.ReasonPhrase}");
}
}
}
Kidd_Ip
Thanks for the answer. What I would llike to do is to have the email open in the new outlook, with some text and an attachement already added, so that the user can modify it before sending it. I don't want/need to obtain access token. Is there a way?