Setup SMTP Configuration using AWS SES
Published
- Navigate to Amazon SES console.
- From the navigation pane, choose SMTP Settings.
- Under Under Simple Mail Transfer Protocol (SMTP) settings, note the values for SMTP endpoints and Ports. Use the SMTP endpoint and ports to connect to SMTP. For example, if you're in the ap-southeast-1 AWS Region, note the following:
SMTP endpoint: email-smtp.ap-southeast-1.amazonaws.com
Port: 25, 465 or 587 - Click on Create SMTP Credentials, which will redirect you to IAM Console. For Create User for SMTP, type a name for your SMTP user in the User Name field and click on Create user in the bottom-right corner.
- Note the SMTP Username and SMTP Password for your SMTP user. Alternatively, you can download the CSV file.
aws / ses.js
const nodemailer = require("nodemailer");
async function sendEmail(to, subject, body) {
// Configure SMTP transporter
const transporter = nodemailer.createTransport({
host: "email-smtp.<region>.amazonaws.com", // Replace with your AWS SES region
port: 587, // Use 465 for SSL, 587 for TLS
secure: false, // false for TLS, true for SSL
auth: {
user: "YOUR_SMTP_USERNAME", // Replace with your SMTP username
pass: "YOUR_SMTP_PASSWORD" // Replace with your SMTP password
}
});
// Email options
const mailOptions = {
from: "your-email@example.com", // Must be a verified SES sender email
to: to,
subject: subject,
text: body, // Plain Text body
html: `<p>${body}</p>` // HTML body
};
try {
const info = await transporter.sendMail(mailOptions);
console.log("Email sent successfully:", info.messageId);
} catch (error) {
console.error("Error sending email:", error);
}
}
// Example Usage
sendEmail("recipient@example.com", "Test Email",
"This is a test email from AWS SES using SMTP in Node.js.");