基于AWS SES批量模板邮件的Java SDK实现及List-Unsubscribe头配置咨询
AWS SES Bulk Templated Emails with Per-Recipient List-Unsubscribe Headers (Java SDK)
Short Answer
Yes, you can add per-recipient custom headers like List-Unsubscribe when using SES Bulk Templated Emails. This approach will drastically cut down your data transfer costs compared to sending individual raw emails—since you only transmit the template content once to SES, plus tiny personalized snippets for each recipient instead of the full 150KB message 35,000 times.
Java SDK (v1) Implementation Example
Let’s walk through a complete, working example that matches your existing code style:
1. Prerequisites
- You’ve already created an SES email template (via AWS Console or API) with placeholders for personalized content (e.g.,
{{name}},{{unsubscribeUrl}}). - Your SES client is properly configured with valid credentials and your target region.
2. Full Code Example
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService; import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder; import com.amazonaws.services.simpleemail.model.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class SesBulkTemplateSender { public static void main(String[] args) { // Initialize SES client (replace with your region) AmazonSimpleEmailService sesClient = AmazonSimpleEmailServiceClientBuilder.standard() .withRegion("us-east-1") .build(); // Your existing SES template name String templateName = "Your_Marketing_Template"; // Sender details (use your verified domain/email) String sender = "Your Brand Name <marketing@your-domain.com>"; // Prepare bulk recipient list List<BulkEmailDestination> bulkDestinations = new ArrayList<>(); // Loop through your 35k subscribers for (Map.Entry<Integer, String> emailEntry : email_ids.entrySet()) { int userId = emailEntry.getKey(); String recipientEmail = emailEntry.getValue(); // 1. Personalized data for the template (matches your template's placeholders) String templateData = String.format( "{\"userName\": \"Valued Subscriber\", \"unsubscribeUrl\": \"https://your-domain.com/unsubscribe/%d\"}", userId ); // 2. Add per-recipient List-Unsubscribe header (follows RFC standard) Map<String, String> customHeaders = new HashMap<>(); customHeaders.put("List-Unsubscribe", String.format( "<https://your-domain.com/unsubscribe/%d>, mailto:unsubscribe@your-domain.com?user=%d", userId, userId )); // 3. Build the destination entry for this recipient BulkEmailDestination destination = new BulkEmailDestination() .withDestination(new Destination().withToAddresses(recipientEmail)) .withTemplateData(templateData) .withCustomHeaders(customHeaders); bulkDestinations.add(destination); } // Assemble and send the bulk request SendBulkTemplatedEmailRequest bulkRequest = new SendBulkTemplatedEmailRequest() .withSource(sender) .withTemplate(templateName) .withDestinations(bulkDestinations); try { SendBulkTemplatedEmailResult result = sesClient.sendBulkTemplatedEmail(bulkRequest); System.out.println("Bulk email sent! Statuses: " + result.getStatuses()); } catch (AmazonSimpleEmailServiceException e) { System.err.println("Error sending bulk email: " + e.getMessage()); System.err.println("Error code: " + e.getErrorCode()); } } }
Key Details for List-Unsubscribe Headers
- RFC Compliance: The header must use the format
<URL>, mailto:emailto work with most email clients. This gives users both a one-click unsubscribe link and an email option. - Per-Recipient Uniqueness: By defining
customHeadersfor eachBulkEmailDestination, you can tie the unsubscribe link directly to the user’s ID—critical for compliance (like CAN-SPAM) and accurate subscription management.
Cost Savings Breakdown
Instead of sending 150KB × 35,000 = ~5.25GB of data, you’ll send:
- The template content once (stored in SES, no repeated transfers)
- For each recipient: a tiny JSON payload (~100-200 bytes) plus header data
This reduces your data transfer volume by 99%+, leading to massive cost cuts.
Quick Notes
- If you’re using AWS SDK for Java v2, the logic is nearly identical—just use
SesClientinstead ofAmazonSimpleEmailService(the request/response objects likeSendBulkTemplatedEmailRequestare still available). - Make sure your SES template includes all static content (images, body text) so you don’t have to retransmit it per recipient.
- Double-check your domain’s DKIM/SPF authentication to keep deliverability high.
内容的提问来源于stack exchange,提问作者Kalesh Kaladharan
相关产品推荐
相关产品推荐

