This is a demo to send email with Amazon Simple Email Service API V2 and AWS JDK V2.
To compile send-email-ses you will ned:
- JDK ver 11.0+. Amazon Coretto distribution is recommended.
- Apache Maven.
To compile send-email-ses you will need to execute next maven command:
mvn verify
Wait a minute...
Why did not just execute
mvn clean package
?
See: https://andresalmiray.com/maven-verify-or-clean-install/ from @aalmiray.
Once compilates successfully you can use the fat jar (a jar file with all dependencies included) like a command line tool to test by sending emails using Amazon Simple Email Service.
Note: Fat jar is being generated as a convenience for making call from command line easier.
The fat jar will be located in the target directory inside the main project folder ant it could be executed like.
java -jar ./target/send-email-ses.jar
(in Linux/MacOs)java -jar .\target\send-email-ses.jar
(in Windows)
And it will show the coomand help, like this:
PS C:\Temp\send-email-ses> java -jar .\target\send-email-ses.jar
Picked up JAVA_TOOL_OPTIONS: -Dlog4j2.formatMsgNoLookups=true
Missing required options: f, t
usage: send-email
-a,--attachments Email attachement(s)
-b,--body Email body
-f,--from From email address
-p,--protocol Protocol to send email (SMTP | API).
Default to API.
-r,--repetitions Number of repetitions (emails to be
sent)
-s,--subject Email subject
-t,--to To email address
Creating and configuring Amazon Simple Email Service
In order create and get Amazon Simple Email Service ready to be used you will need to do the next steps:
- Having a valid AWS Account or acreate a new one. See: https://portal.aws.amazon.com/billing/signup.
- Set up a new Amazon Simple Email Service in your account. See: https://docs.aws.amazon.com/ses/latest/dg/setting-up.html or have a oreviously created one.
- Create and Verify Amazon Simple Email Service Identity: https://docs.aws.amazon.com/ses/latest/dg/creating-identities.html.
- In case you want to send emails to any email address you need to move Amazon Simple Email Service out of the sanbox (production mode). See: https://docs.aws.amazon.com/ses/latest/dg/request-production-access.html. Otherwise, you can just test sending emails using the simulator, like this: https://docs.aws.amazon.com/ses/latest/dg/send-an-email-from-console.html#send-email-simulator.
- Create a new https://docs.aws.amazon.com/iam/index.html user without console access: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html.
- Create a new access key for the recently created user: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html and keep the access key and password as well as the region you created it, you will need them to send emails later.
- Create a sending authorization policy and assign it to the recently created user. Make sure to enable
ses:SendRawEmail
permission that is needed to use this demo. Note: To assign this IAM policy you will need the recently created user ARN. See: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users.html#id_users_create_aws-identifiers.
Configure send-email-ses to connect to Amazon Simple Email Service
To enable send-email-ses to send emails using your Amazon Simple Email Service verified identity you need to create the next environment varibles:
- For SMTP protocol:
SMTP_HOST
(set the host for Amazon Simple Email Service).
Usually:email-smtp.us-east-1.amazonaws.com
.SMTP_PORT
(set the port for Amazon Simple Email Service).
Example:587
.SMTP_USERNAME
(set the username for Amazon Simple Email Service).
Example:BTIARO8GHKXGYWOBXQKB
.SMTP_PASSWORD
(set the password for Amazon Simple Email Service).
Example:AFhfouBjJU8btiY4hkhAdS/oCilfa7iE7x1rzPqaPhnN
.
- For API protocol:
AWS_REGION
(set the region were your created the Amazon Simple Email Service verified identity).
Example value:us-east-1
.AWS_ACCESS_KEY_ID
(set the access key of your IAM credentials created to use the service).
Example value:FKGRTI6GFKXGT6TMVJ5E
AWS_SECRET_ACCESS_KEY
(Set the secret of your IAM credentials created to use the service).
Example value:YOhg+FzepvjtftLNJGUlP4IXkwSkAr+ZQmMgCC4RE
.
java -jar send-email-ses.jar --from [email protected] --to [email protected]
.
java -jar send-email-ses.jar --from [email protected] --to [email protected] '' --subject "Lorem Ipsum" --body "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
.
java -jar send-email-ses.jar --from [email protected] --to [email protected] '' --subject "Lorem Ipsum" --body "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." --attachments "/home/johndoe/lorem-ipsum.pdf"
.
java -jar send-email-ses.jar --from [email protected] --to [email protected] --repetitions 10
.
The execution of send-email-ses will show you some useful statistics like this:
[main] INFO com.amazon.aws.App - Command Started
[main] INFO com.amazon.aws.App - Sending 1 emails...
[main] INFO com.amazon.aws.App - Process: SendEmail finished.
[main] INFO com.amazon.aws.App - Protocol used: SMTP.
[main] INFO com.amazon.aws.App - Emails sent: 1.
[main] INFO com.amazon.aws.App - Total duration: 0:00:02.226 (H:MM:SS.MS)
[main] INFO com.amazon.aws.App - Average duration: 0:00:02.226 (H:MM:SS.MS)
[main] INFO com.amazon.aws.App - Average speed: 0.449 (emails/second)
[main] INFO com.amazon.aws.App - Command Finished