List of usage examples for javax.mail.internet MimeBodyPart MimeBodyPart
public MimeBodyPart(InternetHeaders headers, byte[] content) throws MessagingException
From source file:org.apache.axis2.datasource.jaxb.JAXBAttachmentMarshaller.java
public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String namespace, String localPart) {//from ww w .j a v a2 s.c o m if (offset != 0 || length != data.length) { int len = length - offset; byte[] newData = new byte[len]; System.arraycopy(data, offset, newData, 0, len); data = newData; } if (mimeType == null || mimeType.length() == 0) { mimeType = APPLICATION_OCTET; } if (log.isDebugEnabled()) { log.debug("Adding MTOM/XOP byte array attachment for element: " + "{" + namespace + "}" + localPart); } String cid = null; try { // Create MIME Body Part final InternetHeaders ih = new InternetHeaders(); final byte[] dataArray = data; ih.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, mimeType); final MimeBodyPart mbp = (MimeBodyPart) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { return new MimeBodyPart(ih, dataArray); } catch (MessagingException e) { throw new OMException(e); } } }); //Create a data source for the MIME Body Part MimePartDataSource mpds = (MimePartDataSource) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return new MimePartDataSource(mbp); } }); long dataLength = data.length; Integer value = null; if (msgContext != null) { value = (Integer) msgContext.getProperty(Constants.Configuration.MTOM_THRESHOLD); } else if (log.isDebugEnabled()) { log.debug( "The msgContext is null so the MTOM threshold value can not be determined; it will default to 0."); } int optimizedThreshold = (value != null) ? value.intValue() : 0; if (optimizedThreshold == 0 || dataLength > optimizedThreshold) { DataHandler dataHandler = new DataHandler(mpds); cid = addDataHandler(dataHandler, false); } // Add the content id to the mime body part mbp.setHeader(HTTPConstants.HEADER_CONTENT_ID, cid); } catch (MessagingException e) { throw new OMException(e); } return cid == null ? null : "cid:" + cid; }
From source file:es.pode.catalogadorWeb.presentacion.importar.ImportarControllerImpl.java
public String submit(ActionMapping mapping, SubmitForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String resultado = ""; String action = form.getAccion(); //String idiomaLocale=((Locale)request.getSession().getAttribute(ConstantesAgrega.DEFAULT_LOCALE)).getLanguage(); ResourceBundle i18n = I18n.getInstance().getResource("application-resources", (Locale) request.getSession().getAttribute(ConstantesAgrega.DEFAULT_LOCALE)); if (action != null) { if (action.equals(i18n.getString("catalogadorAvanzado.importar.aceptar"))) { resultado = "Aceptar"; if (form.getFichero() == null || form.getFichero().getFileName().equals("") || form.getFichero().getFileSize() == 0) throw new ValidatorException("{catalogadorAvanzado.importar.error.ficherovacio}"); //crear el datahandler InternetHeaders ih = new InternetHeaders(); MimeBodyPart mbp = null;//from w ww .jav a 2s . c om DataSource source = null; DataHandler dh = null; try { FormFile ff = (FormFile) form.getFichero(); mbp = new MimeBodyPart(ih, ff.getFileData()); source = new MimePartDataSource(mbp); dh = new DataHandler(source); } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("error al crear el datahandler"); } throw new ValidatorException("{catalogadorAvanzado.importar.error}"); } //validar el fichero Boolean valido = new Boolean(false); try { valido = this.getSrvValidadorService().obtenerValidacionLomes(dh); } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("error al llamar al servicio de validacin"); } throw new ValidatorException("{catalogadorAvanzado.importar.error.novalido}"); } if (!valido.booleanValue()) throw new ValidatorException("{catalogadorAvanzado.importar.error.novalido}"); //agregar el datahandler a sesion this.getCatalogadorAvSession(request).setLomesImportado(dh); } else if (action.equals(i18n.getString("catalogadorAvanzado.importar.cancelar"))) resultado = "Cancelar"; } return resultado; }
From source file:org.apache.axis2.jaxws.marshaller.impl.alt.Attachment.java
private static DataHandler createDataHandler(Object value, Class cls, String[] mimeTypes, String cid) { if (log.isDebugEnabled()) { System.out.println("Construct data handler for " + cls + " cid=" + cid); }/* w w w. java2 s . co m*/ DataHandler dh = null; if (cls.isAssignableFrom(DataHandler.class)) { dh = (DataHandler) value; if (dh == null) { return dh; //return if DataHandler is null } try { Object content = dh.getContent(); // If the content is a Source, convert to a String due to // problems with the DataContentHandler if (content instanceof Source) { if (log.isDebugEnabled()) { System.out .println("Converting DataHandler Source content to " + "DataHandlerString content"); } byte[] bytes = (byte[]) ConvertUtils.convert(content, byte[].class); String newContent = new String(bytes); return new DataHandler(newContent, mimeTypes[0]); } } catch (Exception e) { throw ExceptionFactory.makeWebServiceException(e); } } else { try { byte[] bytes = createBytes(value, cls, mimeTypes); // Create MIME Body Part InternetHeaders ih = new InternetHeaders(); ih.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, mimeTypes[0]); MimeBodyPart mbp = new MimeBodyPart(ih, bytes); //Create a data source for the MIME Body Part MimePartDataSource ds = new MimePartDataSource(mbp); dh = new DataHandler(ds); mbp.setHeader(HTTPConstants.HEADER_CONTENT_ID, cid); } catch (Exception e) { throw ExceptionFactory.makeWebServiceException(e); } } return dh; }
From source file:com.adaptris.util.text.mime.MultiPartOutput.java
/** * Add a new part to the mime multipart. * /*from ww w .j a va 2s . co m*/ * @param payload the data. * @param encoding the encoding to apply * @param contentId the id to set the content with. * @throws MessagingException if there was a failure adding the part. MimeMultiPart * @throws IOException if there was an IOException */ public void addPart(String payload, String encoding, String contentId) throws MessagingException, IOException { InternetHeaders header = new InternetHeaders(); byte[] encodedBytes = encodeData(payload, encoding, header); MimeBodyPart part = new MimeBodyPart(header, encodedBytes); this.addPart(part, contentId); }
From source file:com.threewks.thundr.mail.JavaMailMailer.java
private void addAttachments(Multipart multipart, List<Attachment> attachments) throws MessagingException { for (Attachment attachment : attachments) { BasicViewRenderer response = render(attachment.view()); byte[] base64Encoded = Base64.encodeToByte(response.getOutputAsBytes()); InternetHeaders headers = new InternetHeaders(); headers.addHeader(Header.ContentType, response.getContentType()); headers.addHeader(Header.ContentTransferEncoding, "base64"); MimeBodyPart part = new MimeBodyPart(headers, base64Encoded); part.setFileName(attachment.name()); part.setDisposition(attachment.disposition().value()); if (attachment.isInline()) { part.setContentID(attachment.contentId()); }//from www. j a v a2s. c o m multipart.addBodyPart(part); } }
From source file:mitm.common.mail.BodyPartUtils.java
/** * This is the only way I know of to create a new MimeBodyPart from another Message which is safe * for signed email. All other methods break the signature when quoted printable soft breaks are used. * // w w w. ja v a 2s.c om * example of quoted printable soft breaks: * * Content-Transfer-Encoding: quoted-printable * soft break example = * another line = * * All other methods will re-encode and removes the soft breaks. * * @param sourceMessage * @param matcher * @return * @throws IOException * @throws MessagingException */ @SuppressWarnings("unchecked") public static MimeBodyPart makeContentBodyPartRawBytes(MimeMessage sourceMessage, HeaderMatcher matcher) throws IOException, MessagingException { /* * getRawInputStream() can throw a MessagingException when the source message is a MimeMessage * that is created in code (ie. not from a stream) */ InputStream messageStream = sourceMessage.getRawInputStream(); byte[] rawMessage = IOUtils.toByteArray(messageStream); InternetHeaders destinationHeaders = new InternetHeaders(); Enumeration<Header> sourceHeaders = sourceMessage.getAllHeaders(); HeaderUtils.copyHeaders(sourceHeaders, destinationHeaders, matcher); MimeBodyPart newBodyPart = new MimeBodyPart(destinationHeaders, rawMessage); return newBodyPart; }
From source file:com.adaptris.util.text.mime.MultiPartOutput.java
/** * Add a new part to the mime multipart. * /* w w w.j av a2 s. com*/ * @param payload the data. * @param encoding the encoding to apply * @param contentId the id to set the content with. * @throws MessagingException on error manipulating the bodypart * @throws IOException on general IO error. */ public void addPart(byte[] payload, String encoding, String contentId) throws MessagingException, IOException { InternetHeaders header = new InternetHeaders(); byte[] encodedBytes = encodeData(payload, encoding, header); MimeBodyPart part = new MimeBodyPart(header, encodedBytes); this.addPart(part, contentId); }
From source file:com.adaptris.mail.MailSenderImp.java
private MimeBodyPart create(byte[] bytes, InternetHeaders headers, String encoding) throws MessagingException, IOException { byte[] content = encode(bytes, headers, encoding); return new MimeBodyPart(headers, content); }
From source file:org.apache.mailet.base.test.MimeMessageBuilder.java
public MimeMessageBuilder setMultipartWithSubMessage(MimeMessage mimeMessage) throws MessagingException, IOException { return setMultipartWithBodyParts(new MimeBodyPart( new InternetHeaders( new ByteArrayInputStream("Content-Type: multipart/mixed".getBytes(Charsets.US_ASCII))), IOUtils.toByteArray(mimeMessage.getInputStream()))); }
From source file:org.apache.james.core.builder.MimeMessageBuilder.java
public MimeMessageBuilder setMultipartWithSubMessage(MimeMessage mimeMessage) throws MessagingException, IOException { return setMultipartWithBodyParts(new MimeBodyPart( new InternetHeaders(new ByteArrayInputStream( "Content-Type: multipart/mixed".getBytes(StandardCharsets.US_ASCII))), IOUtils.toByteArray(mimeMessage.getInputStream()))); }