创建 csv 文件并作为附件发送
Create csv file and send as attachment
所以我正在尝试生成一个 csv 文件,然后立即将其作为附件发送。
@Async
public void sendAuditEventsReportEmail(@NonNull Locale locale,
@NonNull String fileName,
@NonNull String[] contacts,
@NonNull List<AuditEventResponse> auditEventResponses) {
try {
final MimeMessageHelper message = message(true);
message.setFrom(portalProperties.getMail().getFrom());
message.setCc(contacts);
message.setSubject(messageSource.getMessage("mail.act.subject", null, locale)
+ fileName);
writeCSVFile(fileName, auditEventResponses);
message.addInline("customer-logo",
new ClassPathResource(CUSTOMER_LOGO_PATH),
PNG_MIME);
this.mailSender.send(message.getMimeMessage());
} catch (MessagingException e) {
log.error("Could not send audit report email");
}
}
private void writeCSVFile(String fileName, List<AuditEventResponse> auditEventResponses) {
ICsvBeanWriter beanWriter = null;
try {
beanWriter = new CsvBeanWriter(new FileWriter(fileName + ".csv"),
CsvPreference.STANDARD_PREFERENCE);
String[] header = {"type", "entityType", "principal", "timestamp", "data", "date"};
beanWriter.writeHeader(header);
for (AuditEventResponse auditEventResponse : auditEventResponses) {
beanWriter.write(auditEventResponse, header);
}
} catch (IOException e) {
System.err.println("Error writing the CSV file: " + e);
} finally {
if (beanWriter != null) {
try {
beanWriter.close();
} catch (IOException e) {
System.err.println("Error closing the writer: " + e);
}
}
}
}
但如您所见,我只是在创建文件(想确保它已正确生成)。有没有办法在内存中生成文件,以便我可以将其用作电子邮件中的附件?我认为我正在使用的库不支持它(supercsv)是否有其他可以做同样但在内存中的库?提前致谢!
如果您需要发送附件,则必须实际创建一个文件,因此您执行程序的方式很好。您只需在发送邮件后删除文件即可。
所以我正在尝试生成一个 csv 文件,然后立即将其作为附件发送。
@Async
public void sendAuditEventsReportEmail(@NonNull Locale locale,
@NonNull String fileName,
@NonNull String[] contacts,
@NonNull List<AuditEventResponse> auditEventResponses) {
try {
final MimeMessageHelper message = message(true);
message.setFrom(portalProperties.getMail().getFrom());
message.setCc(contacts);
message.setSubject(messageSource.getMessage("mail.act.subject", null, locale)
+ fileName);
writeCSVFile(fileName, auditEventResponses);
message.addInline("customer-logo",
new ClassPathResource(CUSTOMER_LOGO_PATH),
PNG_MIME);
this.mailSender.send(message.getMimeMessage());
} catch (MessagingException e) {
log.error("Could not send audit report email");
}
}
private void writeCSVFile(String fileName, List<AuditEventResponse> auditEventResponses) {
ICsvBeanWriter beanWriter = null;
try {
beanWriter = new CsvBeanWriter(new FileWriter(fileName + ".csv"),
CsvPreference.STANDARD_PREFERENCE);
String[] header = {"type", "entityType", "principal", "timestamp", "data", "date"};
beanWriter.writeHeader(header);
for (AuditEventResponse auditEventResponse : auditEventResponses) {
beanWriter.write(auditEventResponse, header);
}
} catch (IOException e) {
System.err.println("Error writing the CSV file: " + e);
} finally {
if (beanWriter != null) {
try {
beanWriter.close();
} catch (IOException e) {
System.err.println("Error closing the writer: " + e);
}
}
}
}
但如您所见,我只是在创建文件(想确保它已正确生成)。有没有办法在内存中生成文件,以便我可以将其用作电子邮件中的附件?我认为我正在使用的库不支持它(supercsv)是否有其他可以做同样但在内存中的库?提前致谢!
如果您需要发送附件,则必须实际创建一个文件,因此您执行程序的方式很好。您只需在发送邮件后删除文件即可。