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

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

Introduction

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

Prototype

public SendRawEmailRequest(RawMessage rawMessage) 

Source Link

Document

Constructs a new SendRawEmailRequest object.

Usage

From source file:com.irurueta.server.commons.email.AWSMailSender.java

License:Apache License

/**
 * Method to send a text email with attachments or HTML emails with 
 * attachments (inline or not)./*ww w . j  a  v a  2  s.  c  o m*/
 * @param m email message to be sent.
 * @return id of message that has been sent.
 * @throws MailNotSentException if mail couldn't be sent.
 */
private String sendRawEmail(EmailMessage<MimeMessage> m) throws MailNotSentException {

    String messageId;
    long currentTimestamp = System.currentTimeMillis();
    prepareClient();
    if (!mEnabled) {
        //don't send message if not enabled
        return null;
    }

    try {
        synchronized (this) {
            //prevents throttling and excessive memory usage
            checkQuota(currentTimestamp);

            //if no subject, set to empty string to avoid errors
            if (m.getSubject() == null) {
                m.setSubject("");
            }

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            //convert message into mime multi part and write it to output
            //stream into memory
            Session session = Session.getInstance(new Properties());
            MimeMessage mimeMessage = new MimeMessage(session);
            if (m.getSubject() != null) {
                mimeMessage.setSubject(m.getSubject(), "utf-8");
            }
            m.buildContent(mimeMessage);
            mimeMessage.writeTo(outputStream);
            //m.buildMultipart().writeTo(outputStream);

            RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));

            SendRawEmailRequest rawRequest = new SendRawEmailRequest(rawMessage);
            rawRequest.setDestinations(m.getTo());
            rawRequest.setSource(mMailFromAddress);
            SendRawEmailResult result = mClient.sendRawEmail(rawRequest);
            messageId = result.getMessageId();

            //update timestamp of last sent email
            mLastSentMailTimestamp = System.currentTimeMillis();

            //wait to avoid throwttling exceptions to avoid making any
            //further requests
            this.wait(mWaitIntervalMillis);
        }
    } catch (Throwable t) {
        throw new MailNotSentException(t);
    }

    return messageId;
}

From source file:com.netflix.ice.basic.BasicWeeklyCostEmailService.java

License:Apache License

private void sendEmail(boolean test, AmazonSimpleEmailServiceClient emailService, String email,
        List<ApplicationGroup> appGroups) throws IOException, MessagingException {

    StringBuilder body = new StringBuilder();
    body.append(// w ww.j a v a 2  s. co m
            "<html><head><style type=\"text/css\">a:link, a:visited{color:#006DBA;}a:link, a:visited, a:hover {\n"
                    + "text-decoration: none;\n" + "}\n" + "body {\n" + "color: #333;\n" + "}"
                    + "</style></head>");
    List<MimeBodyPart> mimeBodyParts = Lists.newArrayList();
    int index = 0;
    String subject = "";
    for (ApplicationGroup appGroup : appGroups) {
        boolean hasData = false;
        for (String prodName : appGroup.data.keySet()) {
            if (config.productService.getProductByName(prodName) == null)
                continue;
            hasData = appGroup.data.get(prodName) != null && appGroup.data.get(prodName).size() > 0;
            if (hasData)
                break;
        }
        if (!hasData)
            continue;

        try {
            MimeBodyPart mimeBodyPart = constructEmail(index, appGroup, body);
            index++;
            if (mimeBodyPart != null) {
                mimeBodyParts.add(mimeBodyPart);
                subject = subject + (subject.length() > 0 ? ", " : "") + appGroup.getDisplayName();
            }
        } catch (Exception e) {
            logger.error("Error contructing email", e);
        }
    }
    body.append("</html>");

    if (mimeBodyParts.size() == 0)
        return;

    DateTime end = new DateTime(DateTimeZone.UTC).withDayOfWeek(1).withMillisOfDay(0);
    subject = String.format("%s Weekly AWS Costs (%s - %s)", subject, formatter.print(end.minusWeeks(1)),
            formatter.print(end));
    String toEmail = test ? testEmail : email;
    Session session = Session.getInstance(new Properties());
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setSubject(subject);
    mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, toEmail);
    if (!test && !StringUtils.isEmpty(bccEmail)) {
        mimeMessage.addRecipients(Message.RecipientType.BCC, bccEmail);
    }
    MimeMultipart mimeMultipart = new MimeMultipart();
    BodyPart p = new MimeBodyPart();
    p.setContent(body.toString(), "text/html");
    mimeMultipart.addBodyPart(p);

    for (MimeBodyPart mimeBodyPart : mimeBodyParts)
        mimeMultipart.addBodyPart(mimeBodyPart);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    mimeMessage.setContent(mimeMultipart);
    mimeMessage.writeTo(outputStream);
    RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));

    SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage);
    rawEmailRequest.setDestinations(Lists.<String>newArrayList(toEmail));
    rawEmailRequest.setSource(fromEmail);
    logger.info("sending email to " + toEmail + " " + body.toString());
    emailService.sendRawEmail(rawEmailRequest);
}

From source file:es.logongas.fpempresa.service.mail.impl.MailServiceImplAWS.java

License:Open Source License

@Override
public void send(Mail mail) {
    try {//from  www . j a  v a2s .co m
        Session session = Session.getDefaultInstance(new Properties());

        Message message = JavaMailHelper.getMessage(mail, session);

        //Aqu es el proceso de envio
        AWSCredentials credentials = new BasicAWSCredentials(Config.getSetting("aws.accessKey"),
                Config.getSetting("aws.secretKey"));
        AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceAsyncClient(credentials);
        Region REGION = Region.getRegion(Regions.EU_WEST_1);
        client.setRegion(REGION);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        message.writeTo(outputStream);
        RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
        SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage);
        client.sendRawEmail(rawEmailRequest);
    } catch (IllegalArgumentException | IOException | MessagingException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.springframework.cloud.aws.mail.simplemail.SimpleEmailServiceJavaMailSender.java

License:Apache License

@SuppressWarnings("OverloadedVarargsMethod")
@Override/*  w ww  .j a va  2  s  . c o  m*/
public void send(MimeMessage... mimeMessages) throws MailException {
    Map<Object, Exception> failedMessages = new HashMap<>();

    for (MimeMessage mimeMessage : mimeMessages) {
        try {
            RawMessage rm = createRawMessage(mimeMessage);
            SendRawEmailResult sendRawEmailResult = getEmailService().sendRawEmail(new SendRawEmailRequest(rm));
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Message with id: {0} successfully send", sendRawEmailResult.getMessageId());
            }
        } catch (Exception e) {
            //Ignore Exception because we are collecting and throwing all if any
            //noinspection ThrowableResultOfMethodCallIgnored
            failedMessages.put(mimeMessage, e);
        }
    }

    if (!failedMessages.isEmpty()) {
        throw new MailSendException(failedMessages);
    }
}

From source file:org.springframework.integration.aws.ses.core.AmazonSESMailSenderImpl.java

License:Apache License

public void send(MimeMessage[] mailMessages) {
    if (mailMessages != null && mailMessages.length > 0) {
        for (MimeMessage mailMessage : mailMessages) {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                mailMessage.writeTo(baos);
                RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(baos.toByteArray()));
                SendRawEmailRequest rawMail = new SendRawEmailRequest(rawMessage);
                emailService.sendRawEmail(rawMail);
            } catch (Exception e) {
                throw new AmazonSESMailSendException(credentials.getAccessKey(),
                        "Exception Caught with message \"" + e.getMessage() + "\" while sending raw mail", e,
                        null);/*from  ww  w. ja v  a2s .  c o  m*/
            }
        }
    }

}