Example usage for com.amazonaws.services.simpleemail.model Destination getToAddresses

List of usage examples for com.amazonaws.services.simpleemail.model Destination getToAddresses

Introduction

In this page you can find the example usage for com.amazonaws.services.simpleemail.model Destination getToAddresses.

Prototype


public java.util.List<String> getToAddresses() 

Source Link

Document

The recipients to place on the To: line of the message.

Usage

From source file:com.kolich.aws.services.ses.impl.KolichSESClient.java

License:Open Source License

@Override
public Either<HttpFailure, SendEmailResult> sendEmail(final Destination destination, final Message message,
        final List<String> replyToAddresses, final String returnPath, final String from) {
    return new AwsSESHttpClosure<SendEmailResult>(client_, SC_OK, new SendEmailResultStaxUnmarshaller()) {
        @Override//www . j  av  a2s .  c o  m
        public void validate() throws Exception {
            checkNotNull(destination, "Destination cannot be null.");
            checkNotNull(message, "Message cannot be null.");
            checkNotNull(from, "From email address cannot be null.");
            checkState(isValidEmail(from),
                    "Invalid 'from' email address, " + "did not match expected email pattern.");
        }

        @Override
        public void prepare(final AwsHttpRequest request) throws Exception {
            request.addParameter(SES_ACTION_PARAM, SES_ACTION_SENDEMAIL);
            request.addParameter(SES_SOURCE_ADDRESS_PARAM, from);
            // To
            for (int i = 0; i < destination.getToAddresses().size(); i++) {
                final String to = destination.getToAddresses().get(i);
                request.addParameterOpt(String.format("%s.%s", SES_DESTINATION_TO_PARAM, i + 1), to);
            }
            // CC
            for (int i = 0; i < destination.getCcAddresses().size(); i++) {
                final String cc = destination.getCcAddresses().get(i);
                request.addParameterOpt(String.format("%s.%s", SES_DESTINATION_CC_PARAM, i + 1), cc);
            }
            // BCC
            for (int i = 0; i < destination.getBccAddresses().size(); i++) {
                final String bcc = destination.getBccAddresses().get(i);
                request.addParameterOpt(String.format("%s.%s", SES_DESTINATION_BCC_PARAM, i + 1), bcc);
            }
            // Subject
            final Content subject;
            if ((subject = message.getSubject()) != null) {
                request.addParameterOpt(SES_SUBJECT_PARAM, subject.getData());
                request.addParameterOpt(SES_SUBJECT_CHARSET_PARAM, subject.getCharset());
            }
            // Body
            final Body body;
            if ((body = message.getBody()) != null) {
                // Text body
                final Content text;
                if ((text = body.getText()) != null) {
                    request.addParameterOpt(SES_BODY_TEXT_PARAM, text.getData());
                    request.addParameterOpt(SES_BODY_TEXT_CHARSET_PARAM, text.getCharset());
                }
                final Content html;
                if ((html = body.getHtml()) != null) {
                    request.addParameterOpt(SES_BODY_HTML_PARAM, html.getData());
                    request.addParameterOpt(SES_BODY_HTML_CHARSET_PARAM, html.getCharset());
                }
            }
            // Reply-To
            if (replyToAddresses != null) {
                for (int i = 0; i < replyToAddresses.size(); i++) {
                    final String replyTo = replyToAddresses.get(i);
                    request.addParameterOpt(String.format("%s.%s", SES_REPLY_TO_PARAM, i + 1), replyTo);
                }
            }
            // Return path
            request.addParameterOpt(SES_RETURN_PATH_PARAM, returnPath);
        }
    }.post();
}

From source file:jp.co.hde.mail.ses.CMCAmazonClient.java

License:Apache License

