Rebex Graph
.NET client library for MS Graph API (Exchange Online)
Download 30-day free trial Buy from $199More .NET libraries
-
Rebex Mail Pack
IMAP, MS Graph, EWS, POP3, SMTP, MIME, S/MIME, MSG
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Sending e-mails
On this page:
The sample code on this page assumes you have already connected and authenticated to Microsoft 365 (Exchange Online) server.
Sending a new mail message
To send an e-mail message, create an instance of MailMessage
class and
call GraphClient.SendMessage()
method:
// prepare e-mail var mail = new Rebex.Mail.MailMessage(); mail.Sender = "alice@example.org"; // not needed mail.From = "alice@example.org"; // not needed mail.To = "bob@example.org"; mail.Subject = "Sent using Rebex Graph"; mail.BodyText = "Hello world!"; mail.BodyHtml = "<strong>Hello world!</strong>"; // ... // create Graph client instance, connect, log in // ... // send the message client.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.
Sending an existing draft message
To send an e-mail message that is already stored at the Exchange server, determine its ID and use the
following SendMessage()
overload:
// create Graph client instance, connect, log in // ... // message is sent according to the "TO", "CC" and "BCC" fields // it is saved to "Sent Items" client.SendMessage(messageId);
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:
// load message from a stream (or a file) var mail = new Rebex.Mail.MailMessage(); mail.Load(stream); // send the message client.SendMessage(mail);
Sending e-mail on behalf of another user
To send an e-mail message on behalf of another address,
use MailMessage.From
to specify the desired `From` address.
The `Sender` address will still reflect the currently authenticated user.
// prepare e-mail var mail = new Rebex.Mail.MailMessage(); mail.Sender = "alice@example.org"; // not needed mail.From = "joe@example.org"; mail.To = "bob@example.org"; // ... // create Graph client instance, connect // ... // login with senders (Alice) OAuth access token client.Login(token); // Alice sends mail on behalf of Joe (to Bob) client.SendMessage(mail);
- No permissions - sender cannot impersonate another address or send e-mail on its behalf.
- Send on behalf - sender can send an e-mail on behalf of another address.
- Send as - sender can impersonate the other address.
Instructing the receiver to send replies to another address
To request that replies are delivered to a different address,
specify the MailMessage.ReplyTo
property:
// prepare e-mail var mail = new Rebex.Mail.MailMessage(); mail.From = "alice@example.org"; mail.To = "bob@example.org"; mail.ReplyTo = "joe@example.org"; // ... // send mail from Alice, but request that replies go to Joe client.SendMessage(mail);
Sending replies
To create a reply to a message, use MailMessage.CreateReply()
method:
// create Graph client instance, connect // ... // login with senders (Alice) OAuth access token client.Login(token); // download a message to reply to MailMessage original = client.GetMailMessage(messageId); // reply to all; from logged-on (Alice) account MailMessage reply = original.CreateReply("alice@example.org", ReplyBodyTransformation.None, true); reply.BodyText = "Hello, I agree.\n" + original.BodyText; // send the reply client.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 = client.GetMailMessage(messageId); // prepare forwarded message MailMessage forward = new MailMessage(); forward.To = "bob@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 client.SendMessage(forward);
Back to feature list...