Package javax.mail.internet

Examples of javax.mail.internet.MimeBodyPart


    sAuthor = Gadgets.ASCIIEncode(sAuthor);

    if (null != oStrm) {
      StringBuffer oStrBuff = new StringBuffer();
      try {
        MimeBodyPart oMsgText = new MimeBodyPart(oStrm);
        DBMimePart.parseMimePart(oStrBuff, null, sContainer, "", oMsgText, 0);
      } catch (MessagingException xcpt) {
        if (DebugFile.trace)
          DebugFile.writeln(xcpt.getClass().getName()+" "+xcpt.getMessage()+" indexing message "+sGuid+" - "+sSubject);
      }
View Full Code Here


    if (sContentType.equals("html"))
      sText = oOrMsg.tagBodyHtml();
    else
      sText = oOrMsg.tagBodyPlain();

    MimeBodyPart oText = new MimeBodyPart();
    oText.setContent(sText, "text/html");
    java.io.ByteArrayOutputStream oBaStrm = new java.io.ByteArrayOutputStream(sText.length()*2+2);
    oText.writeTo(oBaStrm);

    oStmt = oConn.prepareStatement("UPDATE "+DB.k_mime_msgs+" SET "+DB.len_mimemsg+"=?"+","+DB.tx_subject+"=?,"+DB.by_content+"=? WHERE "+DB.gu_mimemsg+"=?");
    oStmt.setInt(1, oBaStrm.size());
    oStmt.setString(2, oOrMsg.getSubject());
    oStmt.setBinaryStream(3, new java.io.ByteArrayInputStream(oBaStrm.toByteArray()), oBaStrm.size());
View Full Code Here

    if (sContentType.equals("html"))
      sText = oOrMsg.tagBodyHtml();
    else
      sText = oOrMsg.tagBodyPlain();

    MimeBodyPart oText = new MimeBodyPart();
    oText.setContent(sText, "text/html");
    java.io.ByteArrayOutputStream oBaStrm = new java.io.ByteArrayOutputStream(sText.length()*2+2);
    oText.writeTo(oBaStrm);

    java.math.BigDecimal oPosition;
    oStmt = oConn.prepareStatement("SELECT "+DB.nu_position+" FROM "+DB.k_mime_msgs+" WHERE "+DB.gu_mimemsg+"=?");
    oStmt.setString(1, sGuOriginalMsg);
    oRSet = oStmt.executeQuery();
View Full Code Here

    oStmt.setString(1, sTxMailFrom);
    oStmt.setString(2, sTxMailReply);
    oStmt.setString(3, sNmFrom);
    oStmt.setString(4, sTxSubject);
    oStmt.setString(5, "text/"+sContentType);
    MimeBodyPart oBody = new MimeBodyPart();
    if (DebugFile.trace) DebugFile.writeln("MimeBodyPart.setContent(\""+Gadgets.left(sBody,255)+"\",\"text/"+sContentType+"\")");
    if (sContentType.toLowerCase().startsWith("text/"))
      oBody.setContent(sBody, sContentType);
    else
      oBody.setContent(sBody, "text/"+sContentType);
    ByteArrayOutputStream oBodyStrm = new ByteArrayOutputStream((sBody.length()*2)+2);
    oBody.writeTo(oBodyStrm);
    oStmt.setInt(6, oBodyStrm.size());
    oStmt.setBytes(7, oBodyStrm.toByteArray());   
    oStmt.setString(8, sGuMsg);
    if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()")
    oStmt.executeUpdate();
View Full Code Here

           oRetVal = DBMimePart.getMessagePart (oPart, p);
         }
       }  // next (p)
     }
     else if (sContentClass.equals("java.lang.String")) {
       oRetVal = new MimeBodyPart();
       oRetVal.setText((String) oContent);
     }
     else {
       throw new MessagingException("Unparsed Mime Content " + oContent.getClass().getName());
     }

     if (null==oRetVal) {
       oRetVal = new MimeBodyPart();
       oRetVal.setText("");
     }

     if (DebugFile.trace) {
       DebugFile.decIdent();
View Full Code Here

     }
     else if (sContentClass.equals("com.sun.mail.dsn.MultipartReport")) {
       oRetVal = ((MultipartReport) oContent).getTextBodyPart();
     }
     else if (sContentClass.equals("java.lang.String")) {
       oRetVal = new MimeBodyPart();
       oRetVal.setText((String) oContent);
     }
     else if (oContent instanceof InputStream) {
       // This branch is reached when the content-type is not recognized
       // (usually with a com.sun.mail.util.SharedByteArrayInputStream)
       // Decode content as an ISO-8859-1 string
       if (DebugFile.trace) DebugFile.writeln("No data handler found for Content-Type, decoding as ISO-8859-1 string");
       InputStream oInStrm = (InputStream) oContent;
       ByteArrayOutputStream oBaStrm = new ByteArrayOutputStream();
       StreamPipe oPipe = new StreamPipe();
       oPipe.between(oInStrm, oBaStrm);
       oRetVal = new MimeBodyPart();
       oRetVal.setText(oBaStrm.toString("ISO8859_1"));
     }
     else {
       throw new MessagingException("Unparsed Mime Content " + oContent.getClass().getName());
     }

     if (null==oRetVal) {
       oRetVal = new MimeBodyPart();
       oRetVal.setText("");
     }

     if (DebugFile.trace) {
       DebugFile.decIdent();
View Full Code Here

    }
    else {
      InputStream oInStrm;
      PreparedStatement oStmt = null;
      ResultSet oRSet = null;
      MimeBodyPart oBody = null;
      String sFolderNm = null;
      String sType = "multipart/";

      try {
        sFolderNm = ((DBFolder)oFolder).getCategory().getStringNull(DB.nm_category, null);
        if (getMessageGuid()!=null) {
          oStmt = ((DBFolder)oFolder).getConnection().prepareStatement("SELECT "+DB.id_type+","+DB.by_content+" FROM "+DB.k_mime_msgs+" WHERE "+DB.gu_mimemsg+"=?");
          oStmt.setString(1, getMessageGuid());
        }
        else {
          oStmt = ((DBFolder)oFolder).getConnection().prepareStatement("SELECT "+DB.id_type+","+DB.by_content+" FROM "+DB.k_mime_msgs+" WHERE "+DB.id_message+"=? AND "+DB.gu_category+"=?");
          oStmt.setString(1, getMessageID());
          oStmt.setString(2, ((DBFolder)oFolder).getCategory().getString(DB.gu_category));
        }
        oRSet = oStmt.executeQuery();
        if (oRSet.next()) {
          sType = oRSet.getString(1);
          oInStrm = oRSet.getBinaryStream(2);
          if (!oRSet.wasNull()) {
            oBody = new MimeBodyPart(oInStrm);
            oInStrm.close();
          }
        }
        oRSet.close();
        oRSet=null;
        oStmt.close();
        oStmt=null;
      }
      catch (SQLException sqle) {
        if (oRSet!=null) { try {oRSet.close();} catch (Exception ignore) {} }
        if (oStmt!=null) { try {oStmt.close();} catch (Exception ignore) {} }
        throw new MessagingException(sqle.getMessage(), sqle);
      }
      if (oBody!=null) {
        if (sType.startsWith("text/"))
          oBuffer.append(oBody.getContent());
        else
          DBMimePart.parseMimePart (oBuffer, null, sFolderNm, getMessageID()!=null ? getMessageID() : getContentID(), oBody, 0);
      }
    }