public SendEmailResult sendEmail(SendEmailRequest req) {

    if (req == null) {
        throw new AmazonClientException("SendEmailRequest is null");
    }//from w w w. jav a 2 s .c om

    if (this.hostname == null) {
        throw new AmazonClientException("hostname is null");
    }

    Properties props = new Properties();
    props.put("mail.smtp.host", this.hostname);

    Session session = Session.getInstance(props);
    MimeMessage message = new MimeMessage(session);

    Destination dest = req.getDestination();
    if (dest == null) {
        throw new AmazonClientException("Destination is null");
    }

    try {
        // To
        List<String> toAddrs = dest.getToAddresses();
        if (toAddrs != null && toAddrs.size() > 0) {
            message.setRecipients(RecipientType.TO, toArray(toAddrs));
        } else {
            throw new AmazonClientException("To Address is not exist");
        }

        // Cc
        List<String> ccAddrs = dest.getCcAddresses();
        if (ccAddrs != null && ccAddrs.size() > 0) {
            message.setRecipients(RecipientType.CC, toArray(ccAddrs));
        }

        // Bcc
        List<String> bccAddrs = dest.getBccAddresses();
        if (bccAddrs != null && bccAddrs.size() > 0) {
            message.setRecipients(RecipientType.BCC, toArray(bccAddrs));
        }
    } catch (AddressException e) {
        throw new AmazonClientException("Invalid internet address: " + e.getMessage());
    } catch (MessagingException e) {
        throw new AmazonClientException("setRecipients failed: " + e.getMessage());
    }

    // From
    try {
        message.setFrom(new InternetAddress(req.getSource()));
    } catch (MessagingException e) {
        throw new AmazonClientException("setFrom failed: " + e.getMessage());
    }

    // Date
    try {
        message.setSentDate(new Date());
    } catch (MessagingException e) {
        throw new AmazonClientException("setSentDate failed: " + e.getMessage());
    }

    Message original = req.getMessage();
    if (original != null) {
        // Subject
        try {
            Content subject = original.getSubject();
            if (subject != null) {
                message.setSubject(subject.getData(), subject.getCharset());
            } else {
                message.setSubject("");
            }
        } catch (MessagingException e) {
            throw new AmazonClientException("setSubject failed: " + e.getMessage());
        }

        // Body
        Body body = original.getBody();
        if (body != null) {
            try {
                Content htmlBody = body.getHtml();
                Content textBody = body.getText();
                if (htmlBody != null && textBody != null) {
                    String htmlData = htmlBody.getData();
                    if (htmlData != null && !htmlData.isEmpty()) {
                        // Create multipart message
                        Multipart multipart = new MimeMultipart("alternative");

                        // TextPart
                        MimeBodyPart textPart = new MimeBodyPart();
                        if (textBody != null) {
                            String textData = textBody.getData();
                            if (textData != null && !textData.isEmpty()) {
                                textPart.setText(textData, textBody.getCharset());
                            } else {
                                textPart.setText("");
                            }
                        }
                        // HtmlPart
                        MimeBodyPart htmlPart = new MimeBodyPart();
                        htmlPart.setText(htmlData, htmlBody.getCharset(), "html");
                        htmlPart.addHeader("Content-Transfer-Encoding", "base64");
                        // Add multipart body in the message
                        multipart.addBodyPart(textPart);
                        multipart.addBodyPart(htmlPart);
                        message.setContent(multipart);
                    }
                } else if (htmlBody != null) {
                    message.setText(htmlBody.getData(), htmlBody.getCharset(), "html");
                    if (htmlBody.getCharset() != null
                            && htmlBody.getCharset().equalsIgnoreCase("iso-2022-jp")) {
                        message.addHeader("Content-Transfer-Encoding", "7bit");
                    }
                } else if (textBody != null) {
                    message.setText(textBody.getData(), textBody.getCharset());
                    if (textBody.getCharset() != null
                            && textBody.getCharset().equalsIgnoreCase("iso-2022-jp")) {
                        message.addHeader("Content-Transfer-Encoding", "7bit");
                    }
                } else {
                    throw new AmazonClientException("Message body is not exist");
                }
            } catch (MessagingException e) {
                throw new AmazonClientException("setContent failed: " + e.getMessage());
            }
        } else {
            throw new AmazonClientException("Message body is not exist");
        }

    } else {
        throw new AmazonClientException("Message is not exist");
    }

    // Send email
    try {
        SendEmailResult result = new SendEmailResult();
        if (this.username != null) {
            if (this.password == null) {
                throw new AmazonClientException("SMTP-Auth password is not exist");
            }
            CMCTransport.send(message, this.username, this.password);
        } else {
            CMCTransport.send(message);
        }
        result.setMessageId(message.getMessageID());
        return result;
    } catch (MessagingException e) {
        throw new AmazonClientException("CMCTransport.send failed : " + e.getMessage());
    }
}