java发送带附件的电子邮件

时间:2016-11-08 11:56:01

标签: java

package abc;

import java.io.FileInputStream;
import java.io.FileOutputStream;  
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFBorderFormatting;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import java.util.Iterator;

public class read {
    public static void main(String[] args) throws IOException {

        String Filepath = "C:\\test.xls";
        FileInputStream input = new FileInputStream("Filepath");

        HSSFWorkbook wb = new HSSFWorkbook(); //Access the workbook
        //Access the worksheet, so that we can update / modify it.     
        HSSFSheet worksheet = wb.getSheetAt(0); 


        Cell cell = null; // declare a Cell object

        cell = worksheet.getRow(2).getCell(2);
        CellStyle style = wb.createCellStyle();
        style.setFillForegroundColor(IndexedColors.BLUE.getIndex());

        cell.setCellStyle(style);

        try {           
            FileOutputStream output = new FileOutputStream(
                "C:\\Users\\TEMP\\Desktop\\downloads.xls"
            );
            wb.write(output);
            output.close(); 
        } catch(Exception e){
            e.printStackTrace();
        }        
    }
}

1 个答案:

答案 0 :(得分:0)

我建议使用Apache Commons Email而不是直接使用JavaMail API。后者非常棘手,有很多边缘条件,真的需要对MIME有很好的理解。

以下是您要求的示例:https://commons.apache.org/proper/commons-email/userguide.html

我对这个图书馆运气很好,并从第一手经验中推荐。