List of usage examples for javax.mail.util ByteArrayDataSource ByteArrayDataSource
public ByteArrayDataSource(String data, String type) throws IOException
From source file:com.flexoodb.common.FlexUtils.java
/** * use this method to obtain a mimemessage with the file wrapped in it. * * @param filename filename of the file. * @param data the file in bytes./*from www . j a v a2s . c o m*/ * @return a byte array of the mimemessage. */ static public byte[] wrapInMimeMessage(String filename, byte[] data) throws Exception { MimeMessage mimemessage = new MimeMessage(javax.mail.Session.getDefaultInstance(new Properties(), null)); BodyPart messageBodyPart = new MimeBodyPart(); Multipart multipart = new MimeMultipart(); DataSource source = new ByteArrayDataSource(data, "application/octet-stream"); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); mimemessage.setContent(multipart); mimemessage.saveChanges(); // finally pipe to an array so we can conver to string ByteArrayOutputStream bos = new ByteArrayOutputStream(); mimemessage.writeTo(bos); return bos.toString().getBytes(); }