Example usage for com.amazonaws.services.simpleemail.model SendEmailResult SendEmailResult

List of usage examples for com.amazonaws.services.simpleemail.model SendEmailResult SendEmailResult

Introduction

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

Prototype

SendEmailResult

Source Link

Usage

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");
    }//w  ww  . j  a  va2 s.  c  o m

    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());
    }
}

From source file:org.apache.camel.component.aws.ses.AmazonSESClientMock.java

License:Apache License

@Override
public SendEmailResult sendEmail(SendEmailRequest sendEmailRequest)
        throws AmazonServiceException, AmazonClientException {
    this.sendEmailRequest = sendEmailRequest;
    SendEmailResult result = new SendEmailResult();
    result.setMessageId("1");

    return result;
}