Rebex Secure Mail

SMTP, IMAP, EWS, POP3, S/MIME .NET library

Download 30-day free trial Buy from $299
More .NET libraries

Back to feature list...

EWS - sending e-mails

The sample code on this page assumes you have already connected and authenticated to an Exchange server.

Sending a simple mail message 

To send a simple mail message, use one of Ews.SendMessage method overloads.

// create EWS client instance, connect, log in
var ews = new Rebex.Net.Ews();
ews.Connect(hostname);
ews.Login(username, password);

// send plain text message (to/subject/body)
ews.SendMessage("john@example.org", "Test 1", "Hello world!");

// send HTML message (to/subject/body)
ews.SendMessage("john@example.org", "Test 2",
    new EwsMessageBody("<strong>Hello world!</strong>", EwsBodyFormat.Html));
' create EWS client instance, connect, log in
Dim ews = New Rebex.Net.Ews()
ews.Connect(hostname)
ews.Login(username, password)

' send plain text message (to/subject/body)
ews.SendMessage("john@example.org", "Test 1", "Hello world!")

' send HTML message (to/subject/body)
ews.SendMessage("john@example.org", "Test 2", _
    New EwsMessageBody("<strong>Hello world!</strong>", EwsBodyFormat.Html))

Note: You don't need to specify the 'From' address unless you are sending e-mail on behalf of another user. The server will add the field for you.

Note: By default, sent messages are stored to the "Sent Items" folder at the Exchange server. To store the message into a different folder, specify desired folder ID using the optional second argument. Alternatively, to send the message without storing it, specifynull (Nothing in VB.NET) as the folder ID.

Sending a mail message 

To send a more complex mail message, create an instance of MailMessage class and use Ews.SendMessage method to send it:

// prepare new mail
var mail = new MailMessage();
//mail.From = "joe@example.org"; // not needed
mail.To = "john@example.org";
mail.Subject = "Test";
mail.BodyText = "Hello world!";
mail.BodyHtml = "<strong>Hello world!</strong>";

// create EWS client instance, connect, log in
// ...

// send the message
ews.SendMessage(mail);
' prepare new mail
Dim mail = New MailMessage()
'mail.From = "joe@example.org" ' not needed
mail.To = "john@example.org"
mail.Subject = "Test"
mail.BodyText = "Hello World!"
mail.BodyHtml = "<strong>Hello World!</strong>"

' create EWS client instance, connect, log in
' ...

' send the message
ews.SendMessage(mail)

Note: You don't need to specify the 'From' address unless you are sending e-mail on behalf of another user. The server will add the field for you.

Note: By default, sent messages are stored to the "Sent Items" folder at the Exchange server. To store the message into a different folder, specify desired folder ID using the optional second argument. Alternatively, to send the message without storing it, specify null (Nothing in VB.NET) as the folder ID.

Sending existing message (a draft) 

To send an message that is already stored at the Exchange server, determine its ID and use the following Ews.SendMessage overload:

// create EWS client instance, connect, log in
var ews = new Rebex.Net.Ews();
ews.Connect(hostname);
ews.Login(username, password);

// load message ID from a file, database, etc.
// (or find it on the server using 'Search' method)
EwsItemId messageId = GetMessageId();

// send existing message using the message ID
// message is moved to "Sent Items" afterwards
ews.SendMessage(messageId);
' create EWS client instance, connect, log in
Dim ews = New Rebex.Net.Ews()
ews.Connect(hostname)
ews.Login(username, password)

' load message ID from a file, database, etc.
' (or find it on the server using 'Search' method)
Dim messageId As EwsItemId = GetMessageId()

' send existing message using the message ID
' message is moved to "Sent Items" afterwards
ews.SendMessage(messageId)
Note: By default, sent messages are moved to the "Sent Items" folder at the Exchange server. To move the message into a different folder, specify desired folder ID using the optional second argument. Alternatively, to discard (delete) the message after sending, specify null (Nothing in VB.NET) as the folder ID.
Note: Older versions of Exchange server (such as Exchange 2010) cannot send messages which were already sent before.

