Skip to content

Instantly share code, notes, and snippets.

@hussainanjar
Created November 18, 2015 13:27
Show Gist options
  • Save hussainanjar/7326c64289d2b51c65dc to your computer and use it in GitHub Desktop.
Save hussainanjar/7326c64289d2b51c65dc to your computer and use it in GitHub Desktop.
Groovy script to send email using Amazon SES
@Grab('javax.mail:mail:1.4.7')
import java.util.Properties
import javax.mail.*
import javax.mail.internet.*
final String FROM = "SENDER@EXAMPLE.COM" // Replace with your "From" address. This address or domain must be verified.
final String TO = "RECIPIENT@EXAMPLE.COM" // Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.
final String BODY = "This email was sent through the Amazon SES SMTP interface by using Groovy."
final String SUBJECT = "Amazon SES test (SMTP interface accessed using Groovy)"
// Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials.
final String SMTP_USERNAME = "YOUR_SMTP_USERNAME" // Replace with your SMTP username.
final String SMTP_PASSWORD = "YOUR_SMTP_PASSWORD" // Replace with your SMTP password.
// Amazon SES SMTP host name. This example uses the US West (Oregon) region.
final String HOST = "email-smtp.us-west-2.amazonaws.com"
// Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 25 because we will use
// STARTTLS to encrypt the connection.
final int PORT = 25
// Create a Properties object to contain connection configuration information.
Properties props = System.getProperties()
props.put("mail.transport.protocol", "smtp")
props.put("mail.smtp.port", PORT)
// Set properties indicating that we want to use STARTTLS to encrypt the connection.
// The SMTP session will begin on an unencrypted connection, and then the client
// will issue a STARTTLS command to upgrade to an encrypted connection.
props.put("mail.smtp.auth", "true")
props.put("mail.smtp.starttls.enable", "true")
props.put("mail.smtp.starttls.required", "true")
// Create a Session object to represent a mail session with the specified properties.
Session session = Session.getDefaultInstance(props)
// Create a message with the specified information.
MimeMessage msg = new MimeMessage(session)
msg.setFrom(new InternetAddress(FROM))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO))
msg.setSubject(SUBJECT)
msg.setContent(BODY,"text/plain")
// Create a transport.
Transport transport = session.getTransport()
// Send the message.
try
{
println("Attempting to send an email through the Amazon SES SMTP interface...")
// Connect to Amazon SES using the SMTP username and password you specified above.
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD)
// Send the email.
transport.sendMessage(msg, msg.getAllRecipients())
println("Email sent!")
}
catch (Exception ex) {
println("The email was not sent.")
println("Error message: " + ex.getMessage())
}
finally
{
// Close and terminate the connection.
transport.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment