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

Source Link

Document

Default constructor for SendRawEmailRequest object.

Usage

From source file:com.hiveTown.util.AmazonSESUtil.java

License:Open Source License

public String sendRawEmail(String[] toEmail, String fromAddress, MimeMessage mimeMessage) {
    try {//from ww w .  j a v a 2 s.  c  o  m
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        mimeMessage.writeTo(outputStream);
        RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));

        // Assemble the email.
        SendRawEmailRequest request = new SendRawEmailRequest().withSource(fromAddress)
                .withDestinations(toEmail).withRawMessage(rawMessage);

        // Send the email.
        SendRawEmailResult r = this.getClient().sendRawEmail(request);

        LOGGER.info(r.toString());
        return r.getMessageId();

    } catch (Exception ex) {
        LOGGER.error(ex.toString());
    }
    return null;
}

From source file:com.netflix.iep.ses.EmailRequestBuilder.java

License:Apache License

/**
 * Creates the raw email request for use with
 * {@link com.amazonaws.services.simpleemail.AmazonSimpleEmailService#sendRawEmail(SendRawEmailRequest)}.
 *
 * @deprecated Use {@link #toByteBuffer()} instead and construct the {@code RawMessage}
 * object. This allows the builder to be used with either v1 or v2 of the AWS SDK for Java.
 * In iep version 2.0 this method and the explicit dependency on v1 of the AWS SDK will be
 * removed./* www.j av  a2  s.  c o m*/
 */
@Deprecated
public SendRawEmailRequest build() {
    return new SendRawEmailRequest().withRawMessage(toRawMessage());
}

From source file:fr.xebia.demo.amazon.aws.AmazonAwsIamAccountCreatorV2.java

License:Apache License

/**
 * Send email with Amazon Simple Email Service.
 * <p/>/*from  w  w  w .  j ava 2s.co m*/
 * 
 * Please note that the sender (ie 'from') must be a verified address (see
 * {@link AmazonSimpleEmailService#verifyEmailAddress(com.amazonaws.services.simpleemail.model.VerifyEmailAddressRequest)}
 * ).
 * <p/>
 * 
 * Please note that the sender is a CC of the meail to ease support.
 * <p/>
 * 
 * @param subject
 * @param body
 * @param from
 * @param toAddresses
 * @throws MessagingException
 * @throws AddressException
 */
public void sendEmail(String subject, String body, AccessKey accessKey, KeyPair sshKeyPair,
        java.security.KeyPair x509KeyPair, X509Certificate x509Certificate,
        SigningCertificate signingCertificate, String from, String toAddress) {

    try {
        Session s = Session.getInstance(new Properties(), null);
        MimeMessage msg = new MimeMessage(s);

        msg.setFrom(new InternetAddress(from));
        msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toAddress));
        msg.addRecipient(javax.mail.Message.RecipientType.CC, new InternetAddress(from));

        msg.setSubject(subject);

        MimeMultipart mimeMultiPart = new MimeMultipart();
        msg.setContent(mimeMultiPart);

        // body
        BodyPart plainTextBodyPart = new MimeBodyPart();
        mimeMultiPart.addBodyPart(plainTextBodyPart);
        plainTextBodyPart.setContent(body, "text/plain");

        // aws-credentials.txt / accessKey
        {
            BodyPart awsCredentialsBodyPart = new MimeBodyPart();
            awsCredentialsBodyPart.setFileName("aws-credentials.txt");
            StringWriter awsCredentialsStringWriter = new StringWriter();
            PrintWriter awsCredentials = new PrintWriter(awsCredentialsStringWriter);
            awsCredentials
                    .println("#Insert your AWS Credentials from http://aws.amazon.com/security-credentials");
            awsCredentials.println("#" + new DateTime());
            awsCredentials.println();
            awsCredentials.println("# ec2, rds & elb tools use accessKey and secretKey");
            awsCredentials.println("accessKey=" + accessKey.getAccessKeyId());
            awsCredentials.println("secretKey=" + accessKey.getSecretAccessKey());
            awsCredentials.println();
            awsCredentials.println("# iam tools use AWSAccessKeyId and AWSSecretKey");
            awsCredentials.println("AWSAccessKeyId=" + accessKey.getAccessKeyId());
            awsCredentials.println("AWSSecretKey=" + accessKey.getSecretAccessKey());

            awsCredentialsBodyPart.setContent(awsCredentialsStringWriter.toString(), "text/plain");
            mimeMultiPart.addBodyPart(awsCredentialsBodyPart);
        }
        // private ssh key
        {
            BodyPart keyPairBodyPart = new MimeBodyPart();
            keyPairBodyPart.setFileName(sshKeyPair.getKeyName() + ".pem.txt");
            keyPairBodyPart.setContent(sshKeyPair.getKeyMaterial(), "application/octet-stream");
            mimeMultiPart.addBodyPart(keyPairBodyPart);
        }

        // x509 private key
        {
            BodyPart x509PrivateKeyBodyPart = new MimeBodyPart();
            x509PrivateKeyBodyPart.setFileName("pk-" + signingCertificate.getCertificateId() + ".pem.txt");
            String x509privateKeyPem = Pems.pem(x509KeyPair.getPrivate());
            x509PrivateKeyBodyPart.setContent(x509privateKeyPem, "application/octet-stream");
            mimeMultiPart.addBodyPart(x509PrivateKeyBodyPart);
        }
        // x509 private key
        {
            BodyPart x509CertificateBodyPart = new MimeBodyPart();
            x509CertificateBodyPart.setFileName("cert-" + signingCertificate.getCertificateId() + ".pem.txt");
            String x509CertificatePem = Pems.pem(x509Certificate);
            x509CertificateBodyPart.setContent(x509CertificatePem, "application/octet-stream");
            mimeMultiPart.addBodyPart(x509CertificateBodyPart);
        }
        // Convert to raw message
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        msg.writeTo(out);

        RawMessage rawMessage = new RawMessage();
        rawMessage.setData(ByteBuffer.wrap(out.toString().getBytes()));

        ses.sendRawEmail(new SendRawEmailRequest().withRawMessage(rawMessage));
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}