Upload an Email Message with Exchange

The example below shows how you can use the GemBox.Email component to connect to the Exchange Server and upload an email message from a C# console application.

using GemBox.Email;
using GemBox.Email.Imap;
using System;
using System.Collections.Generic;

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

        // Create new email message.
        MailMessage 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 = "Upload Email in C# / VB.NET / ASP.NET";
        message.BodyText = "Hi 👋,\n" +
            "This message was created and uploaded with GemBox.Email.\n" +
            "Read more about it on https://www.gemboxsoftware.com/email";

        // Create a new Exchange client and upload an email message to the Inbox folder.
        var exchangeClient = new ExchangeClient("<HOST> (e.g. https://outlook.office365.com/EWS/Exchange.asmx)");
        exchangeClient.Authenticate("<USERNAME>", "<PASSWORD>");
        exchangeClient.UploadMessage(message, "Inbox");
    }
}
Imports GemBox.Email
Imports GemBox.Email.Imap

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 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 = "Upload Email in C# / VB.NET / ASP.NET"
        message.BodyText = "Hi 👋," & vbLf &
                "This message was created and uploaded with GemBox.Email." & vbLf &
                "Read more about it on https://www.gemboxsoftware.com/email"

        ' Create a new Exchange client and upload an email message to the Inbox folder.
        Dim exchangeClient = New ExchangeClient("<HOST> (e.g. https://outlook.office365.com/EWS/Exchange.asmx)")
        exchangeClient.Authenticate("<USERNAME>", "<PASSWORD>")
        exchangeClient.UploadMessage(message, "Inbox")
    End Sub
End Module
Screenshot of an email message uploaded to Exchange folder
Screenshot of an email message uploaded to Exchange folder

You can use the ExchangeClient.UploadMessage method to save the email message in the desired folder. Note that this method will not send the email message to its recipients. If you are looking for that, check out our sending example.

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