Apache poi excel attach mail
A interesting combination!
To attach an Excel file generated using Apache POI to an email using Java, you can follow these steps:
Step 1: Generate the Excel file using Apache POI
First, you need to create an Excel file using Apache POI. You can use the XSSFWorkbook
class to create a new workbook and add sheets, rows, and cells as needed.
Here's an example:
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
// Create a new workbook
XSSFWorkbook workbook = new XSSFWorkbook();
// Create a new sheet
XSSFSheet sheet = workbook.createSheet("MySheet");
// Add some data to the sheet
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Hello World!");
// Save the workbook to a file
File file = new File("example.xlsx");
workbook.write(file);
Step 2: Attach the Excel file to an email using JavaMail
Next, you need to use JavaMail to send an email with the Excel file attached. You'll need to create a MimeMessage
object and add the Excel file as an attachment.
Here's an example:
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
// Create a new MimeMessage
MimeMessage message = new MimeMessage(session);
// Set the sender and recipient
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
// Set the subject and body
message.setSubject("Example Email");
message.setText("This is an example email with an attached Excel file.");
// Create a new MimeBodyPart for the attachment
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("example.xlsx");
attachment.setTransferEncoding("binary");
// Add the attachment to the message
message.addAttachment(attachment);
// Send the email
Transport.send(message);
Putting it all together
Here's the complete code:
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws Exception {
// Generate the Excel file
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("MySheet");
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Hello World!");
File file = new File("example.xlsx");
workbook.write(file);
// Create a new MimeMessage
Session session = Session.getDefaultInstance(new Properties(), null);
MimeMessage message = new MimeMessage(session);
// Set the sender and recipient
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
// Set the subject and body
message.setSubject("Example Email");
message.setText("This is an example email with an attached Excel file.");
// Create a new MimeBodyPart for the attachment
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("example.xlsx");
attachment.setTransferEncoding("binary");
// Add the attachment to the message
message.addAttachment(attachment);
// Send the email
Transport.send(message);
}
}
This code generates an Excel file using Apache POI, attaches it to an email using JavaMail, and sends the email.