401 trying to connect my NodeJS API with a Microsoft exchange server using ews-javascript-api

153 Views Asked by At

Anyone can help me tell If there is something wrong with the code

When I try the credentials on this site: https://testconnectivity.microsoft.com/tests/Eas/input it works fine

var ews = require('ews-javascript-api');
var ewsAuth = require("ews-javascript-api-auth");

  async send(params) {
    const { subject, html } = params;

    let { to } = params;

    if (process.env.APP_ENV === "development") {
      to = process.env.MAIL_TO_DEV;
    }

   
  const sendMail = async (to, subject, html) => {
    ews.ConfigurationApi.ConfigureXHR(new ewsAuth.cookieAuthXhrApi(`${process.env.MAIL_USERNAME}`, process.env.MAIL_PASSWORD));
    const ewsConnection = new ews.ExchangeService(ews.ExchangeVersion.Exchange2010);
   
    ewsConnection.Credentials = new ews.ExchangeCredentials(`${process.env.MAIL_USERNAME}`, process.env.MAIL_PASSWORD);
    ewsConnection.Url = new ews.Uri(`${process.env.MAIL_HOST}`);

    const emailMessage = new ews.EmailMessage(ewsConnection);
    emailMessage.Subject = subject;
    emailMessage.Body = new ews.MessageBody(ews.BodyType.HTML,html);

    emailMessage.ToRecipients.Add(to);

    try {
      await emailMessage.Send();
      console.log('Email sent successfully.');
    } catch (error) {
      console.log('Email error:', error);
    }
  };

  sendMail(to, subject, html)
  .catch((error) => console.log('Email error:', error));
   
    return {};
  }

I'm receiving an error

SoapFaultDetails {
  message: '401 Unauthorized',
  InnerException: null,
  faultCode: null,
  faultString: null,
  faultActor: null,
  responseCode: 127,
  errorCode: 0,
  exceptionType: null,
  lineNumber: 0,
  positionWithinLine: 0,
  errorDetails: DictionaryWithStringKey {
    keys: [],
    keysToObjs: {},
    objects: {},
    keyPicker: [Function (anonymous)]
  },
  HttpStatusCode: 401,
  Exception: ServiceRequestUnauthorizedException {
    message: '401 Unauthorized',
    InnerException: null
  }
}

An example of my .env file contains all the information on the server

MAIL_HOST="https://xxx.xxx.com.br/EWS/Exchange.asmx"
MAIL_PORT=25
MAIL_USERNAME="xxxxxxxxx"
MAIL_PASSWORD="xxxx2023"
MAIL_FROM="[email protected]"
MAIL_TO_DEV="[email protected]"

I would like to know if there is a specific SDK for javascript to integrate with the exchange server or if someone has a piece of code on javascript that works with the exchange server

NOTE: when I change authentication to ewsAuth.ntlmAuthXhrApi instead of cookieAuthXhrApi, I am having

SoapFaultDetails {
  message: 'Invalid message signature: Negotiate',
  InnerException: null,
  faultCode: null,
  faultString: null,
  faultActor: null,
  responseCode: 127,
  errorCode: 0,
  exceptionType: null,
  lineNumber: 0,
  positionWithinLine: 0,
  errorDetails: DictionaryWithStringKey {
    keys: [],
    keysToObjs: {},
    objects: {},
    keyPicker: [Function (anonymous)]
  },
  HttpStatusCode: undefined
}
0

There are 0 best solutions below