Example usage for com.amazonaws.services.simpleemail AWSJavaMailTransport AWSJavaMailTransport

List of usage examples for com.amazonaws.services.simpleemail AWSJavaMailTransport AWSJavaMailTransport

Introduction

In this page you can find the example usage for com.amazonaws.services.simpleemail AWSJavaMailTransport AWSJavaMailTransport.

Prototype

public AWSJavaMailTransport(Session session, URLName urlname) 

Source Link

Usage

From source file:AWSJavaMailSample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*/*from  w  w w  .ja va2 s .  com*/
     * Important: Be sure to fill in your AWS access credentials in the
     * AwsCredentials.properties file before you try to run this sample.
     * http://aws.amazon.com/security-credentials
     */
    PropertiesCredentials credentials = new PropertiesCredentials(
            AWSJavaMailSample.class.getResourceAsStream("AwsCredentials.properties"));
    AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(credentials);

    /*
     * Before you can send email via Amazon SES, you need to verify that you
     * own the email address from which youll be sending email. This will
     * trigger a verification email, which will contain a link that you can
     * click on to complete the verification process.
     */
    verifyEmailAddress(ses, FROM);

    /*
     * If you've just signed up for SES, then you'll be placed in the Amazon
     * SES sandbox, where you must also verify the email addresses you want
     * to send mail to.
     *
     * You can uncomment the line below to verify the TO address in this
     * sample.
     *
     * Once you have full access to Amazon SES, you will *not* be required
     * to verify each email address you want to send mail to.
     *
     * You can request full access to Amazon SES here:
     * http://aws.amazon.com/ses/fullaccessrequest
     */
    //verifyEmailAddress(ses, TO);

    /*
     * Setup JavaMail to use the Amazon Simple Email Service by specifying
     * the "aws" protocol.
     */
    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "aws");

    /*
     * Setting mail.aws.user and mail.aws.password are optional. Setting
     * these will allow you to send mail using the static transport send()
     * convince method.  It will also allow you to call connect() with no
     * parameters. Otherwise, a user name and password must be specified
     * in connect.
     */
    props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());
    props.setProperty("mail.aws.password", credentials.getAWSSecretKey());

    Session session = Session.getInstance(props);

    try {
        // Create a new Message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(TO));
        msg.setSubject(SUBJECT);
        msg.setText(BODY);
        msg.saveChanges();

        // Reuse one Transport object for sending all your messages
        // for better performance
        Transport t = new AWSJavaMailTransport(session, null);
        t.connect();
        t.sendMessage(msg, null);

        // Close your transport when you're completely done sending
        // all your messages
        t.close();
    } catch (AddressException e) {
        e.printStackTrace();
        System.out.println("Caught an AddressException, which means one or more of your "
                + "addresses are improperly formatted.");
    } catch (MessagingException e) {
        e.printStackTrace();
        System.out.println("Caught a MessagingException, which means that there was a "
                + "problem sending your message to Amazon's E-mail Service check the "
                + "stack trace for more information.");
    }
}

From source file:aws.sample.AWSJavaMailSample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*// www .  j a v a  2 s . co m
     * Important: Be sure to fill in your AWS access credentials in the AwsCredentials.properties file before you try to run this sample. http://aws.amazon.com/security-credentials
     */
    PropertiesCredentials credentials = new PropertiesCredentials(
            AWSJavaMailSample.class.getResourceAsStream("/AwsCredentials.properties"));
    AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(credentials);

    /*
     * Before you can send email via Amazon SES, you need to verify that you own the email address from which you?l be sending email. This will trigger a verification email, which will contain a link that you can click on to complete the verification process.
     */
    verifyEmailAddress(ses, FROM);

    /*
     * If you've just signed up for SES, then you'll be placed in the Amazon SES sandbox, where you must also verify the email addresses you want to send mail to.
     * 
     * You can uncomment the line below to verify the TO address in this sample.
     * 
     * Once you have full access to Amazon SES, you will *not* be required to verify each email address you want to send mail to.
     * 
     * You can request full access to Amazon SES here: http://aws.amazon.com/ses/fullaccessrequest
     */
    // verifyEmailAddress(ses, TO);

    /*
     * Setup JavaMail to use the Amazon Simple Email Service by specifying the "aws" protocol.
     */
    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "aws");

    /*
     * Setting mail.aws.user and mail.aws.password are optional. Setting these will allow you to send mail using the static transport send() convince method. It will also allow you to call connect() with no parameters. Otherwise, a user name and password must be specified in connect.
     */
    props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());
    props.setProperty("mail.aws.password", credentials.getAWSSecretKey());

    Session session = Session.getInstance(props);

    try {
        // Create a new Message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(TO));
        msg.setSubject(SUBJECT);
        msg.setText(BODY);
        msg.saveChanges();

        // Reuse one Transport object for sending all your messages
        // for better performance
        Transport t = new AWSJavaMailTransport(session, null);
        t.connect();
        t.sendMessage(msg, null);

        // Close your transport when you're completely done sending
        // all your messages
        t.close();
    } catch (AddressException e) {
        e.printStackTrace();
        System.out.println("Caught an AddressException, which means one or more of your "
                + "addresses are improperly formatted.");
    } catch (MessagingException e) {
        e.printStackTrace();
        System.out.println("Caught a MessagingException, which means that there was a "
                + "problem sending your message to Amazon's E-mail Service check the "
                + "stack trace for more information.");
    }
}

From source file:com.ajah.email.data.AmazonSESTransport.java

License:Apache License

/**
 * Sends a message through Amazon's SES service.
 * //from www  .j  av  a2 s  .  c o m
 * @param message
 *            The message to send. Subject, from and to are required.
 * @throws AddressException
 *             If there is a problem with one of the email addresses.
 * @throws MessagingException
 *             If there is a problem with the transport of the message.
 */
@Override
public void send(final EmailMessage message) throws AddressException, MessagingException {
    AjahUtils.requireParam(message, "message");
    AjahUtils.requireParam(message.getSubject(), "message.subject");
    AjahUtils.requireParam(message.getFrom(), "message.from");
    AjahUtils.requireParam(message.getRecipients(), "message.recipients");

    final AWSCredentials credentials = new BasicAWSCredentials(Config.i.get("aws.accessKey", null),
            Config.i.get("aws.secretKey", null));
    if (Config.i.getBoolean("aws.ses.verify", false)) {
        // Verification is active so we'll need to check that first
        final AmazonSimpleEmailService email = new AmazonSimpleEmailServiceClient(credentials);
        final ListVerifiedEmailAddressesResult verifiedEmails = email.listVerifiedEmailAddresses();
        boolean verified = true;
        if (!isVerified(message.getFrom(), email, verifiedEmails)) {
            log.warning("Sender " + message.getFrom() + " is not verified");
            verified = false;
        }
        for (final EmailRecipient emailRecipient : message.getRecipients()) {
            if (!isVerified(emailRecipient.getAddress(), email, verifiedEmails)) {
                log.warning("Recipient " + emailRecipient.getAddress() + " is not verified");
                verified = false;
            }
        }
        if (!verified) {
            throw new MessagingException("Message not sent because one or more addresses need to be verified");
        }
    }
    final Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "aws");
    props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());
    props.setProperty("mail.aws.password", credentials.getAWSSecretKey());

    final Session session = Session.getInstance(props);

    final MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(new InternetAddress(message.getFrom().toString()));
    for (final EmailRecipient emailRecipient : message.getRecipients()) {
        switch (emailRecipient.getType()) {
        case BCC:
            mimeMessage.addRecipient(Message.RecipientType.BCC, new InternetAddress(emailRecipient.toString()));
            break;
        case CC:
            mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(emailRecipient.toString()));
            break;
        case TO:
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(emailRecipient.toString()));
            break;
        default:
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(emailRecipient.toString()));
            break;
        }
    }
    mimeMessage.setSubject(message.getSubject());
    final String htmlContent = message.getHtml();
    if (StringUtils.isBlank(htmlContent)) {
        // No HTML so we'll just send a plaintext message.
        mimeMessage.setText(message.getText());
    } else {
        final Multipart multiPart = new MimeMultipart("alternative");

        final BodyPart text = new MimeBodyPart();
        text.setText(message.getText());
        multiPart.addBodyPart(text);

        final BodyPart html = new MimeBodyPart();
        html.setContent(message.getHtml(), "text/html");
        multiPart.addBodyPart(html);

        mimeMessage.setContent(multiPart);
    }
    mimeMessage.saveChanges();

    final Transport transport = new AWSJavaMailTransport(session, null);
    transport.connect();
    transport.sendMessage(mimeMessage, null);
    transport.close();

}

