Sending email using EWS and OAuth2

Hi

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"
};

exchClient.SendMessage(message);

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")
};

message.ToRecipients.Add("to@address.no");
message.Send();

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

Thank you

Regards
Arild

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.

Regards,
Mario

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.

Regards
Arild

Yes, exactly!

Regards,
Mario