简体   繁体   中英

Node.Js AWS SES - ETIMEDOUT when sending Email

I want to send a verification Email to new people signing up via AWS SES in Node.js:

var params = {
    Destination: {
        ToAddresses: [
            '...',
        ]
    },
    Message: {
        Body: {
            Html: {
                Data: '...',
                Charset: 'utf-8'
            },
            Text: {
                Data: '...',
                Charset: 'utf-8'
            }
        },
        Subject: {
            Data: '...',
            Charset: 'utf-8'
        }
    },
    Source: '...',
    ReturnPath: '...',
};
const ses = new AWS.SES({
    my_accessKeyId,
    my_secretAccessKey,
    my_region,
})
ses.sendEmail(params, function (err, data) {
    // ...
});

Unfortunately, nothing happens and after a minute or so I get the following error:

{ Error: connect ETIMEDOUT 35.157.44.176:443
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
  message: 'connect ETIMEDOUT 35.157.44.176:443',
  code: 'NetworkingError',
  errno: 'ETIMEDOUT',
  syscall: 'connect',
  address: '35.157.44.176',
  port: 443,
  region: 'eu-central-1',
  hostname: 'email.eu-central-1.amazonaws.com',
  retryable: true,
  time: 2018-10-18T16:52:00.803Z }

I am not sure if the error results from a mistake in my code, as I am currently only using the sandbox environment. But I verified the sending and receiving email, which should work fine even in sandbox mode.


PS I am not quite sure how to properly test my application when being in sandbox mode, as I am not allowed to send to unverified emails. Is it possible to request production access without a proper application running?

It's not obvious, so we'll need to do some troubleshooting.

First, lets see if you can connect from your local machine to the SES API using the AWS CLI. Make sure you have set up the aws credentials using aws configure and try:

aws ses list-identities

If this works, you should see a list of validated email addresses. If not, you will see an error (permissions maybe), or a timeout suggests a network connectivity issue.

Note on credentials: Don't include your credentials in your code, either configure a .credentials file , which happens when you used the aws configure above, load it from a shared json file or use environment variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).

Next, try to do the same in code:

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'eu-central-1'});
// Create SES Client
const ses = new AWS.SES({apiVersion: '2010-12-01'})
// Create params
var params = {
  IdentityType: "EmailAddress", 
  MaxItems: 123, 
  NextToken: ""
};
ses.listIdentities(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
});

After you set up your credentials as mentioned above, use this in your Send Email code to create the ses client:

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: myregion});
// Create SES Client
const ses = new AWS.SES({apiVersion: '2010-12-01'})

Other things to check:

  • Make sure that all the email addresses are verified ("From", "Source", "Sender", or "Return-Path")
  • Make sure that you have the correct SES Access Keys
  • If using EC2, make sure that the security groups allow access to the AWS API.
  • I hope it's working by now, if not I can only say go to this page and make sure you haven't missed anything obvious.

You need to change region from eu-central-1 to region where endpoint exist (eg. eu-west-1) in order to send emails out

https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html

You need to make sure your user your using to send the email

my_accessKeyId,
    my_secretAccessKey,

has the correct IAM permissions (role attached to it) to send the email with ses. Test with the role SES full access I believe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM