exchangewebservicesoffice365ews-managed-apiofficedev

Can not send mail using outlook manage api 2.2 with identity token


I want to send mail using EWS Managed API 2.2 and authentication token which obtained from Office.context.mailbox.getCallbackTokenAsync() method in office javascript api 1.1. also i used asp.net web api project to call ews functions.an execption occured when executing messge.Send() method.Here is code sample and error

public class HomeController : ApiController
        {
            string EwsUrl = "https://outlook.office365.com/EWS/Exchange.asmx";
            string rawToken = "eyJ0eXAiOiJKV1QiLCJhbGciO……WER";

            public void GetSendMailResponse()
            {
                ExchangeService service = new ExchangeService();
                service.Credentials = new OAuthCredentials(rawToken);
                service.Url = new Uri(EwsUrl);

                // Create an email message and identify the Exchange service.
                EmailMessage message = new EmailMessage(service);

                // Add properties to the email message.
                message.Subject = "Interesting";
                message.Body = "The merger is finalized.";
                message.ToRecipients.Add("aaa@aaa.com");
                message.Send();
            }
        } 

Full exception

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The requested web method is unavailable to this caller or application.
</ExceptionMessage>
<ExceptionType>
Microsoft.Exchange.WebServices.Data.ServiceResponseException
</ExceptionType>
<StackTrace>
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ProcessWebException(WebException webException) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute() at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalCreateItems(IEnumerable`1 items, FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode, ServiceErrorHandling errorHandling) at Microsoft.Exchange.WebServices.Data.ExchangeService.CreateItem(Item item, FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode) at Microsoft.Exchange.WebServices.Data.Item.InternalCreate(FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode) at Microsoft.Exchange.WebServices.Data.EmailMessage.InternalSend(FolderId parentFolderId, MessageDisposition messageDisposition) at Microsoft.Exchange.WebServices.Data.EmailMessage.Send() at VirtruInterface.Controllers.HomeController.GetSendMailResponse() in \Controllers\HomeController.cs:line 42 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClassc.<GetExecutor>b__6(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
</StackTrace>


Solution

  • The token you get from getCallbackTokenAsync in a mail app cannot be used to send mail. You can only retrieve the current item or an attachment on the current item. See remarks at http://msdn.microsoft.com/en-us/library/office/jj984589(v=office.1501401).aspx.