Search code examples
javascriptoutlookoffice-jsoutlook-addinexchangewebservices

Sending Email in outlook using EWS using js


I am able to send the mail to users but the attachment file is not going with the request. The reciver is reciveing just Subject and body and no attachment . What should i do so that it sends attachment also. Also The mails recieved by people are going to junk automaticlly. Reference-

Is there a way to send a mail seamlessly using Office.js?

https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem-operation-email-message

https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/createattachment

  function send_mail(msg){
  console.log(msg);
  var request = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
    '  <soap:Header><t:RequestServerVersion Version="Exchange2010" /></soap:Header>'+
    '  <soap:Body>'+
    '    <m:CreateItem MessageDisposition="SendAndSaveCopy">'+
    '      <m:SavedItemFolderId><t:DistinguishedFolderId Id="sentitems" /></m:SavedItemFolderId>'+
    '      <m:Items>'+
    '        <t:Message>'+
  '           <ItemId Id="123" />'+
    '          <t:Subject>'+ msg.subject+'</t:Subject>'+
    '          <t:Body BodyType="HTML">'+msg.body +'</t:Body>'+
    '          <t:ToRecipients>'+
    '            <t:Mailbox><t:EmailAddress>' +msg.To + '</t:EmailAddress></t:Mailbox>'+
    '          </t:ToRecipients>'+
    '        </t:Message>'+
    '      </m:Items>'+
    '    </m:CreateItem>'+
  '  <CreateAttachment xmlns="https://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">'+
  ' <ParentItemId Id="123"/>'+
  ' <Attachments>'+
    ' <t:ItemAttachment>'+
    '   <t:Name>MyAttachment</t:Name>'+
      ' <t:Message>'+
       '  <t:ItemClass></t:ItemClass>'+
       '  <t:Subject>My attachment subject</t:Subject>'+
        ' <t:Body BodyType="Text">My attachment body</t:Body>'+
     '  </t:Message>'+
   '  </t:ItemAttachment>'+
 '  </Attachments>'+
' </CreateAttachment>'+
    ' </soap:Body>'+
    ' </soap:Envelope>';


Office.context.mailbox.makeEwsRequestAsync(request, function (asyncResult) {
  if (asyncResult.status == "failed") {
    console.log("Error" + asyncResult.error.message)
   // showMessage("Action failed with error: ");
  }
  else {
    console.log("Message Sent")
  // showMessage("Message sent!");
  }
});

Solution

  • In your SOAP request, you are calling the CreateAttachment EWS operation. This operation is not supported when running EWS in the context of an add-in. See this link for the full list of supported EWS operations: https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/web-services

    Instead of using EWS, consider using REST/Microsoft Graph. We have documentation on that here: https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/use-rest-api.