如何将 ZIP 文件附加到 Java 中的电子邮件?
How to attach a ZIP file to an email in Java?
我正在使用 AWS SES。我正在尝试将 ZIP 文件附加到在内存中创建的 CSV 电子邮件。我很接近,但我很难弄明白。
目前,当我收到电子邮件时,我仍然收到一个 .CSV 文件,但打开后,内容似乎是压缩的。如何压缩文件而不是内容?
当前正在接收文件作为再字节数组:
public void emailReport(
byte[] fileAttachment,
String attachmentType,
String attachmentName,
List<String> emails) throws MessagingException, IOException {
......
// Attachment
messageBodyPart = new MimeBodyPart();
byte[] test = zipBytes(attachmentName, fileAttachment);
// DataSource source = new ByteArrayDataSource(fileAttachment, attachmentType);
DataSource source = new ByteArrayDataSource(test, "application/zip");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachmentName);
multipart.addBodyPart(messageBodyPart);
log.info("Successfully attached file.");
message.setContent(multipart);
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream);
RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage);
client.sendRawEmail(rawEmailRequest);
System.out.println("Email sent!");
log.info("Email successfully sent.");
} catch (Exception ex) {
log.error("Error when sending email");
ex.printStackTrace();
}
}
这是一个压缩文件的方法:
public static byte[] zipBytes(String filename, byte[] input) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry(filename);
entry.setSize(input.length);
zos.putNextEntry(entry);
zos.write(input);
zos.closeEntry();
zos.close();
return baos.toByteArray();
}
感谢您的帮助,如果您有任何其他问题,请告诉我,我会提供任何需要的代码等。
文件名应具有 .zip 扩展名。
messageBodyPart.setFileName("file.zip");
我正在使用 AWS SES。我正在尝试将 ZIP 文件附加到在内存中创建的 CSV 电子邮件。我很接近,但我很难弄明白。
目前,当我收到电子邮件时,我仍然收到一个 .CSV 文件,但打开后,内容似乎是压缩的。如何压缩文件而不是内容?
当前正在接收文件作为再字节数组:
public void emailReport(
byte[] fileAttachment,
String attachmentType,
String attachmentName,
List<String> emails) throws MessagingException, IOException {
......
// Attachment
messageBodyPart = new MimeBodyPart();
byte[] test = zipBytes(attachmentName, fileAttachment);
// DataSource source = new ByteArrayDataSource(fileAttachment, attachmentType);
DataSource source = new ByteArrayDataSource(test, "application/zip");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachmentName);
multipart.addBodyPart(messageBodyPart);
log.info("Successfully attached file.");
message.setContent(multipart);
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream);
RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage);
client.sendRawEmail(rawEmailRequest);
System.out.println("Email sent!");
log.info("Email successfully sent.");
} catch (Exception ex) {
log.error("Error when sending email");
ex.printStackTrace();
}
}
这是一个压缩文件的方法:
public static byte[] zipBytes(String filename, byte[] input) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry(filename);
entry.setSize(input.length);
zos.putNextEntry(entry);
zos.write(input);
zos.closeEntry();
zos.close();
return baos.toByteArray();
}
感谢您的帮助,如果您有任何其他问题,请告诉我,我会提供任何需要的代码等。
文件名应具有 .zip 扩展名。
messageBodyPart.setFileName("file.zip");