View Full Code Here

    Multipart oDraftParts = getParts();
    final int iDraftParts = oDraftParts.getCount();

    if (DebugFile.trace) DebugFile.writeln("Multipart.getCount() = " + String.valueOf(iDraftParts));

    MimeBodyPart oMsgPlainText = new MimeBodyPart();
    MimeMultipart oSentMsgParts = new MimeMultipart("mixed");

    if (sContentType.equalsIgnoreCase("html")) {

      MimeMultipart oHtmlRelated  = new MimeMultipart("related");
      MimeMultipart oTextHtmlAlt  = new MimeMultipart("alternative");

      // ************************************************************************
      // Replace image CIDs

      String sSrc, sCid, sText = "";

      Parser oPrsr = Parser.createParser(sBody, getEncoding());

      // String sCid, sSrc;

      HtmlMimeBodyPart oHtmBdy = new HtmlMimeBodyPart(sBody, getEncoding());

      try {

        // ****************************
        // Extract plain text from HTML
        if (DebugFile.trace) DebugFile.writeln("new StringBean()");

        StringBean oStrBn = new StringBean();

        try {
          oPrsr.visitAllNodesWith (oStrBn);
        } catch (ParserException pe) {
          if (DebugFile.trace) {
            DebugFile.writeln("org.htmlparser.util.ParserException " + pe.getMessage());
          }
          throw new MessagingException(pe.getMessage(), pe);
        }

        sText = oStrBn.getStrings();

        oStrBn = null;

        // *******************************
        // Set plain text alternative part

        oMsgPlainText.setDisposition("inline");
        oMsgPlainText.setText(sText,Charset.forName(sEncoding).name(),"plain");
        if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/alternative).addBodyPart(text/plain)");
        oTextHtmlAlt.addBodyPart(oMsgPlainText);

        // *****************************************
        // Iterate images from HTML and replace CIDs

    if (bAttachInlineImages) {
      sBody = oHtmBdy.addPreffixToImgSrc("cid:");         
    } // fi (bAttachInlineImages)
      }
      catch (ParserException pe) {
        if (DebugFile.trace) {
          DebugFile.writeln("org.htmlparser.util.ParserException " + pe.getMessage());
        }
        throw new MessagingException(pe.getMessage(), pe);
      }
      // End replace image CIDs
      // ************************************************************************


      // ************************************************************************
      // Some defensive programming: ensure that all src="..." attributes point
      // either to an absolute http:// URL or to a cid:
      oHtmBdy.setHtml (sBody);
      ArrayList<String> aLocalUrls = oHtmBdy.extractLocalUrls();
      if (aLocalUrls.size()>0) {
        if (DebugFile.trace) {
          DebugFile.writeln("HTML body part contains local references to external resources");
          for (String i : aLocalUrls) {
            DebugFile.writeln(i);
          }
          DebugFile.write(sBody+"\n");
        }
        throw new MessagingException("HTML body part "+(sId==null ? "" : "of message"+sId)+" contains local references to external resources such as "+aLocalUrls.get(0));
      } // aLocalUrls != {}

     
      // ************************************************************************
      // Add HTML related images

      if (oHtmBdy.getImagesCids().isEmpty()) {
          // Set HTML part
          MimeBodyPart oMsgHtml = new MimeBodyPart();
          oMsgHtml.setDisposition("inline");

          // ****************************************************************************
          // Replace <!--WEBBEACON SRC="http://..."--> tag by an <IMG SRC="http://..." />

        try {
            iWebBeaconStart = Gadgets.indexOfIgnoreCase(sBody,"<!--WEBBEACON", 0);
            if (iWebBeaconStart>0) {
              iSrcTagStart = sBody.indexOf('"', iWebBeaconStart+13);
              iSrcTagEnd = sBody.indexOf('"', iSrcTagStart+1);
              iWebBeaconEnd = sBody.indexOf("-->", iWebBeaconStart+13);
              if (iWebBeaconEnd>0) {
            sBody = sBody.substring(0,iWebBeaconStart)+"<IMG SRC=\""+sBody.substring(iSrcTagStart+1,iSrcTagEnd)+"\" WIDTH=\"1\" HEIGHT=\"1\" BORDER=\"0\" ALT=\"\" />"+sBody.substring(iWebBeaconEnd+3);
              }
            }
        } catch (Exception malformedwebbeacon) {
          if (DebugFile.trace) DebugFile.writeln("Malformed Web Beacon");
        }
         
          oMsgHtml.setText(sBody,Charset.forName(sEncoding).name(),"html");
          oTextHtmlAlt.addBodyPart(oMsgHtml);
         
      } else {

        // Set HTML text related part

        MimeBodyPart oMsgHtmlText = new MimeBodyPart();
        oMsgHtmlText.setDisposition("inline");

        // ****************************************************************************
        // Replace <!--WEBBEACON SRC="http://..."--> tag by an <IMG SRC="http://..." />
         
        try {
          iWebBeaconStart = Gadgets.indexOfIgnoreCase(sBody,"<!--WEBBEACON", 0);
          if (iWebBeaconStart>0) {
            iSrcTagStart = sBody.indexOf('"', iWebBeaconStart+13);
            iSrcTagEnd = sBody.indexOf('"', iSrcTagStart+1);
            iWebBeaconEnd = sBody.indexOf("-->", iWebBeaconStart+13);
            if (iWebBeaconEnd>0) {
            sBody = sBody.substring(0,iWebBeaconStart)+"<IMG SRC=\""+sBody.substring(iSrcTagStart+1,iSrcTagEnd)+"\" WIDTH=\"1\" HEIGHT=\"1\" BORDER=\"0\" ALT=\"\" />"+sBody.substring(iWebBeaconEnd+3);
            }
          }
      } catch (Exception malformedwebbeacon) {
          if (DebugFile.trace) DebugFile.writeln("Malformed Web Beacon");
      }
       
        oMsgHtmlText.setText(sBody,Charset.forName(sEncoding).name(),"html");
        if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/related).addBodyPart(text/html)");
        oHtmlRelated.addBodyPart(oMsgHtmlText);

        // Set HTML text related inline images

        Iterator oImgs = oHtmBdy.getImagesCids().keySet().iterator();

        while (oImgs.hasNext()) {
          BodyPart oImgBodyPart = new MimeBodyPart();

          sSrc = (String) oImgs.next();
          sCid = (String) oHtmBdy.getImagesCids().get(sSrc);

          if (sSrc.startsWith("www."))
            sSrc = "http://" + sSrc;

          if (sSrc.startsWith("http://") || sSrc.startsWith("https://")) {
            oImgBodyPart.setDataHandler(new DataHandler(new URL(Hosts.resolve(sSrc))));
          }
          else {
            oImgBodyPart.setDataHandler(new DataHandler(new FileDataSource(sSrc)));
          }
     
      if (sSrc.endsWith(".png")) oImgBodyPart.setHeader("Content-Type", "image/png;name="+sCid);
          oImgBodyPart.setDisposition("inline");
          oImgBodyPart.setHeader("Content-ID", sCid);
          oImgBodyPart.setFileName(sCid);

          // Add image to multi-part
          if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/related).addBodyPart("+sCid+")");
          oHtmlRelated.addBodyPart(oImgBodyPart);
        } // wend

        // Set html text alternative part (html text + inline images)
        MimeBodyPart oTextHtmlRelated = new MimeBodyPart();
        oTextHtmlRelated.setContent(oHtmlRelated);
        if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/alternative).addBodyPart(multipart/related)");
        oTextHtmlAlt.addBodyPart(oTextHtmlRelated);
      }

      // ************************************************************************
      // Create message to be sent and add main text body to it

      if (0==iDraftParts) {
        oSentMessage.setContent(oTextHtmlAlt);
      } else {
        MimeBodyPart oMixedPart = new MimeBodyPart();
        oMixedPart.setContent(oTextHtmlAlt);
        oSentMsgParts.addBodyPart(oMixedPart);
      }

    } else { // (sContentType=="plain")

      // *************************************************
      // If this is a plain text message just add the text

      if (0==iDraftParts) {
        oSentMessage.setText(sBody, Charset.forName(sEncoding).name(), "plain");
      } else {
        oMsgPlainText.setDisposition("inline");
        oMsgPlainText.setText(sBody,Charset.forName(sEncoding).name(),"plain");
        if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/mixed).addBodyPart(text/plain)");
        oSentMsgParts.addBodyPart(oMsgPlainText);
      }
    }
    // fi (sContentType=="html")

    // ************************************************************************
    // Add attachments to message to be sent

    if (iDraftParts>0) {

      for (int p=0; p<iDraftParts; p++) {
        DBMimePart oPart = (DBMimePart) oDraftParts.getBodyPart(p);

        String sDisposition = oPart.getDisposition();
        if (sDisposition==null)
          sDisposition = "inline";
        else if (sDisposition.equals("reference") || sDisposition.equals("pointer"))
          sDisposition = "attachment";

        int iSize = oPart.getSize();
        if (iSize<=0) iSize = 4000;
        InputStream oInStrm = oPart.getInputStream();
        ByteArrayOutputStream oByStrm = new java.io.ByteArrayOutputStream(iSize);
        new StreamPipe().between(oInStrm, oByStrm);
        oInStrm.close();

        if (DebugFile.trace) DebugFile.writeln("part " + String.valueOf(p) + " size is " + String.valueOf(oByStrm.size()));

        ByteArrayDataSource oDataSrc = new ByteArrayDataSource(oByStrm.toByteArray(), oPart.getContentType());
        MimeBodyPart oAttachment = new MimeBodyPart();
        oAttachment.setDisposition(sDisposition);
        if (sDisposition.equals("attachment"))
        if (null==oPart.getDescription())
          oAttachment.setFileName(oPart.getFileName());
        else
          oAttachment.setFileName(oPart.getDescription());
        oAttachment.setHeader("Content-Transfer-Encoding", "base64");
        oAttachment.setDataHandler(new DataHandler(oDataSrc));
        oSentMsgParts.addBodyPart(oAttachment);
      } // next
      oSentMessage.setContent(oSentMsgParts);
    } // fi (iDraftParts>0)

