java to send email with attachment -
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 workbook //access worksheet, can update / modify it. hssfsheet worksheet = wb.getsheetat(0); cell cell = null; // declare 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(); } } }
i suggest using apache commons email rather using javamail api directly. later tricky , has lots of edge conditions , requires understanding of mime.
here example of asked for: https://commons.apache.org/proper/commons-email/userguide.html
i've had excellent luck library , recommend first hand experience.
Comments
Post a Comment