Forum Discussion

FrancoisBard's avatar
FrancoisBard
Brass Contributor
Feb 21, 2024

Create and send an email programmatically with the new Outlook

Hello,

I'm maintaining an application that relies on VSTO to interface with PowerPoint, Excel, Word and Outlook. With the new Outlook desktop app, creating and sending simple emails with VSTO (link) is now impossible.

How can I create an email with attachment and display it to the user that has the new outlook?

I found a way to detect if the new outlook is installed (registry key) so I'm happy with a solution that works only with the new outlook.

  • PeterFriberg's avatar
    PeterFriberg
    Copper Contributor

    Create an .eml file including emailTo, Subject, Body and attachments. Then run olk.exe myEmailfile.eml

  • PeterFriberg's avatar
    PeterFriberg
    Copper Contributor

    Create an .eml file including emailTo, Subject, Body and attachments. Then run olk.exe myEmailfile.eml

  • FrancoisBard 

     

    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}");
    }
    }
    }

    • FrancoisBard's avatar
      FrancoisBard
      Brass Contributor

      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?

  • Brian_Grigg's avatar
    Brian_Grigg
    Copper Contributor

    FrancoisBard Love that after 9 months you have over 1k views but not one response. I'm curious if you've found a resolution?

Resources