From source file:com.ajah.email.data.EmailMessageManager.java

License:Apache License

/**
 * Sends a message through Amazon's SES service.
 * //from  w ww  .java 2 s. c om
 * @param message
 *            The message to send. Subject, from and to are required.
 * @throws AddressException
 *             If there is a problem with one of the email addresses.
 * @throws MessagingException
 *             If there is a problem with the transport of the message.
 */
public static void send(final EmailMessage message) throws AddressException, MessagingException {
    AjahUtils.requireParam(message, "message");
    AjahUtils.requireParam(message.getSubject(), "message.subject");
    AjahUtils.requireParam(message.getFrom(), "message.from");
    AjahUtils.requireParam(message.getTo(), "message.to");

    final AWSCredentials credentials = new BasicAWSCredentials(Config.i.get("aws.accessKey", null),
            Config.i.get("aws.secretKey", null));
    if (Config.i.getBoolean("aws.ses.verify", false)) {
        // Verification is active so we'll need to check that first
        final AmazonSimpleEmailService email = new AmazonSimpleEmailServiceClient(credentials);
        final ListVerifiedEmailAddressesResult verifiedEmails = email.listVerifiedEmailAddresses();
        boolean verified = true;
        if (!isVerified(message.getFrom(), email, verifiedEmails)) {
            log.warning("Sender " + message.getFrom() + " is not verified");
            verified = false;
        }
        for (final EmailAddress emailAddress : message.getTo()) {
            if (!isVerified(emailAddress, email, verifiedEmails)) {
                log.warning("Recipient " + emailAddress + " is not verified");
                verified = false;
            }
        }
        if (!verified) {
            throw new MessagingException("Message not sent because one or more addresses need to be verified");
        }
    }
    final Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "aws");
    props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());
    props.setProperty("mail.aws.password", credentials.getAWSSecretKey());

    final Session session = Session.getInstance(props);

    final MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(new InternetAddress(message.getFrom().toString()));
    for (final EmailAddress to : message.getTo()) {
        mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to.toString()));
    }
    mimeMessage.setSubject(message.getSubject());
    final String htmlContent = message.getHtml();
    if (StringUtils.isBlank(htmlContent)) {
        // No HTML so we'll just send a plaintext message.
        mimeMessage.setText(message.getText());
    } else {
        final Multipart multiPart = new MimeMultipart("alternative");

        final BodyPart text = new MimeBodyPart();
        text.setText(message.getText());
        multiPart.addBodyPart(text);

        final BodyPart html = new MimeBodyPart();
        html.setContent(message.getHtml(), "text/html");
        multiPart.addBodyPart(html);

        mimeMessage.setContent(multiPart);
    }
    mimeMessage.saveChanges();

    final Transport transport = new AWSJavaMailTransport(session, null);
    transport.connect();
    transport.sendMessage(mimeMessage, null);
    transport.close();

}

From source file:com.clicktravel.infrastructure.mail.aws.AmazonMailSender.java

License:Apache License

