Send Email Messages in Microsoft 365 Server

The example below shows how you can use the GemBox.Email component to connect to the Microsoft 365 (Office 365) Server and send an email from your console application by using C# or VB.NET code.

using GemBox.Email;
using GemBox.Email.Graph;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create a new email message.
        var message = new MailMessage(
            new MailAddress("sender@example.com", "Sender"),
            new MailAddress("first.receiver@example.com", "First receiver"),
            new MailAddress("second.receiver@example.com", "Second receiver"));

        // Add additional receivers.
        message.Cc.Add(
            new MailAddress("third.receiver@example.com", "Third receiver"),
            new MailAddress("fourth.receiver@example.com", "Fourth receiver"));

        // Add subject and body.
        message.Subject = "Send Email in C# and VB.NET";
        message.BodyText = "Hi 👋,\n" +
            "This message was created and sent with GemBox.Email.\n" +
            "Read more about it on https://www.gemboxsoftware.com/email";

        // Create a new Graph client and send an email message.
        var graphClient = new GraphClient();
        graphClient.Authenticate("<OAUTH2.0-TOKEN>");
        graphClient.ImpersonateUser("<USER-ID-OR-EMAIL>");
        graphClient.SendMessage(message);
    }
}
Imports GemBox.Email
Imports GemBox.Email.Graph

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Create a new email message.
        Dim message As New MailMessage(
            New MailAddress("sender@example.com", "Sender"),
            New MailAddress("first.receiver@example.com", "First receiver"),
            New MailAddress("second.receiver@example.com", "Second receiver"))

        ' Add additional receivers.
        message.Cc.Add(
            New MailAddress("third.receiver@example.com", "Third receiver"),
            New MailAddress("fourth.receiver@example.com", "Fourth receiver"))

        ' Add subject and body.
        message.Subject = "Send Email in C# and VB.NET"
        message.BodyText = "Hi 👋," & vbLf &
            "This message was created and sent with GemBox.Email." & vbLf &
            "Read more about it on https://www.gemboxsoftware.com/email"

        ' Create a new Graph client and send an email message.
        Dim graphClient = New GraphClient()
        graphClient.Authenticate("<OAUTH2.0-TOKEN>")
        graphClient.ImpersonateUser("<USER-ID-OR-EMAIL>")
        graphClient.SendMessage(message)

    End Sub

End Module
Email message sent with GemBox.Email via Microsoft 365 Server
Screenshot of an email message sent with GemBox.Email through the Microsoft 365 Server

GemBox.Email supports communication with the Microsoft 365 Server, when using the Microsoft Graph API.

Send an email with an attachment in Microsoft 365

The following example shows how you can use GemBox.Email to send email with attachments using Microsoft Graph.

using GemBox.Email;
using GemBox.Email.Graph;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create new email message.
        var message = new MailMessage("sender@example.com", "receiver@example.com");

        // Add subject.
        message.Subject = "Send HTML Email with Image and Attachment";

        // Add HTML body with CID embedded image.
        string cid = "image001@gembox.com";
        message.BodyHtml = "<html><body>" +
            "<p>Hi 👋,</p>" +
            "<p>This message was created and sent with:</p>" +
            "<p><img src='cid:" + cid + "'/></p>" +
            "<p>Read more about it on <a href='https://www.gemboxsoftware.com/email'>GemBox.Email Overview</a> page.</p>" +
            "</body></html>";

        // Add image as an inline attachment.
        message.Attachments.Add(new Attachment("%#GemBoxEmailLogo.png%") { ContentId = cid });

        // Add PDF as an attachment.
        message.Attachments.Add(new Attachment("%#GemBoxSampleFile.pdf%"));

        // Create a new Graph client and send an email message.
        var graphClient = new GraphClient();
        graphClient.Authenticate("<OAUTH2.0-TOKEN>");
        graphClient.ImpersonateUser("<USER-ID-OR-EMAIL>");
        graphClient.SendMessage(message);
    }
}
Imports GemBox.Email
Imports GemBox.Email.Graph

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Create new email message.
        Dim message = New MailMessage("sender@example.com", "receiver@example.com")

        ' Add subject.
        message.Subject = "Send HTML Email with Image and Attachment"

        ' Add HTML body with CID embedded image.
        Dim cid = "image001@gembox.com"
        message.BodyHtml = "<html><body>" &
            "<p>Hi 👋,</p>" &
            "<p>This message was created and sent with:</p>" &
            "<p><img src='cid:" & cid & "'/></p>" &
            "<p>Read more about it on <a href='https://www.gemboxsoftware.com/email'>GemBox.Email Overview</a> page.</p>" &
            "</body></html>"

        ' Add image as inline attachment.
        message.Attachments.Add(New Attachment("%#GemBoxEmailLogo.png%") With {.ContentId = cid})

        ' Add file as attachment.
        message.Attachments.Add(New Attachment("%#GemBoxSampleFile.pdf%"))

        ' Create a new Graph client and send an email message.
        Dim graphClient = New GraphClient()
        graphClient.Authenticate("<OAUTH2.0-TOKEN>")
        graphClient.ImpersonateUser("<USER-ID-OR-EMAIL>")
        graphClient.SendMessage(message)

    End Sub

End Module
Email message sent with GemBox.Email via Microsoft 365 Server
Screenshot of an email message with an attachment sent with GemBox.Email through the Microsoft 365 Server

See also


Next steps

GemBox.Email is a .NET component that enables you to read, write, receive, and send emails from your .NET applications using one simple API.

Download Buy