-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
scmacdon
wants to merge
1
commit into
awsdocs:main
Choose a base branch
from
scmacdon:sesFix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
javav2/example_code/ses/src/main/java/com/example/sesv2/SendwithHeader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.