Node.Js AWS SES - 发送电子邮件时超时
Node.Js AWS SES - ETIMEDOUT when sending Email
我想向 Node.js 中通过 AWS SES 注册的新人发送验证电子邮件:
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) {
// ...
});
不幸的是,什么也没发生,大约一分钟后我收到以下错误:
{ 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 }
我不确定这个错误是否是我的代码错误造成的,因为我目前只使用沙盒环境。但我验证了发送和接收电子邮件,即使在沙盒模式下也应该可以正常工作。
P.S。我不太确定在沙盒模式下如何正确测试我的应用程序,因为我不允许发送到未经验证的电子邮件。是否可以在没有适当应用程序的情况下请求生产访问权限运行?
您需要确保您的用户用于发送电子邮件
my_accessKeyId,
my_secretAccessKey,
具有正确的 IAM 权限(附加角色)来发送带有 ses 的电子邮件。我相信使用角色 SES 完全访问权限进行测试。
您需要将区域从 eu-central-1 更改为存在端点的区域(例如 eu-west-1)才能发送电子邮件
https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html
这并不明显,因此我们需要进行一些故障排除。
首先,让我们看看您是否可以使用 AWS CLI 从本地计算机连接到 SES API。确保您已使用 aws configure
设置 aws credentials 并尝试:
aws ses list-identities
如果成功,您应该会看到经过验证的电子邮件地址列表。
否则,您将看到一个错误(可能是权限问题),或者超时表明存在网络连接问题。
凭据注意事项:不要在代码中包含您的凭据,configure a .credentials file, which happens when you used the aws configure
above, load it from a shared json file 或使用环境变量(AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY)。
接下来,尝试在代码中做同样的事情:
// 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
});
如上所述设置凭据后,在发送电子邮件代码中使用它来创建 ses 客户端:
// 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'})
其他要检查的东西:
- 确保所有电子邮件地址都是 verified("From"、"Source"、"Sender" 或 "Return-Path")
- 确保您有正确的 SES Access Keys
- 如果使用 EC2,请确保安全组允许访问 AWS API。
- 我希望它现在能正常工作,否则我只能说去 this page 并确保你没有错过任何明显的事情。
我想向 Node.js 中通过 AWS SES 注册的新人发送验证电子邮件:
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) {
// ...
});
不幸的是,什么也没发生,大约一分钟后我收到以下错误:
{ 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 }
我不确定这个错误是否是我的代码错误造成的,因为我目前只使用沙盒环境。但我验证了发送和接收电子邮件,即使在沙盒模式下也应该可以正常工作。
P.S。我不太确定在沙盒模式下如何正确测试我的应用程序,因为我不允许发送到未经验证的电子邮件。是否可以在没有适当应用程序的情况下请求生产访问权限运行?
您需要确保您的用户用于发送电子邮件
my_accessKeyId,
my_secretAccessKey,
具有正确的 IAM 权限(附加角色)来发送带有 ses 的电子邮件。我相信使用角色 SES 完全访问权限进行测试。
您需要将区域从 eu-central-1 更改为存在端点的区域(例如 eu-west-1)才能发送电子邮件
https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html
这并不明显,因此我们需要进行一些故障排除。
首先,让我们看看您是否可以使用 AWS CLI 从本地计算机连接到 SES API。确保您已使用 aws configure
设置 aws credentials 并尝试:
aws ses list-identities
如果成功,您应该会看到经过验证的电子邮件地址列表。 否则,您将看到一个错误(可能是权限问题),或者超时表明存在网络连接问题。
凭据注意事项:不要在代码中包含您的凭据,configure a .credentials file, which happens when you used the aws configure
above, load it from a shared json file 或使用环境变量(AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY)。
接下来,尝试在代码中做同样的事情:
// 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
});
如上所述设置凭据后,在发送电子邮件代码中使用它来创建 ses 客户端:
// 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'})
其他要检查的东西:
- 确保所有电子邮件地址都是 verified("From"、"Source"、"Sender" 或 "Return-Path")
- 确保您有正确的 SES Access Keys
- 如果使用 EC2,请确保安全组允许访问 AWS API。
- 我希望它现在能正常工作,否则我只能说去 this page 并确保你没有错过任何明显的事情。