Skip to content

Java V2 added SES example that shows new header information #7430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .doc_gen/metadata/sesv2_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ sesv2_SendEmail:
- description: Sends a message using a template.
snippet_tags:
- sesv2.java2.newsletter.SendEmail.template
- description: Sends a message with header information.
snippet_tags:
- ses.java2.send.header.sesv2.main
Rust:
versions:
- sdk_version: 1
Expand Down
5 changes: 3 additions & 2 deletions javav2/example_code/ses/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<source>21</source>
<target>21</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
<plugin>
Expand Down
161 changes: 81 additions & 80 deletions javav2/example_code/ses/src/main/java/com/example/sesv2/SendEmail.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// snippet-start:[ses.java2.sendmessage.sesv2.main]
// snippet-start:[ses.java2.sendmessage.sesv2.import]

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sesv2.model.Body;
import software.amazon.awssdk.services.sesv2.model.Content;
Expand All @@ -19,93 +20,93 @@
/**
* Before running this AWS SDK for Java (v2) example, set up your development
* environment, including your credentials.
*
* <p>
* For more information, see the following documentation topic:
*
* <p>
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/

public class SendEmail {
public static void main(String[] args) {
final String usage = """

Usage:
<sender> <recipient> <subject>\s

Where:
sender - An email address that represents the sender.\s
recipient - An email address that represents the recipient.\s
subject - The subject line.\s
""";

if (args.length != 3) {
System.out.println(usage);
System.exit(1);
}

String sender = args[0];
String recipient = args[1];
String subject = args[2];

Region region = Region.US_EAST_1;
SesV2Client sesv2Client = SesV2Client.builder()
.region(region)
.build();

// The HTML body of the email.
String bodyHTML = "<html>" + "<head></head>" + "<body>" + "<h1>Hello!</h1>"
+ "<p> See the list of customers.</p>" + "</body>" + "</html>";

send(sesv2Client, sender, recipient, subject, bodyHTML);
public static void main(String[] args) {
final String usage = """

Usage:
<sender> <recipient> <subject>\s

Where:
sender - An email address that represents the sender.\s
recipient - An email address that represents the recipient.\s
subject - The subject line.\s
""";

if (args.length != 3) {
System.out.println(usage);
System.exit(1);
}

public static void send(SesV2Client client,
String sender,
String recipient,
String subject,
String bodyHTML) {

Destination destination = Destination.builder()
.toAddresses(recipient)
.build();

Content content = Content.builder()
.data(bodyHTML)
.build();

Content sub = Content.builder()
.data(subject)
.build();

Body body = Body.builder()
.html(content)
.build();

Message msg = Message.builder()
.subject(sub)
.body(body)
.build();

EmailContent emailContent = EmailContent.builder()
.simple(msg)
.build();

SendEmailRequest emailRequest = SendEmailRequest.builder()
.destination(destination)
.content(emailContent)
.fromEmailAddress(sender)
.build();

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

} catch (SesV2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
String sender = args[0];
String recipient = args[1];
String subject = args[2];

Region region = Region.US_EAST_1;
SesV2Client sesv2Client = SesV2Client.builder()
.region(region)
.build();

// The HTML body of the email.
String bodyHTML = "<html>" + "<head></head>" + "<body>" + "<h1>Hello!</h1>"
+ "<p> See the list of customers.</p>" + "</body>" + "</html>";

send(sesv2Client, sender, recipient, subject, bodyHTML);
}

public static void send(SesV2Client client,
String sender,
String recipient,
String subject,
String bodyHTML) {

Destination destination = Destination.builder()
.toAddresses(recipient)
.build();

Content content = Content.builder()
.data(bodyHTML)
.build();

Content sub = Content.builder()
.data(subject)
.build();

Body body = Body.builder()
.html(content)
.build();

Message msg = Message.builder()
.subject(sub)
.body(body)
.build();

EmailContent emailContent = EmailContent.builder()
.simple(msg)
.build();

SendEmailRequest emailRequest = SendEmailRequest.builder()
.destination(destination)
.content(emailContent)
.fromEmailAddress(sender)
.build();

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

} catch (SesV2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}
// snippet-end:[ses.java2.sendmessage.sesv2.main]
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.sesv2;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sesv2.SesV2Client;
import software.amazon.awssdk.services.sesv2.model.EmailContent;
import software.amazon.awssdk.services.sesv2.model.Message;
import software.amazon.awssdk.services.sesv2.model.MessageHeader;
import software.amazon.awssdk.services.sesv2.model.SendEmailRequest;
import software.amazon.awssdk.services.sesv2.model.SendEmailResponse;
import software.amazon.awssdk.services.sesv2.model.SesV2Exception;

import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Before running this AWS SDK for Java (v2) example, set up your development
* environment, including your credentials.
* <p>
* For more information, see the following documentation topic:
* <p>
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/

// snippet-start:[ses.java2.send.header.sesv2.main]
public class SendwithHeader {

public static void main(String[] args) {
final String usage = """

Usage:
<sender> <recipient> <subject>\s

Where:
sender - An email address that represents the sender.\s
recipient - An email address that represents the recipient.\s
subject - The subject line.\s
""";

if (args.length != 3) {
System.out.println(usage);
System.exit(1);
}

String sender = args[0];
String recipient = args[1];
String subject = args[2];
Region region = Region.US_EAST_1;
SesV2Client sesv2Client = SesV2Client.builder()
.region(region)
.build();

String bodyHTML = """
<html>
<head></head>
<body>
<h1>Hello!</h1>
<p>See the list of customers.</p>
</body>
</html>
""";

sendWithHeader(sesv2Client, sender, recipient, subject, bodyHTML);
sesv2Client.close();
}

/**
* Sends an email using the AWS SES V2 client.
*
* @param sesv2Client the SES V2 client to use for sending the email
* @param sender the email address of the sender
* @param recipient the email address of the recipient
* @param subject the subject of the email
* @param bodyHTML the HTML content of the email body
*/
public static void sendWithHeader(SesV2Client sesv2Client,
String sender,
String recipient,
String subject,
String bodyHTML) {
EmailContent emailContent = EmailContent.builder()
.simple(Message.builder()
.body(b -> b.html(c -> c.charset(UTF_8.name()).data(bodyHTML))
.text(c -> c.charset(UTF_8.name()).data(bodyHTML)))
.subject(c -> c.charset(UTF_8.name()).data(subject))
.headers(List.of(
MessageHeader.builder()
.name("List-Unsubscribe")
.value("<https://nutrition.co/?address=x&topic=x>, <mailto:unsubscribe@nutrition.co?subject=TopicUnsubscribe>")
.build(),
MessageHeader.builder()
.name("List-Unsubscribe-Post")
.value("List-Unsubscribe=One-Click")
.build()))
.build())
.build();

SendEmailRequest request = SendEmailRequest.builder()
.fromEmailAddress(sender)
.destination(d -> d.toAddresses(recipient))
.content(emailContent)
.build();

try {
SendEmailResponse response = sesv2Client.sendEmail(request);
System.out.println("Email sent! Message ID: " + response.messageId());
} catch (SesV2Exception e) {
System.err.println("Failed to send email: " + e.awsErrorDetails().errorMessage());
throw new RuntimeException(e);
}
}
}
// snippet-end:[ses.java2.send.header.sesv2.main]
Loading