Package javax.mail.internet

Examples of javax.mail.internet.ContentType


        StringWriter alterTextWriter = new StringWriter();
        Velocity.evaluate(context, alterTextWriter, "message body", alterText);
        alterTextWriter.flush();
        alterTextWriter.close();
        alterText = alterTextWriter.toString();
        ContentType ct = new ContentType(altPart.getContentType());
        String alterTextCharset = ct.getParameter("charset");
       
        //get main part (html or related)
        Object mainPart = ((Multipart)body).getBodyPart(1).getContent();
        if(mainPart instanceof Multipart)
        {
          /*
           * -mixed
           * \-alternative (body)
           *     \-text
           *     |-related (mainPart)
           *       \-html
           *       |-attachment
           * |-attachments
           */
         
          //replace html
          String html = (String)  ((Multipart)mainPart).getBodyPart(0).getContent();
          StringWriter mailBody = new StringWriter();
          Velocity.evaluate(context, mailBody, "message body", html);
          mailBody.flush();
          mailBody.close();
          html = mailBody.toString();
          MimeBodyPart htmlPart = new MimeBodyPart();
          htmlPart.setContent(html, ((Multipart)mainPart).getBodyPart(0).getContentType());
          htmlPart.setHeader("Content-Type", ((Multipart)mainPart).getBodyPart(0).getContentType());
          htmlPart.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
          ((Multipart)mainPart).removeBodyPart(0);
          ((Multipart)mainPart).addBodyPart(htmlPart, 0);
          //replace related
          MimeBodyPart bodyPart = new MimeBodyPart();
          bodyPart.setContent(((Multipart)mainPart));
          ((Multipart)body).removeBodyPart(1);
          ((Multipart)body).addBodyPart(bodyPart);
         
          //replace body
          ((Multipart)content).removeBodyPart(0);
          bodyPart = new MimeBodyPart();
          bodyPart.setContent(((Multipart)body));
          ((Multipart)content).addBodyPart(bodyPart, 0);
        }
        else
        {
          /*
           * -mixed
           * \-alternative (body)
           *     \-text
           *     |-html
           * |-attachments
           */
          //replace html
          String html = (String)  ((Multipart)body).getBodyPart(1).getContent();
          StringWriter mailBody = new StringWriter();
          Velocity.evaluate(context, mailBody, "message body", html);
          mailBody.flush();
          mailBody.close();
          html = mailBody.toString();
          MimeBodyPart htmlPart = new MimeBodyPart();
          htmlPart.setContent(html, ((Multipart)body).getBodyPart(1).getContentType());
          htmlPart.setHeader("Content-Type", ((Multipart)body).getBodyPart(1).getContentType());
          htmlPart.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
         
          //replace html
          ((Multipart)body).removeBodyPart(1);
          ((Multipart)body).addBodyPart(htmlPart, 1);
         
          //replace body
          ((Multipart)content).removeBodyPart(0);
          MimeBodyPart bodyPart = new MimeBodyPart();
          bodyPart.setContent(((Multipart)body));
          ((Multipart)content).addBodyPart(bodyPart, 0);
        }
       
        if(alterTextCharset != null)
          setAlternativeText(alterText, alterTextCharset);
        else
          setAlternativeText(alterText, charset);
      }
      else if(body instanceof Multipart
          && ((Multipart)body).getContentType().startsWith("multipart/related"))
      {
        /*
         * -mixed
         * \-related (body)
         *     \-html
         *     |-attachment
         * |-attachments
         */
       
        //replace html
        String html = (String) ((Multipart)body).getBodyPart(0).getContent();;
        StringWriter mailBody = new StringWriter();
        Velocity.evaluate(context, mailBody, "message body", html);
        mailBody.flush();
        mailBody.close();
        html = mailBody.toString();
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(html, ((Multipart)body).getBodyPart(0).getContentType());
        htmlPart.setHeader("Content-Type", ((Multipart)body).getBodyPart(0).getContentType());
        htmlPart.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
        ((Multipart)body).removeBodyPart(0);
        ((Multipart)body).addBodyPart(htmlPart, 0);
       
        //replace body
        ((Multipart)content).removeBodyPart(0);
        MimeBodyPart bodyPart = new MimeBodyPart();
        bodyPart.setContent(((Multipart)body));
        ((Multipart)content).addBodyPart(bodyPart, 0);
      }
      else
      {
        /*
         * -mixed
         * \-html (body)
         * |-attachments
         */
       
        //replace html
        String html = (String) ((Multipart)content).getBodyPart(0).getContent();
        StringWriter mailBody = new StringWriter();
        Velocity.evaluate(context, mailBody, "message body", html);
        mailBody.flush();
        mailBody.close();
        html = mailBody.toString();
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(html, ((Multipart)content).getBodyPart(0).getContentType());
        htmlPart.setHeader("Content-Type", ((Multipart)content).getBodyPart(0).getContentType());
        htmlPart.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
       
        ((Multipart)content).removeBodyPart(0);
        ((Multipart)content).addBodyPart(htmlPart, 0);
      }
    }
    else
    {
      if(content instanceof Multipart
          && ((Multipart)content).getContentType().startsWith("multipart/alternative"))
      {
        //get alternative text
        BodyPart altPart = ((Multipart)content).getBodyPart(0);
        String alterText = (String) altPart.getContent();
        StringWriter alterTextWriter = new StringWriter();
        Velocity.evaluate(context, alterTextWriter, "message body", alterText);
        alterTextWriter.flush();
        alterTextWriter.close();
        alterText = alterTextWriter.toString();
        ContentType ct = new ContentType(altPart.getContentType());
        String alterTextCharset = ct.getParameter("charset");
       
        //get and replace html part of message
        Object body = ((Multipart)content).getBodyPart(1).getContent();
        if(body instanceof Multipart)
        {
View Full Code Here


    return true;
  }

  protected Message createMessage(MimeMessage aInput, Connection aConnection) throws JMSException {
    ContentType ct = null;
    try {
      ct = new ContentType(aInput.getContentType());
    } catch (javax.mail.internet.ParseException e) {
      LOG.warn("Unable to get content type from input message. {}", e.getMessage(), e);
    } catch (MessagingException e) {
      LOG.warn("Unable to get content type from input message. {}", e.getMessage(), e);
    }
    Message message = null;
    if (ct != null && ct.match("text/*")) {
      message = aConnection.createTextMessage();
      // TODO: MessageUtils.copy(aInput.getInputStream(), tmsg);
    } else {
      BytesMessage bmsg = aConnection.createBytesMessage();
      message = bmsg;
View Full Code Here

    }

    public EncodedData getBytesFromMultipart(MimeMultipart value, XMLMarshaller marshaller) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            ContentType contentType = new ContentType(value.getContentType());
            String boundary = contentType.getParameter("boundary");

            output.write(Helper.cr().getBytes());
            output.write(("Content-Type: " + contentType.getBaseType() + "; boundary=\"" + boundary + "\"\n").getBytes());
        } catch (Exception ex) {
            throw ConversionException.couldNotBeConverted(value, byte[].class, ex);
        }

        try {
View Full Code Here

    protected String getCharacterSet(String aType)
    {
        String characterSet = null;
        try
        {
            characterSet = new ContentType(aType).getParameter("charset");
        }
        catch (ParseException e)
        {
            // no-op
        }
View Full Code Here

   
    /**
     * Obtains the content of the encoded message, if previously encoded as <code>format=flowed</code>.
     */
    public static String deflow(Message m) throws IOException, MessagingException {
        ContentType ct = new ContentType(m.getContentType());
        String format = ct.getParameter("format");
        if (ct.getBaseType().equals("text/plain") && format != null && format.equalsIgnoreCase("flowed")) {
            String delSp = ct.getParameter("delsp");
            return deflow((String) m.getContent(), delSp != null && delSp.equalsIgnoreCase("yes"));
           
        } else if (ct.getPrimaryType().equals("text")) return (String) m.getContent();
       
        else return null;
    }
View Full Code Here

    /**
     * If the message is <code>format=flowed</code>
     * set the encoded version as message content.
     */
    public static void deflowMessage(Message m) throws MessagingException, IOException {
        ContentType ct = new ContentType(m.getContentType());
        String format = ct.getParameter("format");
        if (ct.getBaseType().equals("text/plain") && format != null && format.equalsIgnoreCase("flowed")) {
            String delSp = ct.getParameter("delsp");
            String deflowed = deflow((String) m.getContent(), delSp != null && delSp.equalsIgnoreCase("yes"));
           
            ct.getParameterList().remove("format");
            ct.getParameterList().remove("delsp");
           
            if (ct.toString().indexOf("flowed") >= 0)
                System.out.println("\n\n*************************\n* ERROR!!! FlowedMessageUtils dind't remove the flowed correctly!\n******************\n\n" + ct.toString() + " \n " + ct.toString() + "\n");
           
            m.setContent(deflowed, ct.toString());
            m.saveChanges();
        }
    }
View Full Code Here

     * Encodes the input text and sets it as the new message content.
     */
    public static void setFlowedContent(Message m, String text, boolean delSp, int width, boolean preserveCharset, String charset) throws MessagingException {
        String coded = flow(text, delSp, width);
        if (preserveCharset) {
            ContentType ct = new ContentType(m.getContentType());
            charset = ct.getParameter("charset");
        }
        ContentType ct = new ContentType();
        ct.setPrimaryType("text");
        ct.setSubType("plain");
        if (charset != null) ct.setParameter("charset", charset);
        ct.setParameter("format", "flowed");
        if (delSp) ct.setParameter("delsp", "yes");
        m.setContent(coded, ct.toString());
        m.saveChanges();
    }
View Full Code Here

    /**
     * Encodes the message content (if text/plain).
     */
    public static void flowMessage(Message m, boolean delSp, int width) throws MessagingException, IOException {
        ContentType ct = new ContentType(m.getContentType());
        if (!ct.getBaseType().equals("text/plain")) return;
        String format = ct.getParameter("format");
        String text = format != null && format.equals("flowed") ? deflow(m) : (String) m.getContent();
        String coded = flow(text, delSp, width);
        ct.setParameter("format", "flowed");
        if (delSp) ct.setParameter("delsp", "yes");
        m.setContent(coded, ct.toString());
        m.saveChanges();
    }
View Full Code Here

    /**
     * Checks whether the input message is <code>format=flowed</code>.
     */
    public static boolean isFlowedTextMessage(Message m) throws MessagingException {
        ContentType ct = new ContentType(m.getContentType());
        String format = ct.getParameter("format");
        return ct.getBaseType().equals("text/plain") && format != null && format.equalsIgnoreCase("flowed");
    }
View Full Code Here

     * @param reportType
     * @throws MessagingException
     */
    public void setReportType(String reportType) throws MessagingException
    {
        ContentType contentType = new ContentType(getContentType());
        contentType.setParameter("report-type", reportType);
        setContentType(contentType);
    }
View Full Code Here

TOP

Related Classes of javax.mail.internet.ContentType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.