我想利用增加的 SES 发送限制,从 10MB 到现在最高 40MB since September 2021以电子邮件附件形式发送较大的 Excel 文件。
我用了official code example但不幸的是,我的大小不能超过 10MB。
我收到错误:
Message length is more than 10485760 bytes long: 12148767
我使用的是最新版本的 software.amazon.awssdk:ses
,即 2.17.196。
static Region region = Region.EU_CENTRAL_1;
static SesClient client = SesClient.builder().region(region).build();
public static void sendemailAttachment(SesClient client,
String sender,
String recipient,
String subject,
String bodyText,
String bodyHTML,
String fileName, // must include .xlsx
String fileLocation) throws AddressException, MessagingException, IOException {
java.io.File theFile = new java.io.File(fileLocation);
byte[] fileContent = Files.readAllBytes(theFile.toPath());
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);
// Define the attachment
MimeBodyPart att = new MimeBodyPart();
DataSource fds = new ByteArrayDataSource(fileContent, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
att.setDataHandler(new DataHandler(fds));
String reportName = fileName; // include .xlsx
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...");
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);
}
System.out.println("Email sent with attachment");
}
知道为什么我仍然收到有关 10 MB 电子邮件大小限制的错误吗?
最佳答案
虽然该示例从语法角度来看是正确的,但它有点误导,因为它实际上使用的是 v1 SES 客户端,而不是 v2 SES 客户端,而您不会使用 v2 SES 客户端。不希望使用 v2 示例。我已提交反馈请求来纠正此问题。
适用于 Java 的 AWS 开发工具包 2.x 通常按 software.amazon.awssdk
的组 ID 进行分类,而适用于 Java 的 AWS 开发工具包 1.x 通常按组 ID 进行分类>com.amazonaws
正如您在 docs 中看到的那样.
在本例中,v2 SDK 中没有一个,而是两个 SES 客户端。这可能是因为向后兼容性。
-
SesClient
-software.amazon.awssdk.services.ses
- 调用 SES v1 API - limited to 10 MB -
SesV2Client
- software.amazon.awssdk.services.sesv2 - 调用 SES v2 API - 允许 40 MB
示例(因此您)使用的是 v1 客户端,因此请使用 SesV2Client
,您应该能够发送大于 10 MB 的电子邮件。
(您可能需要一些语法调整,但核心逻辑将保持不变)
关于java - 为什么 AWS SDK for Java v2 不允许我发送大于 10 MB 的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72362821/