Search in sources :

Example 1 with SesClient

use of software.amazon.awssdk.services.ses.SesClient in project aws-doc-sdk-examples by awsdocs.

the class SendNotifications method handleEmailMessage.

public String handleEmailMessage(String myDom) throws JDOMException, IOException, MessagingException {
    String myEmail = "";
    SesClient client = SesClient.builder().region(Region.US_EAST_1).build();
    SAXBuilder builder = new SAXBuilder();
    Document jdomDocument = builder.build(new InputSource(new StringReader(myDom)));
    org.jdom2.Element root = jdomDocument.getRootElement();
    // Get the list of children elements.
    List<org.jdom2.Element> students = root.getChildren("Student");
    for (org.jdom2.Element element : students) {
        myEmail = element.getChildText("Email");
        sendEmailMessage(client, myEmail);
    }
    client.close();
    return myEmail;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) InputSource(org.xml.sax.InputSource) SesClient(software.amazon.awssdk.services.ses.SesClient) StringReader(java.io.StringReader) Document(org.jdom2.Document)

Example 2 with SesClient

use of software.amazon.awssdk.services.ses.SesClient in project aws-doc-sdk-examples by awsdocs.

the class SendMessages method send.

public void send(byte[] attachment, String emailAddress) throws MessagingException, IOException {
    MimeMessage message = null;
    Session session = Session.getDefaultInstance(new Properties());
    // Create a new MimeMessage object.
    message = new MimeMessage(session);
    // Add subject, from, and to lines.
    message.setSubject(subject, "UTF-8");
    message.setFrom(new InternetAddress(sender));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAddress));
    // Create a multipart/alternative child container.
    MimeMultipart msgBody = new MimeMultipart("alternative");
    // Create a wrapper for the HTML and text parts.
    MimeBodyPart wrap = new MimeBodyPart();
    // Define the text part.
    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setContent(bodyText, "text/plain; charset=UTF-8");
    // Define the HTML part.
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(bodyHTML, "text/html; charset=UTF-8");
    // Add the text and HTML parts to the child container.
    msgBody.addBodyPart(textPart);
    msgBody.addBodyPart(htmlPart);
    // Add the child container to the wrapper object.
    wrap.setContent(msgBody);
    // Create a multipart/mixed parent container.
    MimeMultipart msg = new MimeMultipart("mixed");
    // Add the parent container to the message.
    message.setContent(msg);
    // Add the multipart/alternative part to the message.
    msg.addBodyPart(wrap);
    // Define the attachment.
    MimeBodyPart att = new MimeBodyPart();
    DataSource fds = new ByteArrayDataSource(attachment, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    att.setDataHandler(new DataHandler(fds));
    String reportName = "WorkReport.xls";
    att.setFileName(reportName);
    // Add the attachment to the message.
    msg.addBodyPart(att);
    // Send the email.
    try {
        System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
        Region region = Region.US_WEST_2;
        SesClient client = SesClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        message.writeTo(outputStream);
        ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());
        byte[] arr = new byte[buf.remaining()];
        buf.get(arr);
        SdkBytes data = SdkBytes.fromByteArray(arr);
        RawMessage rawMessage = RawMessage.builder().data(data).build();
        SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder().rawMessage(rawMessage).build();
        client.sendRawEmail(rawEmailRequest);
    } catch (SesException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    System.out.println("Email sent with attachment");
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) SesClient(software.amazon.awssdk.services.ses.SesClient) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) ByteBuffer(java.nio.ByteBuffer) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) SdkBytes(software.amazon.awssdk.core.SdkBytes) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Region(software.amazon.awssdk.regions.Region) SendRawEmailRequest(software.amazon.awssdk.services.ses.model.SendRawEmailRequest) MimeBodyPart(javax.mail.internet.MimeBodyPart) RawMessage(software.amazon.awssdk.services.ses.model.RawMessage) SesException(software.amazon.awssdk.services.ses.model.SesException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Session(javax.mail.Session)

Example 3 with SesClient

use of software.amazon.awssdk.services.ses.SesClient in project aws-doc-sdk-examples by awsdocs.

the class SendMessage method sendMessage.

public void sendMessage(String email) throws IOException {
    // Sender
    // REPLACE WITH AN EMAIL ADDRESS
    String sender = "<SPECIFY an EMAIL ADDRESS>";
    String subject = "New Case";
    // The email body for recipients with non-HTML email clients.
    String bodyText = "Hello,\r\n" + "You are assigned a new case";
    // The HTML body of the email.
    String bodyHTML = "<html>" + "<head></head>" + "<body>" + "<h1>Hello!</h1>" + "<p>Please check the database for new ticket assigned to you.</p>" + "</body>" + "</html>";
    Region region = Region.US_WEST_2;
    SesClient client = SesClient.builder().region(region).build();
    try {
        send(client, sender, email, subject, bodyText, bodyHTML);
    } catch (IOException | MessagingException e) {
        e.getStackTrace();
    }
}
Also used : MessagingException(javax.mail.MessagingException) SesClient(software.amazon.awssdk.services.ses.SesClient) Region(software.amazon.awssdk.regions.Region) IOException(java.io.IOException)

Example 4 with SesClient

use of software.amazon.awssdk.services.ses.SesClient in project aws-doc-sdk-examples by awsdocs.

the class SendMessage method send.

public void send(byte[] attachment, String emailAddress) throws MessagingException, IOException {
    MimeMessage message = null;
    Session session = Session.getDefaultInstance(new Properties());
    // Create a new MimeMessage object
    message = new MimeMessage(session);
    // Add subject, from and to lines
    message.setSubject(subject, "UTF-8");
    message.setFrom(new InternetAddress(sender));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAddress));
    // Create a multipart/alternative child container
    MimeMultipart msgBody = new MimeMultipart("alternative");
    // Create a wrapper for the HTML and text parts
    MimeBodyPart wrap = new MimeBodyPart();
    // Define the text part
    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setContent(bodyText, "text/plain; charset=UTF-8");
    // Define the HTML part
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(bodyHTML, "text/html; charset=UTF-8");
    // Add the text and HTML parts to the child container
    msgBody.addBodyPart(textPart);
    msgBody.addBodyPart(htmlPart);
    // Add the child container to the wrapper object
    wrap.setContent(msgBody);
    // Create a multipart/mixed parent container
    MimeMultipart msg = new MimeMultipart("mixed");
    // Add the parent container to the message
    message.setContent(msg);
    // Add the multipart/alternative part to the message
    msg.addBodyPart(wrap);
    // Define the attachment
    MimeBodyPart att = new MimeBodyPart();
    DataSource fds = new ByteArrayDataSource(attachment, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    att.setDataHandler(new DataHandler(fds));
    String reportName = "WorkReport.xls";
    att.setFileName(reportName);
    // Add the attachment to the message
    msg.addBodyPart(att);
    // Send the email
    try {
        System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
        Region region = Region.US_WEST_2;
        SesClient client = SesClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        message.writeTo(outputStream);
        ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());
        byte[] arr = new byte[buf.remaining()];
        buf.get(arr);
        SdkBytes data = SdkBytes.fromByteArray(arr);
        RawMessage rawMessage = RawMessage.builder().data(data).build();
        SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder().rawMessage(rawMessage).build();
        client.sendRawEmail(rawEmailRequest);
    } catch (SesException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    System.out.println("Email sent with attachment");
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) SesClient(software.amazon.awssdk.services.ses.SesClient) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) ByteBuffer(java.nio.ByteBuffer) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) SdkBytes(software.amazon.awssdk.core.SdkBytes) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Region(software.amazon.awssdk.regions.Region) SendRawEmailRequest(software.amazon.awssdk.services.ses.model.SendRawEmailRequest) MimeBodyPart(javax.mail.internet.MimeBodyPart) RawMessage(software.amazon.awssdk.services.ses.model.RawMessage) SesException(software.amazon.awssdk.services.ses.model.SesException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Session(javax.mail.Session)

Example 5 with SesClient

use of software.amazon.awssdk.services.ses.SesClient in project aws-doc-sdk-examples by awsdocs.

the class SendEmail method sendMsg.

public void sendMsg(Set<String> unqiueKeys) {
    Region region = Region.US_EAST_1;
    SesClient client = SesClient.builder().region(region).build();
    String sender = "<Enter the sender email address>";
    String recipient = "<Enter the recipient email address>";
    // Set the HTML body.
    String bodyHTML = "<html> <head></head> <body><p> The following images contains PPE gear " + "<ol> ";
    // Persist the data into a DynamoDB table.
    for (String myKey : unqiueKeys) {
        bodyHTML = bodyHTML + "<li> " + myKey + "</li>";
    }
    bodyHTML = bodyHTML + "</ol></p></body></html>";
    Destination destination = Destination.builder().toAddresses(recipient).build();
    Content content = Content.builder().data(bodyHTML).build();
    Content sub = Content.builder().data("PPE Information").build();
    Body body = Body.builder().html(content).build();
    Message msg = Message.builder().subject(sub).body(body).build();
    SendEmailRequest emailRequest = SendEmailRequest.builder().destination(destination).message(msg).source(sender).build();
    try {
        System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
        client.sendEmail(emailRequest);
    } catch (SesException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Destination(software.amazon.awssdk.services.ses.model.Destination) Message(software.amazon.awssdk.services.ses.model.Message) Content(software.amazon.awssdk.services.ses.model.Content) SesClient(software.amazon.awssdk.services.ses.SesClient) Region(software.amazon.awssdk.regions.Region) SesException(software.amazon.awssdk.services.ses.model.SesException) Body(software.amazon.awssdk.services.ses.model.Body) SendEmailRequest(software.amazon.awssdk.services.ses.model.SendEmailRequest)

Aggregations

SesClient (software.amazon.awssdk.services.ses.SesClient)12 Region (software.amazon.awssdk.regions.Region)11 SesException (software.amazon.awssdk.services.ses.model.SesException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ByteBuffer (java.nio.ByteBuffer)5 Properties (java.util.Properties)5 DataHandler (javax.activation.DataHandler)5 DataSource (javax.activation.DataSource)5 Session (javax.mail.Session)5 InternetAddress (javax.mail.internet.InternetAddress)5 MimeBodyPart (javax.mail.internet.MimeBodyPart)5 MimeMessage (javax.mail.internet.MimeMessage)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)5 SdkBytes (software.amazon.awssdk.core.SdkBytes)5 RawMessage (software.amazon.awssdk.services.ses.model.RawMessage)5 SendRawEmailRequest (software.amazon.awssdk.services.ses.model.SendRawEmailRequest)5 MessagingException (javax.mail.MessagingException)4 IOException (java.io.IOException)3 StringReader (java.io.StringReader)1