Ian
Ian

Reputation: 51

Nodemailer with amazon WorkMail SMTP interface vs SES

I've been able to send email using Amazon's SES interface with WorkMail, but when trying to use stmp I get an error. Is their advantages to using the SES over smtp? If sticking with SES, should I being using the aws-sdk module?

My goal is to create a simple server that will send contact form contents to my email. I've stripped out that api from this code for simplicity.

SES code (working):

const nodemailer = require('nodemailer');
const ses = require('nodemailer-ses-transport');
const aws = require('aws-sdk');
const fs = require('fs');

aws.config.loadFromPath('./config/aws.json');



const config = fs.readFileSync('./config/aws.json')

var transporter = nodemailer.createTransport(ses({
    accessKeyId: config.accessKeyId,
    secretAccessKey: config.secretAccessKey,
    rateLimit: 5
}));


var mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Sending Email using Node.js[nodemailer]',
  text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent');
  }
});  

Failing Code

const nodemailer = require('nodemailer');
const ses = require('nodemailer-smtp-transport');
const fs = require('fs');

const config = fs.readFileSync('./config/aws.json')

var transporter = nodemailer.createTransport(smtp({
  service: 'SES-US-EAST-1',
  server: 'smtp.mail.us-east-1.awsapps.com',
  secure: true,
  port: 465,
  auth: {
    user:   config.username,
    pass:   config.password
  },
  tls: {
    // do not fail on invalid certs
    rejectUnauthorized: false
  }
}));

var mailOptions = {
  from: config.username,
  to: '[email protected]',
  subject: 'Sending Email using Node.js[nodemailer]',
  text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});  

Error

Error: connect ENETUNREACH 169.254.169.254:80
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {
  message: 'EC2 Metadata roleName request returned error',
  errno: 'ENETUNREACH',
  code: 'ENETUNREACH',
  syscall: 'connect',
  address: '169.254.169.254',
  port: 80,
  time: 2020-01-27T21:34:12.265Z,
  originalError: {
    errno: 'ENETUNREACH',
    code: 'ENETUNREACH',
    syscall: 'connect',
    address: '169.254.169.254',
    port: 80,
    message: 'connect ENETUNREACH 169.254.169.254:80'
  }
}

Upvotes: 4

Views: 1615

Answers (1)

user20557032
user20557032

Reputation: 26

For the SMTP version, the username and password are created through the SMTP Credentials option in Amazon SES services. The user/pass the Workmail does not work in this case. The credentials are similar to those generated for a USER-style

programmatic user: AKIASADASDDASD
pass: dfsdflkjklrjlkwjrfklsdjkl3jfwerFSDFserf

let transporter = createTransport({
  service: "SES-US-EAST-1",
  host: 'smtp.mail.us-east-1.awsapps.com',
  port: '465',
  tls: {
    rejectUnauthorized: false
  },
  auth: {
    user: "AKIASADASDDASD",
    pass: "dfsdflkjklrjlkwjrfklsdjkl3jfwerFSDFserf"
  }
});

Upvotes: 1

Related Questions