apache poi - POI not sending xls file as attachment , sending as string -
one of junior code not sending excel file attachment . sending file like
------=_part_0_2066339629.1374147892060 content-type: text/plain; name="service_change_alert_thu jul 18 17:14:50 ist 2013.xlsx" content-transfer-encoding: base64 content-disposition: attachment; filename="service_change_alert_thu jul 18 17:14:50 ist 2013.xlsx"
uesdbbqacaaianqj8kiaaaaaaaaaaaaaaaaraaaazg9juhjvchmvy29yzs54bwytkv1lwzauhu/7 k0lu2ytr1bhadlegguladsw7kb7byvnbeu3892bdrcheennypu/d4ary79wa3sh53ugss4xibfqa ptdtixf1jl3hdzukhtqots5yckehj2jl+xj3ivhoijcdkogzgouyvbinriija4kv8lw0qbaunhmf qtqichkwpxbw4aops/vvykbosvvmhknqsaidkndbe5yx8s0gcmr/wzismdz7fqbgcczgfolirow8 3d0+tmunvfzbaam4shaqtnyuhygadyoohj4slpgrecyvrusnrhau5sm9snmqzowvl/ymphfkv//k
function following
public void sendseviceabilitymail(list<octpinsavebean> datalist) throws messagingexception { session session = null; map<string, string> utilsmap = applicationbean.utilsproperties; if (utilsmap == null || utilsmap.size() == 0) utilsmap = applicationbean.getutilspropertyfilevalues(); smtphost = utilsmap.get("smtphost"); = utilsmap.get("to"); try { if (smtphost != null && != null) { date date = new date(); xssfworkbook updatedatabook = updatedserviceabilityexcel(datalist); properties props = system.getproperties(); props.put("mail.smtp.host", smtphost); props.put("to", to); session = session.getinstance(props, null); string str = "strstr"; multipart multipart = new mimemultipart(); bodypart messagebodypart1 = new mimebodypart(); messagebodypart1.setcontent(str, "text/html"); bodypart messagebodypart = new mimebodypart(); bytearrayoutputstream baos = new bytearrayoutputstream(); updatedatabook.write(baos); byte[] bytes = baos.tobytearray(); datasource ds = new bytearraydatasource(bytes, "application/vnd.ms-excel"); datahandler dh = new datahandler(ds); messagebodypart.setdatahandler(dh); string filename = "service_change_alert_" + date+".xlsx"; messagebodypart.setfilename(filename); messagebodypart.setheader("content-disposition", "attachment; filename=\"" + filename + "\""); multipart.addbodypart(messagebodypart1); multipart.addbodypart(messagebodypart); if (to != null && to.length() > 0) { mimemessage msg = new mimemessage(session); msg.setfrom(new internetaddress("tech_noida <tech_noida@xyz.com>")); string[] toarray = to.split(","); internetaddress[] address = new internetaddress[toarray.length]; (int = 0; < toarray.length; i++) { address[i] = new internetaddress(toarray[i]); } msg.setrecipients(message.recipienttype.to, address); msg.setsubject("updated serviceability alert!!!"); msg.setcontent(multipart); msg.setsentdate(date); try { transport.send(msg); logger.info("mail has been sent updated serviceability alert :" + to); } catch (exception e1) { e1.printstacktrace(); } } } } catch (exception e) { e.printstacktrace(); } }
the problem not related poi, javamail (plus way different email clients handles specs , malformed emails). try this:
multipart multipart = new mimemultipart(); mimebodypart html = new mimebodypart(); // use actual html not "strstr" html.setcontent("<html><body><h1>hi</h1></body></html>", "text/html"); multipart.addbodypart(html); // ... // joop eggen suggestion avoid spaces in file name string filename = "service_change_alert_" + new simpledateformat("yyyy-mm-dd_hh:mm").format(date) + ".xlsx"; bytearrayoutputstream baos = new bytearrayoutputstream(); updatedatabook.write(baos); byte[] poibytes = baos.tobytearray(); // can followed datasource / datahandler stuff if need mimebodypart attachment = new mimebodypart(); attachment.setfilename(filename); attachment.setcontent(poibytes, "application/vnd.ms-excel"); //attachment.setdatahandler(dh); attachment.setdisposition(mimebodypart.attachment); multipart.addbodypart(attachment); update:
also don't forget call savechanges update headers before sending message.
msg.savechanges(); see this answer further details.
Comments
Post a Comment