gpt4 book ai didi

java - 如何使用java邮件在单个邮件中发送由函数返回的两个附件?

转载 作者:行者123 更新时间:2023-12-01 18:27:39 25 4
gpt4 key购买 nike

我正在获取一个 Excel 文件作为方法的返回值。我想将该 Excel 文件作为邮件附件发送,但我没有选项将该文件保存在系统上。所以实际上,我没有文件名。如何使用 javamail 将该文件作为附件发送?

digitalDeliveryReportSheet = digitalWorkbook.createSheet("Local Media");
headerStyle = digitalWorkbook.createCellStyle();
headerFont = digitalWorkbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);

cell = null;
XSSFRow rowLm;

rowNum = 1;
colNum = 0;

mergeRowFrom = 0; // --> added
mergeRowTo = 0; // --> added

mergeColFrom = 1; // --> added
mergeColTo = 2; // --> added

contentStatus = new ArrayList<String>();
contentStatus.add("Delivered");
contentStatus.add("Completed");

rowLm = digitalDeliveryReportSheet.createRow(0);
rowLm.createCell(colNum++).setCellValue("Delivery Date");
colNum = 1;

width = 3000;

try {
width = Integer.parseInt(ApplicationUtil.getProperty(ApplicationConstants.REPORTS_DEFAULT_COLUMN_WIDTH));
} catch (Exception e) {
LOG.warn("Exception while fetching REPORTS_DEFAULT_COLUMN_WIDTH", e);
}


for (String header : shelfCodeList) {
cell = rowLm.createCell(colNum++);
cell.setCellStyle(headerStyle);
cell.setCellValue(header);
CellRangeAddress cellRangeAddress = new CellRangeAddress(mergeRowFrom, mergeRowTo, mergeColFrom, mergeColTo); // --> added
digitalDeliveryReportSheet.addMergedRegion(cellRangeAddress); // --> added
mergeColFrom+=2;
mergeColTo+=2;
colNum++;
//sheet.autoSizeColumn(colNum);
digitalDeliveryReportSheet.setColumnWidth(colNum, width);
}

cellsToCover = shelfCodeList.size()*2;
rowLm = digitalDeliveryReportSheet.createRow(1);
colNum = 1;
rowLm = digitalDeliveryReportSheet.createRow(rowNum);
for(int i = 1; i <= cellsToCover; i++) {
cell = rowLm.createCell(colNum++);
cell.setCellStyle(headerStyle);
if(i % 2 != 0) {
cell.setCellValue(contentStatus.get(0));
}
else {
cell.setCellValue(contentStatus.get(1));
}
}

countByShelf = new HashMap<Long, Integer>();
completedCountByShelf = new HashMap<Long, Integer>();
rowNum++;
for (String date : deliveryDateList) {
colNum = 0;
rowLm = digitalDeliveryReportSheet.createRow(rowNum++);
cell = rowLm.createCell(colNum++);
cell.setCellValue(date);

for (int i=0; i < shelfNumberList.size(); i++) {
cell = rowLm.createCell(colNum++);
long shelfNumber = shelfNumberList.get(i);
countByShelf = map.get(date);
completedCountByShelf = byShelf.get(date);

if(countByShelf != null) {
if(countByShelf.get(shelfNumber) != null) {
cell.setCellValue(countByShelf.get(shelfNumber));
}
}
if(completedCountByShelf!=null) {
if(completedCountByShelf.get(shelfNumber) != null) {
cell = rowLm.createCell(colNum);
cell.setCellValue(completedCountByShelf.get(shelfNumber));
}
}
colNum++;
}
}

mergeRowFrom = rowNum; // --> added
mergeRowTo = rowNum; // --> added

mergeColFrom = 1; // --> added
mergeColTo = 2; // --> added
rowLm = digitalDeliveryReportSheet.createRow(rowNum);
colNum = 1;
for (String header : shelfCodeList) {
cell = rowLm.createCell(colNum++);
cell.setCellStyle(headerStyle);
cell.setCellValue(header);
CellRangeAddress cellRangeAddress = new CellRangeAddress(mergeRowFrom, mergeRowTo, mergeColFrom, mergeColTo); // --> added
digitalDeliveryReportSheet.addMergedRegion(cellRangeAddress); // --> added
mergeColFrom+=2;
mergeColTo+=2;
colNum++;
//sheet.autoSizeColumn(colNum);
digitalDeliveryReportSheet.setColumnWidth(colNum, width);
}

rowNum++;
colNum = 1;
rowLm = digitalDeliveryReportSheet.createRow(rowNum);
for(int i = 1; i <= cellsToCover; i++) {
cell = rowLm.createCell(colNum++);
cell.setCellStyle(headerStyle);
if(i % 2 != 0) {
cell.setCellValue(contentStatus.get(0));
}
else {
cell.setCellValue(contentStatus.get(1));
}
}

rowNum++;
colNum = 0;
rowLm = digitalDeliveryReportSheet.createRow(rowNum++);
cell = rowLm.createCell(colNum++);
cell.setCellValue("Total By Column");
for (long shelfNumber : shelfNumberList) {
cell = rowLm.createCell(colNum++);
if (totalByColumn.get(shelfNumber) != null && totalByColumn.get(shelfNumber) > 0) {
cell.setCellValue(totalByColumn.get(shelfNumber));
}
if(totalCompletedByColumn.get(shelfNumber) != null && totalCompletedByColumn.get(shelfNumber) > 0) {
cell = rowLm.createCell(colNum);
cell.setCellValue(totalCompletedByColumn.get(shelfNumber));
}
colNum++;
}

} else {
throw new AssetLibraryValidationException(errorMessages);
}

return digitalWorkbook;

这是返回工作簿的代码,如何发送此返回的工作簿而不将其保存在我的系统上。

最佳答案

您需要将数据转换为某种可导出的格式,例如Excel文件格式、csv格式等。并且您需要将数据转换为内存中的字节数组。一旦您在字节数组中获得了正确格式的数据,您就可以使用 ByteArrayDataSource 将其附加到 JavaMail 消息。稍微搜索一下应该会找到一些例子,但关键是:

mbp.setDataHandler(new DataHandler(
new ByteArrayDataSource(my_byte_array, "text/csv"))); // or whatever MIME type

关于java - 如何使用java邮件在单个邮件中发送由函数返回的两个附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60207999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com