Sending from a file or stream 

To send an e-mail message from a file or a stream, load the mail into MailMessage and send it:

// create EWS client instance, connect, log in
// ...

// load message from a stream (or a file)
var mail = new MailMessage();
mail.Load(stream);

// send the message
ews.SendMessage(mail);
' create EWS client instance, connect, log in
' ...

' load message from stream (or a file)
Dim mail = New MailMessage()
mail.Load(stream)

' send the message
ews.SendMessage(mail)

Sending e-mail on behalf of another user 

To send an e-mail on behalf of another address, use MailMessage.From to specify the desired `From` address. The `Sender` address will reflect the currently authenticated user.

// prepare mail
mail.From = "joe@example.org";

// create EWS client instance, connect
// ...

// login under sender account
ews.Login("bob@example.org", password);

// Bob sends mail on behalf of Joe
ews.SendMessage(mail);
' prepare mail
mail.From = "joe@example.org"

' create EWS client instance, connect
' ...

' login under sender account
ews.Login("bob@example.org", password)

' Bob sends mail on behalf of Joe
ews.SendMessage(mail)
Note: For this to work, mailbox access permissions have to be properly configured at the server:
  1. No permissions - sender cannot impersonate anther address or send e-mail on its behalf.
  2. Send on behalf - sender can send an e-mail on behalf of another address.
  3. Send as - sender can impersonate the other address.

Instructing the receiver to send replies to another address 

When you would like the replies to be delivered to an address different from the sender, specify the MailMessage.ReplyTo property:

// prepare mail message
mail.ReplyTo = "bob@example.org";

// create EWS client instance, connect, log in
// ...

// send mail from Joe, but request that replies go to Bob
ews.SendMessage(mail);
' prepare mail message
mail.ReplyTo = "bob@example.org"

' create EWS client instance, connect, log in
' ...

' send mail from Joe, but request that replies go to Bob
ews.SendMessage(mail)

Sending replies 

To create a reply to a message, use the MailMessage.CreateReply method. Then, send the message using Ews.SendMessage method:

// download a message to reply to
MailMessage original = ews.GetMailMessage(messageId);

// reply to all
MailMessage reply = original.CreateReply("joe@example.org", ReplyBodyTransformation.None, true);
reply.BodyText = "Hello, I agree.\n" + original.BodyText;

// send the reply
ews.SendMessage(reply);
' download a message to reply to
Dim original As MailMessage = ews.GetMailMessage(messageId)

' reply to all
Dim reply As MailMessage = original.CreateReply("joe@example.org", ReplyBodyTransformation.None, True)
reply.BodyText = "Hello, I agree." & vbLf + original.BodyText

' send the reply
ews.SendMessage(reply)

Forwarding messages 

To forward a message to another address, copy the required fields and add the original message as an attachment:

// download a message to forward
MailMessage original = ews.GetMailMessage(messageId);

// prepare forward message
MailMessage forward = new MailMessage();
forward.From = "joe@example.org";
forward.To = "john@example.org";
forward.Subject = "FW: " + original.Subject;
forward.BodyText = "Hello, I am forwarding this to you:\n\n" + original.BodyText;

// include original message as attachment
forward.Attachments.Add(new Attachment(original));

// send it
ews.SendMessage(forward);
' download a message to forward
Dim original As MailMessage = ews.GetMailMessage(messageId)

' prepare forward message
Dim forward As New MailMessage()
forward.From = "joe@example.org"
forward.To = "john@example.org"
forward.Subject = "FW: " & original.Subject
forward.BodyText = "Hello, I am forwarding this to you:" &
    vbLf & vbLf & original.BodyText

' include original message as attachment
forward.Attachments.Add(New Attachment(original))

' send it
ews.SendMessage(forward)

Back to feature list...