Send an Email Through the Outlook Desktop App in C#

If the user already has Outlook set up on their PC, your program can hand a message to it instead of talking to a mail server. Outlook sends it with the user’s own account and signature, and the message lands in their Sent Items folder. This is the usual approach for desktop tools that “email this report”, and it can either send silently or open the message so the user can review it first.

This uses COM interop, so it is Windows-only and needs the classic Outlook for Windows installed. The new Outlook for Windows (and Outlook on the web or Mac) has no COM object model, so this code cannot drive it.

Project setup

Target Windows in the project file:

<TargetFramework>net8.0-windows</TargetFramework>

Then add a reference to the Outlook type library. In Visual Studio, right-click Dependencies, choose Add COM Reference and tick “Microsoft Outlook 16.0 Object Library” (Outlook 2016, 2019, 2021 and Microsoft 365 all register as 16.0). This adds a <COMReference> item to the project. Note that dotnet build on the command line cannot resolve COM references; build from Visual Studio or with msbuild.

The code

using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;

public enum MailSendMode
{
    SendNow,
    ShowModal,
    ShowModeless
}

public static class OutlookMailer
{
    public static void Send(
        string to,
        string subject,
        string htmlBody,
        IEnumerable<string> attachmentPaths,
        MailSendMode mode = MailSendMode.SendNow)
    {
        var app = new Outlook.Application();
        var mail = (Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);
        try
        {
            mail.To = to;
            mail.Subject = subject;
            mail.HTMLBody = htmlBody;

            foreach (string path in attachmentPaths)
            {
                // Outlook needs an absolute path.
                mail.Attachments.Add(Path.GetFullPath(path));
            }

            switch (mode)
            {
                case MailSendMode.SendNow:
                    mail.Send();
                    break;
                case MailSendMode.ShowModal:
                    mail.Display(true);
                    break;
                case MailSendMode.ShowModeless:
                    mail.Display(false);
                    break;
            }
        }
        finally
        {
            Marshal.ReleaseComObject(mail);
            Marshal.ReleaseComObject(app);
        }
    }
}

Usage:

OutlookMailer.Send(
    to: "colleague@example.com; manager@example.com",
    subject: "Weekly figures",
    htmlBody: "<p>Hi,</p><p>The figures for this week are attached.</p>",
    attachmentPaths: [@"C:\reports\weekly.xlsx"],
    mode: MailSendMode.ShowModeless);

To takes a single string with recipients separated by semicolons, just like the To box in Outlook. Use CC and BCC the same way.

Choosing a send mode

ShowModeless opens the message in a normal Outlook window and returns at once. The user can edit it, add recipients and click Send themselves. For most desktop tools this is the friendliest option, and it sidesteps the security prompt described below.

ShowModal blocks your code until the user closes the message window. SendNow puts the message straight into the Outbox; Outlook sends it on its next send/receive, so it may not leave immediately if Outlook was not already running.

Traps

Security prompts. When code calls Send or reads addresses, Outlook’s object model guard may show “A program is trying to send an e-mail message on your behalf”. Outlook suppresses the prompt when it detects up-to-date antivirus software, which is why the same code prompts on one machine and not another. Administrators can also control it through Group Policy. If your users see the prompt, switch to Display.

Outlook is single-instance. new Outlook.Application() attaches to the running Outlook if there is one. Do not call app.Quit() at the end, or you will close the user’s Outlook under them.

Releasing COM objects. Each COM object you touch holds a reference in Outlook until the .NET runtime collects its wrapper. If Outlook stays in Task Manager after the user closes it, unreleased references are usually the reason. Marshal.ReleaseComObject on the objects you hold, as above, is normally enough for a short-lived call like this.

Bitness does not matter. Outlook runs as a separate process, so a 64-bit app can drive 32-bit Outlook and the other way round. This is different from the Access and Excel data providers, where it matters a lot.

Not for servers. Microsoft does not support Office automation from services, ASP.NET or scheduled tasks running without a logged-on user. Outlook may hang on a dialog that no one can see. On a server, send through SMTP or Microsoft Graph instead.

Related examples

1 thought on “Send an Email Through the Outlook Desktop App in C#”

  1. I am genuinely thankful to the owner of this website for sharing his brilliant ideas. I can see how much you’ve helped everybody who comes across your page. By the way, here is my webpage Webemail24 about Pain Management.

    Reply

Leave a Comment