View Full Code Here

    props.setProperty("mail.smtp.host", "localhost");
    props.setProperty("mail.smtp.port", SMTP_PORT+"");
    Session session = Session.getInstance(props);

    MimeMessage baseMsg = new MimeMessage(session);
    MimeBodyPart bp1 = new MimeBodyPart();
    bp1.setHeader("Content-Type", "text/plain");
    bp1.setContent("Hello World!!!", "text/plain; charset=\"ISO-8859-1\"");

    // Attach the file
    MimeBodyPart bp2 = new MimeBodyPart();
    FileDataSource fileAttachment = new FileDataSource(BIGFILE_PATH);
    DataHandler dh = new DataHandler(fileAttachment);
    bp2.setDataHandler(dh);
    bp2.setFileName(fileAttachment.getName());

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(bp1);
    multipart.addBodyPart(bp2);
View Full Code Here

        InputStream oLongVarBin = oRSet.getBinaryStream(17);

        if (!oRSet.wasNull()) {
          if (DebugFile.trace) DebugFile.writeln("MimeMultipart.addBodyPart(new MimeBodyPart(InputStream)");
          oParts.addBodyPart(new MimeBodyPart(oLongVarBin));
        }

        oRSet.close();
        oRSet = null;
View Full Code Here

TOP

Related Classes of javax.mail.internet.MimeBodyPart

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.