使用AWS Java SDK调用Amazon Pinpoint发邮件报错求助:需包含邮件消息
Hey Steve, let's work through this Pinpoint issue you're facing—I’ve dealt with similar headaches before, so here’s what you need to check to fix that "must include an email message" error:
This error almost always boils down to missing or misconfigured email content in your DirectMessageConfiguration, even if your endpoint and verified email look good. Pinpoint requires explicit, valid email content to trigger a send.
Key Checks to Resolve the Issue
- Ensure EmailMessage has required fields: Pinpoint mandates at least a
SubjectAND either a plain-text or HTML body. Empty values here will trigger the error. - Properly link EmailMessage to DirectMessageConfiguration: You have to explicitly attach your built email content to the direct message configuration—this is the most commonly missed step.
Full Working Code Example
Here’s a complete Java snippet that follows Pinpoint’s requirements for direct email sends:
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.*; import java.util.Map; public class PinpointDirectEmailSender { public static void main(String[] args) { // Replace these with your actual values String pinpointAppId = "YOUR_PINPOINT_APPLICATION_ID"; String targetEndpointId = "YOUR_VALIDATED_ENDPOINT_ID"; // Initialize Pinpoint client PinpointClient pinpointClient = PinpointClient.builder() .region(Region.US_EAST_1) // Swap with your AWS region .build(); // Build the core email content EmailMessage emailContent = EmailMessage.builder() .subject(EmailContent.builder().data("Test Direct Email from Pinpoint").build()) .body(EmailBody.builder() .text(EmailContent.builder().data("This is a test message sent via AWS Java SDK.").build()) // Optional: Add HTML body if needed //.html(EmailContent.builder().data("<h1>HTML Test Content</h1>").build()) .build()) .build(); // Configure direct message to use the email content DirectMessageConfiguration directMsgConfig = DirectMessageConfiguration.builder() .emailMessage(emailContent) // Critical: Don't skip this line! .build(); // Assemble the send request SendUsersMessageRequest sendRequest = SendUsersMessageRequest.builder() .applicationId(pinpointAppId) .sendUsersMessageConfiguration(SendUsersMessageConfiguration.builder() .directMessageConfiguration(directMsgConfig) .users(Map.of(targetEndpointId, EndpointSendConfiguration.builder().build())) .build()) .build(); // Execute the send try { SendUsersMessageResponse response = pinpointClient.sendUsersMessage(sendRequest); System.out.println("Message sent successfully! ID: " + response.messageId()); } catch (PinpointException e) { System.err.println("Error details: " + e.awsErrorDetails().errorMessage()); System.exit(1); } finally { pinpointClient.close(); } } }
Common Mistakes to Avoid
- Forgot to attach emailMessage: If your
DirectMessageConfigurationis initialized without linking theEmailMessage, Pinpoint won’t recognize any email content—this is the #1 cause of this error. - Empty content values: A blank subject or empty body text/HTML will trigger the same error. Double-check that your content strings have actual text.
- Wrong message type: Accidentally setting
smsMessageinstead ofemailMessage(or vice versa) will make Pinpoint ignore your email content entirely.
If you still run into issues after adjusting for these points, verify you’re using the latest AWS Java SDK v2, and that your IAM role has the mobiletargeting:SendUsersMessages permission.
内容的提问来源于stack exchange,提问作者Steve

