Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
1.4 kB
3
Indexable
Never
public static void main(String[] args) {
    // Path to your Excel file
    String excelFilePath = "path_to_your_excel_file.xlsx";
    // SMTP host and port for Gmail
    String smtpHost = "smtp.gmail.com";
    String smtpPort = "587";
    // Your Gmail address and password (or app-specific password)
    String username = "your_email@gmail.com";
    String password = "your_password"; // Note: It's recommended to use app-specific password for Gmail

    try (FileInputStream file = new FileInputStream(excelFilePath);
         Workbook workbook = new XSSFWorkbook(file)) {

        // Define a map of sheet names to recipients
        Map<String, String> sheetEmailMap = new HashMap<>();
        sheetEmailMap.put("Sheet1", "email1@mail.com");
        sheetEmailMap.put("Sheet2", "email2@mail.com");
        // Add more mappings as needed

        // Loop through each sheet and send it to the corresponding recipient
        for (String sheetName : sheetEmailMap.keySet()) {
            Sheet sheet = workbook.getSheet(sheetName);
            if (sheet != null) {
                String recipient = sheetEmailMap.get(sheetName);
                sendSheetAsEmail(sheet, recipient, smtpHost, smtpPort, username, password);
            } else {
                System.out.println("Sheet " + sheetName + " not found in the workbook.");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Leave a Comment