GemBox Support Forum

Sending email using EWS and OAuth2


I have installed the latest version of GemBox.Email (15.0.1039) and trying to send e-mail using this example.

var exchClient = new ExchangeClient("https://outlook.office365.com/EWS/Exchange.asmx");
exchClient.Authenticate("from@address.no", myAccessToken, ExchangeAuthentication.OAuth2);

var message = new MailMessage(
    new MailAddress("from@adddress.no", "From name"),
    new MailAddress("to@address.no", "Recipient Name"))
    Subject = "Send Email in C#",
    BodyText = "Hi,\n" +
        "This message was created and sent with GemBox.Email.\n" +
        "Read more about it on gemboxsoftware"


The exchClient.SendMessage code fails with the error:
The remote server returned an error: (500) Internal Server Error.

To verify that my access token is correct and I can send email successfully, I used the same token to successfully send an email using Exchange.WebServices.Managed.Api:

var ewsClient = new ExchangeService();
ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
ewsClient.Credentials = new OAuthCredentials(myAccessToken);
ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "from@address.no");

var message = new EmailMessage(ewsClient)
    Subject = "This is a test", 
    Body = new MessageBody(BodyType.HTML, "This is a test body"), 
    From = new EmailAddress("from@address.no")


Is there a bug in GemBox.Email library for EWS that result in the 500 internal server error?

Thank you


Hi Arild,

Can you try running again that code of yours that uses Exchange.WebServices.Managed.Api, but remove this line:

ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "from@address.no");

Does it reproduce the same issue?
I presume the problem is with the permissions and the above should result in an Exception as well.

Unfortunately, GemBox.Email currently doesn’t support impersonating a user (mailbox).
Also, I’m afraid that at the moment I cannot say when exactly this will be available.

If interested, please feel free to create a GemBox.Email feature request for this so that other users may vote for it, thus increasing its priority.


Hi Mario

Thank you for your reply.

I have tried removing the ImpersonateUserId line and the result was an error:
ExchangeImpersonation SOAP header must be present for this type of OAuth token.

So if I understand you correctly I need to make sure permissions allow me to use the mailbox “directly”, without any impersonation. Then GemBox Email should work as expecte.


Yes, exactly!