software.amazon.awssdk.services.ses.SesClient Java Examples

The following examples show how to use software.amazon.awssdk.services.ses.SesClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeleteHandler.java    From aws-cloudformation-resource-providers-ses with Apache License 2.0 6 votes vote down vote up
@Override
public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
    final AmazonWebServicesClientProxy proxy,
    final ResourceHandlerRequest<ResourceModel> request,
    final CallbackContext callbackContext,
    final Logger logger) {
    final SesClient client = ClientBuilder.getClient();
    final ResourceModel model = request.getDesiredResourceState();

    try {
        final DeleteConfigurationSetRequest deleteConfigurationSetRequest = DeleteConfigurationSetRequest.builder()
            .configurationSetName(model.getName())
            .build();
        proxy.injectCredentialsAndInvokeV2(deleteConfigurationSetRequest, client::deleteConfigurationSet);
        logger.log(String.format("%s [%s] deleted successfully",
            ResourceModel.TYPE_NAME, getPrimaryIdentifier(model).toString()));
    } catch (final ConfigurationSetDoesNotExistException e) {
        throw new CfnNotFoundException(ResourceModel.TYPE_NAME, model.getName());
    }

    return ProgressEvent.defaultSuccessHandler(null);
}
 
Example #2
Source File: SesResource.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> start() {
    DockerClientFactory.instance().client();
    try {
        services = new LocalStackContainer("0.11.1").withServices(Service.SES);
        services.start();
        StaticCredentialsProvider staticCredentials = StaticCredentialsProvider
            .create(AwsBasicCredentials.create("accesskey", "secretKey"));

        client = SesClient.builder()
            .endpointOverride(new URI(endpoint()))
            .credentialsProvider(staticCredentials)
            .httpClientBuilder(UrlConnectionHttpClient.builder())
            .region(Region.US_EAST_1).build();

        client.verifyEmailIdentity(req -> req.emailAddress(FROM_EMAIL));
        client.verifyEmailIdentity(req -> req.emailAddress(TO_EMAIL));

    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Could not start localstack server", e);
    }

    Map<String, String> properties = new HashMap<>();
    properties.put("quarkus.ses.endpoint-override", endpoint());
    properties.put("quarkus.ses.aws.region", "us-east-1");
    properties.put("quarkus.ses.aws.credentials.type", "static");
    properties.put("quarkus.ses.aws.credentials.static-provider.access-key-id", "accessKey");
    properties.put("quarkus.ses.aws.credentials.static-provider.secret-access-key", "secretKey");

    return properties;
}
 
Example #3
Source File: SesRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public RuntimeValue<AwsClientBuilder> createSyncBuilder(SesConfig config, RuntimeValue<SdkHttpClient.Builder> transport) {
    SesClientBuilder builder = SesClient.builder();
    if (transport != null) {
        builder.httpClientBuilder(transport.getValue());
    }
    return new RuntimeValue<>(builder);
}
 
Example #4
Source File: SesRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public RuntimeValue<SesClient> buildClient(RuntimeValue<? extends AwsClientBuilder> builder,
        BeanContainer beanContainer,
        ShutdownContext shutdown) {
    SesClientProducer producer = beanContainer.instance(SesClientProducer.class);
    producer.setSyncConfiguredBuilder((SesClientBuilder) builder.getValue());
    shutdown.addShutdownTask(producer::destroy);
    return new RuntimeValue<>(producer.client());
}
 
Example #5
Source File: SendMessage.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public void sendMessage(String email) throws IOException {


        //Sender
        String sender = "<SPECIFY an EMAIL ADDRESS>" ; // REPLACE WITH 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();
        }
    }
 
Example #6
Source File: ClientBuilder.java    From aws-cloudformation-resource-providers-ses with Apache License 2.0 4 votes vote down vote up
static SesClient getClient() {
    return SesClient.builder()
        .httpClient(LambdaWrapper.HTTP_CLIENT)
        .build();
}
 
Example #7
Source File: SesClientFactory.java    From micronaut-aws with Apache License 2.0 4 votes vote down vote up
@Override
protected SesClientBuilder createSyncBuilder() {
    return SesClient.builder();
}
 
Example #8
Source File: SesClientFactory.java    From micronaut-aws with Apache License 2.0 4 votes vote down vote up
@Override
@Bean(preDestroy = "close")
@Singleton
public SesClient syncClient(SesClientBuilder builder) {
    return super.syncClient(builder);
}
 
Example #9
Source File: SesProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
protected DotName syncClientName() {
    return DotName.createSimple(SesClient.class.getName());
}
 
Example #10
Source File: SesClientProducer.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Produces
@ApplicationScoped
public SesClient client() {
    client = syncConfiguredBuilder.build();
    return client;
}
 
Example #11
Source File: SendMessageAttachment.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void send(byte[] attachment) throws AddressException, MessagingException, IOException {

        Session session = Session.getDefaultInstance(new Properties());

        // Create a new MimeMessage object
        MimeMessage 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(RECIPIENT));

        // 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(BODY_TEXT, "text/plain; charset=UTF-8");

        // Define the HTML part
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(BODY_HTML, "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));

        // Set the attachment name
        String reportName = "WorkReport.xls";
        att.setFileName(reportName);

        // Add the attachment to the message
        msg.addBodyPart(att);

        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().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 (SdkException e) {
            e.getStackTrace();
        }
        // snippet-end:[ses.java2.sendmessageattachment.main]
    }
 
Example #12
Source File: SendMessage.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void send() throws AddressException, MessagingException, IOException {

        Session session = Session.getDefaultInstance(new Properties());

        // Create a new MimeMessage object
        MimeMessage 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(RECIPIENT));

        // 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(BODY_TEXT, "text/plain; charset=UTF-8");

        // Define the HTML part
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(BODY_HTML, "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);


        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().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 (SdkException e) {
            e.getStackTrace();
        }
        // snippet-end:[ses.java2.sendmessage.main]
    }
 
Example #13
Source File: SendMessage.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void send(SesClient client,
                        String sender,
                        String recipient,
                        String subject,
                        String bodyText,
                        String bodyHTML
) throws AddressException, MessagingException, IOException {

    Session session = Session.getDefaultInstance(new Properties());

    // Create a new MimeMessage object.
    MimeMessage 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(recipient));

    // 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);

    try {
        System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");

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


}
 
Example #14
Source File: AWSEmailer.java    From para with Apache License 2.0 4 votes vote down vote up
/**
 * No-args constructor.
 */
public AWSEmailer() {
	sesclient = SesClient.builder().
			// AWS SES is not available in all regions and it's best if we set it manually
			region(Region.of(Config.getConfigParam("aws_ses_region", "eu-west-1"))).build();
}