@Override
public Transport getTransport(final Session session) throws NoSuchProviderException {
    return new AWSJavaMailTransport(session, null);
}

From source file:it.polimi.modaclouds.cpimlibrary.mailservice.AmazonMailManager.java

License:Apache License

@Override
public void sendMail(CloudMail msgToSend) {

    if (!verifyEmailAddress(this.username) || !verifyEmailAddress(msgToSend.getTo()))
        return;// ww w. jav  a2s . c  o  m

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "aws");
    props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());
    props.setProperty("mail.aws.password", credentials.getAWSSecretKey());

    Session session = Session.getInstance(props);

    try {
        // Create a new Message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(this.username));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(msgToSend.getTo()));
        msg.setSubject(msgToSend.getSubject());
        msg.setText(msgToSend.getMsg());
        msg.saveChanges();

        // Reuse one Transport object for sending all your messages
        // for better performance
        Transport t = new AWSJavaMailTransport(session, null);
        t.connect();
        t.sendMessage(msg, null);

        // Close your transport when you're completely done sending
        // all your messages
        t.close();
    } catch (AddressException e) {
        e.printStackTrace();
        System.out.println("Caught an AddressException, which means one or more of your "
                + "addresses are improperly formatted.");
    } catch (MessagingException e) {
        e.printStackTrace();
        System.out.println("Caught a MessagingException, which means that there was a "
                + "problem sending your message to Amazon's E-mail Service check the "
                + "stack trace for more information.");
    }

}

From source file:org.onebusaway.admin.service.impl.EmailServiceImpl.java

License:Apache License

@PostConstruct
@Override//w w w.  j  a  v a  2  s. co  m
public void setup() {
    try {
        String mailSMTPServer = "";
        String mailSMTPServerPort = "";
        // Try getting smtp host and port values from configurationService.
        try {
            mailSMTPServer = configurationService.getConfigurationValueAsString("admin.smtpHost",
                    SMTP_HOST_NOT_FOUND);
            mailSMTPServerPort = configurationService.getConfigurationValueAsString("admin.smtpPort", "25");
        } catch (RemoteConnectFailureException e) {
            _log.error("Setting smtp host to value : '" + SMTP_HOST_NOT_FOUND
                    + "' due to failure to connect to TDM");
            mailSMTPServer = SMTP_HOST_NOT_FOUND;
            e.printStackTrace();
        }

        // If smtp host name was not found, assume this should use AWS
        _properties = new Properties();
        boolean useSMTP = mailSMTPServer.equals(SMTP_HOST_NOT_FOUND) ? false : true;
        if (useSMTP) { // Configure for SMTP
            _properties.setProperty("mail.transport.protocol", "smtp");
            _properties.setProperty("mail.smtp.starttls.enable", "false");
            _properties.setProperty("mail.smtp.host", mailSMTPServer);
            _properties.setProperty("mail.smtp.auth", "false");
            _properties.setProperty("mail.debug", "false");
            _properties.setProperty("mail.smtp.port", mailSMTPServerPort);
        } else { // Configure for AWS
            // AWS specifics
            _credentials = new BasicAWSCredentials(_username, _password);
            _eClient = new AmazonSimpleEmailServiceAsyncClient(_credentials);
            // Java specifics
            _properties.setProperty("mail.transport.protocol", "aws");
            _properties.setProperty("mail.aws.user", _credentials.getAWSAccessKeyId());
            _properties.setProperty("mail.aws.password", _credentials.getAWSSecretKey());
        }

        _session = Session.getInstance(_properties);
        Session session = Session.getDefaultInstance(_properties);
        session.setDebug(false);
        _transport = useSMTP ? _session.getTransport("smtp") : new AWSJavaMailTransport(_session, null);
    } catch (Exception ioe) {
        // log this heavily, but don't let it prevent context startup
        _log.error("EmailServiceImpl setup failed, likely due to missing or invalid credentials.");
        _log.error(ioe.toString());
    }
}