Skip to content

This is a demo to send email with Amazon Simple Email Service API V2 and AWS JDK V2

License

Notifications You must be signed in to change notification settings

josuemb/send-email-ses

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

send-email-ses

This is a demo to send email with Amazon Simple Email Service API V2 and AWS JDK V2.

Compilation

Prerequisites

To compile send-email-ses you will ned:

How to compile it

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:

  1. Having a valid AWS Account or acreate a new one. See: https://portal.aws.amazon.com/billing/signup.
  2. 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.
  3. Create and Verify Amazon Simple Email Service Identity: https://docs.aws.amazon.com/ses/latest/dg/creating-identities.html.
  4. 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.
  5. 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.
  6. 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.
  7. 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:
    1. SMTP_HOST (set the host for Amazon Simple Email Service).
      Usually: email-smtp.us-east-1.amazonaws.com.
    2. SMTP_PORT (set the port for Amazon Simple Email Service).
      Example: 587.
    3. SMTP_USERNAME (set the username for Amazon Simple Email Service).
      Example: BTIARO8GHKXGYWOBXQKB.
    4. SMTP_PASSWORD (set the password for Amazon Simple Email Service).
      Example: AFhfouBjJU8btiY4hkhAdS/oCilfa7iE7x1rzPqaPhnN.
  • For API protocol:
    1. AWS_REGION (set the region were your created the Amazon Simple Email Service verified identity).
      Example value: us-east-1.
    2. AWS_ACCESS_KEY_ID (set the access key of your IAM credentials created to use the service).
      Example value: FKGRTI6GFKXGT6TMVJ5E
    3. AWS_SECRET_ACCESS_KEY (Set the secret of your IAM credentials created to use the service).
      Example value: YOhg+FzepvjtftLNJGUlP4IXkwSkAr+ZQmMgCC4RE.

Use send-email-ses to send test emails

To send an email with default subject and content

java -jar send-email-ses.jar --from [email protected] --to [email protected].

To send an email with custom subject and content

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.".

To send an email with custom subject and content and an attachment

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".

To send 10 emails with default subject and content

java -jar send-email-ses.jar --from [email protected] --to [email protected] --repetitions 10.

Summary of sent emails

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

About

This is a demo to send email with Amazon Simple Email Service API V2 and AWS JDK V2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages