Package javax.mail.internet

Examples of javax.mail.internet.MimeBodyPart


    MimeMessage oMsg;
    InternetAddress oFrom, oTo;
    MimeMultipart oAltParts = new MimeMultipart("alternative");
   
    // Set alternative plain/text part to avoid spam filters as much as possible
    MimeBodyPart oMsgTextPart = new MimeBodyPart();
  oMsgTextPart.setText("This message is HTML, but your e-mail client is not capable of rendering HTML messages", "UTF-8", "plain");

    MimeBodyPart oMsgBodyPart = new MimeBodyPart();
 
    try {
      if (null==getParameter("tx_sender"))
        oFrom = new InternetAddress(getParameter("tx_from"));
      else
        oFrom = new InternetAddress(getParameter("tx_from"), getParameter("tx_sender"));

      if (DebugFile.trace) DebugFile.writeln("to: " + oAtm.getStringNull(DB.tx_email, "ERROR Atom[" + String.valueOf(oAtm.getInt(DB.pg_atom)) + "].tx_email is null!"));

      oTo = new InternetAddress(oAtm.getString(DB.tx_email), oAtm.getStringNull(DB.tx_name,"") + " " + oAtm.getStringNull(DB.tx_surname,""));
    }
    catch (AddressException adre) {
      if (DebugFile.trace) DebugFile.writeln("AddressException " + adre.getMessage() + " job " + getString(DB.gu_job) + " atom " + String.valueOf(oAtm.getInt(DB.pg_atom)));

      oFrom = null;
      oTo = null;

      throw new MessagingException ("AddressException " + adre.getMessage() + " job " + getString(DB.gu_job) + " atom " + String.valueOf(oAtm.getInt(DB.pg_atom)));
    }

    if (DebugFile.trace) DebugFile.writeln("new MimeMessage([Session])");

    oMsg = new MimeMessage(oMailSession);

    oMsg.setSubject(getParameter("tx_subject"));

    oMsg.setFrom(oFrom);

    if (DebugFile.trace) DebugFile.writeln("MimeMessage.addRecipient(MimeMessage.RecipientType.TO, " + oTo.getAddress());

    oMsg.addRecipient(MimeMessage.RecipientType.TO, oTo);

    String sSrc = null, sCid = null;

    try {
   
    // Insert Web Beacon just before </BODY> tag
    if (Yes.equals(getParameter("bo_webbeacon"))) {
      int iEndBody = Gadgets.indexOfIgnoreCase(oReplaced, "</body>", 0);
      if (iEndBody>0) {
        String sWebBeaconDir = getProperty("webbeacon");       
        if (sWebBeaconDir==null) {
          sWebBeaconDir = Gadgets.chomp(getParameter("webserver"),'/')+"hipermail/";
        } else if (sWebBeaconDir.trim().length()==0) {
          sWebBeaconDir = Gadgets.chomp(getParameter("webserver"),'/')+"hipermail/";
        }
        oReplaced = oReplaced.substring(0, iEndBody)+"<img src=\""+Gadgets.chomp(sWebBeaconDir,'/')+"web_beacon.jsp?gu_job="+getString(DB.gu_job)+"&pg_atom="+String.valueOf(oAtm.getInt(DB.pg_atom))+"&gu_company="+oAtm.getStringNull(DB.gu_company,"")+"&gu_contact="+oAtm.getStringNull(DB.gu_contact,"")+"&tx_email="+oAtm.getStringNull(DB.tx_email,"")+"\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" />"+oReplaced.substring(iEndBody);         
      } // fi </body>
    } // fi (bo_webbeacon)
     
      // Images may be attached into message or be absolute http source references
      if (Yes.equals(getParameter("bo_attachimages"))) {

        if (DebugFile.trace) DebugFile.writeln("BodyPart.setText("+oReplaced.replace('\n',' ')+",UTF-8,html)");

        oMsgBodyPart.setText(oReplaced, "UTF-8", "html");

        // Create a related multi-part to combine the parts
        MimeMultipart oRelatedMultiPart = new MimeMultipart("related");
        oRelatedMultiPart.addBodyPart(oMsgBodyPart);

        Iterator<String> oImgs = oDocumentImages.keySet().iterator();

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

          sCid = oImgs.next();
          sSrc = oDocumentImages.get(sCid);

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

          if (sSrc.startsWith("http://") || sSrc.startsWith("https://")) {
            oImgBodyPart.setDataHandler(new DataHandler(new URL(sSrc)));
          }
          else {
            oImgBodyPart.setDataHandler(new DataHandler(new FileDataSource(sSrc)));
          }

          oImgBodyPart.setContentID(sCid);
          oImgBodyPart.setDisposition(oImgBodyPart.INLINE);
          oImgBodyPart.setFileName(sCid);

          // Add part to multi-part
          oRelatedMultiPart.addBodyPart(oImgBodyPart);
        } // wend

        MimeBodyPart oTextHtmlRelated = new MimeBodyPart();
        oTextHtmlRelated.setContent(oRelatedMultiPart);

    oAltParts.addBodyPart(oMsgTextPart);
    oAltParts.addBodyPart(oTextHtmlRelated);

      MimeMultipart oSentMsgParts = new MimeMultipart("mixed");
     MimeBodyPart oMixedPart = new MimeBodyPart();
       
        oMixedPart.setContent(oAltParts);
        oSentMsgParts.addBodyPart(oMixedPart);
       
        oMsg.setContent(oSentMsgParts);
      }
View Full Code Here


      if (attachments != null && attachments.length > 0) {
        // with attachment use multipart message
        Multipart multipart = new MimeMultipart();
        // 1) add body part
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(body);
        multipart.addBodyPart(messageBodyPart);
        // 2) add attachments
        for (File attachmentFile : attachments) {
          // abort if attachment does not exist
          if (attachmentFile == null || !attachmentFile.exists()) {
            result.setReturnCode(MailerResult.ATTACHMENT_INVALID);
            Tracing.logError("Tried to send mail wit attachment that does not exist::"
                + (attachmentFile == null ? null : attachmentFile.getAbsolutePath()), MailHelper.class);
            return msg;
          }
          messageBodyPart = new MimeBodyPart();
          DataSource source = new FileDataSource(attachmentFile);
          messageBodyPart.setDataHandler(new DataHandler(source));
          messageBodyPart.setFileName(attachmentFile.getName());
          multipart.addBodyPart(messageBodyPart);
        }
        // Put parts in message
        msg.setContent(multipart);
      } else {
View Full Code Here

             attachment.setValue(doc);
             attachment.encodeEnd(FacesContext.getCurrentInstance());
            
             // verify we built the message
             Assert.assertEquals(message.getAttachments().size(), 1);
             MimeBodyPart bodyPart = message.getAttachments().get(0);
             Assert.assertEquals(bodyPart.getFileName(), "filename.pdf");
             Assert.assertEquals(bodyPart.getDisposition(),"attachment");
          }
       }.run();
    }
View Full Code Here

                MimeMultipart body = (MimeMultipart) renderedMessage.getContent();
               
                // Check the alternative facet
                Assert.assertTrue(renderedMessage.getContentType().startsWith("multipart/mixed"));
                Assert.assertEquals(body.getCount(), 1);
                MimeBodyPart bodyPart = (MimeBodyPart) body.getBodyPart(0);
                Assert.assertTrue(bodyPart.getContentType().startsWith("multipart/alternative"));
                Assert.assertTrue(bodyPart.getContent() instanceof MimeMultipart);
                MimeMultipart bodyParts = (MimeMultipart) bodyPart.getContent();
                Assert.assertEquals(bodyParts.getCount(), 2);
                Assert.assertTrue(bodyParts.getBodyPart(0) instanceof MimeBodyPart);
                Assert.assertTrue(bodyParts.getBodyPart(1) instanceof MimeBodyPart);
                MimeBodyPart alternative = (MimeBodyPart) bodyParts.getBodyPart(0);
                MimeBodyPart html = (MimeBodyPart) bodyParts.getBodyPart(1);
                Assert.assertTrue(alternative.isMimeType("text/plain"));
                Assert.assertEquals(alternative.getDisposition(), "inline");
                Assert.assertTrue(html.isMimeType("text/html"));
                Assert.assertEquals(html.getDisposition(), "inline");

            }           
        }.run();
      
    }
View Full Code Here

                // Check the body
               
                Assert.assertNotNull(renderedMessage.getContent());
                MimeMultipart body = (MimeMultipart) renderedMessage.getContent();
                Assert.assertEquals(body.getCount(), 1);
                MimeBodyPart bodyPart = (MimeBodyPart) body.getBodyPart(0);
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertEquals(bodyPart.getDisposition(), "inline");
                Assert.assertTrue(bodyPart.isMimeType("text/plain"));
            }
        }.run();
    }
View Full Code Here

                MimeMultipart body = (MimeMultipart) renderedMessage.getContent();
               
                // Check the alternative facet
                Assert.assertTrue(renderedMessage.getContentType().startsWith("multipart/mixed"));
                Assert.assertEquals(body.getCount(), 1);
                MimeBodyPart bodyPart = (MimeBodyPart) body.getBodyPart(0);
                Assert.assertTrue(bodyPart.getContentType().startsWith("multipart/alternative"));
                Assert.assertTrue(bodyPart.getContent() instanceof MimeMultipart);
                MimeMultipart bodyParts = (MimeMultipart) bodyPart.getContent();
                Assert.assertEquals(bodyParts.getCount(), 2);
                Assert.assertTrue(bodyParts.getBodyPart(0) instanceof MimeBodyPart);
                Assert.assertTrue(bodyParts.getBodyPart(1) instanceof MimeBodyPart);
                MimeBodyPart alternative = (MimeBodyPart) bodyParts.getBodyPart(0);
                MimeBodyPart html = (MimeBodyPart) bodyParts.getBodyPart(1);
                Assert.assertTrue(alternative.isMimeType("text/plain"));
                Assert.assertEquals(alternative.getDisposition(), "inline");
                Assert.assertTrue(html.isMimeType("text/html"));
                Assert.assertEquals(html.getDisposition(), "inline");
            }
        }.run();
    }
View Full Code Here

                Assert.assertTrue(renderedMessage.getContent() instanceof MimeMultipart);
                MimeMultipart body = (MimeMultipart) renderedMessage.getContent();
                Assert.assertEquals(body.getCount(), 1);
                Assert.assertNotNull(body.getBodyPart(0));
                Assert.assertTrue(body.getBodyPart(0) instanceof MimeBodyPart);
                MimeBodyPart bodyPart = (MimeBodyPart) body.getBodyPart(0);
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertEquals(bodyPart.getDisposition(), "inline");
                Assert.assertTrue(bodyPart.isMimeType("text/html"));               
            }           
        }.run();
      
    }   
View Full Code Here

                Assert.assertEquals(body.getCount(), 4); //3 Attachments and 1 MimeMultipart               
               
                // The root multipart/related
                Assert.assertNotNull(body.getBodyPart(0));
                Assert.assertTrue(body.getBodyPart(0) instanceof MimeBodyPart);
                MimeBodyPart bodyPart = (MimeBodyPart) body.getBodyPart(0);
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertTrue(bodyPart.getContent() instanceof MimeMultipart);
                Assert.assertTrue(bodyPart.isMimeType("multipart/related"));
               
                MimeMultipart attachments = (MimeMultipart) bodyPart.getContent();
               
                Assert.assertEquals(attachments.getCount(), 3); //2 Attachments and 1 MimeMultipart
               
                // Attachment 0 (the actual message)
                Assert.assertNotNull(attachments.getBodyPart(0));               
                Assert.assertTrue(attachments.getBodyPart(0) instanceof MimeBodyPart);
                bodyPart = (MimeBodyPart) attachments.getBodyPart(0);
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertTrue(bodyPart.isMimeType("text/html"));
                Assert.assertEquals(bodyPart.getDisposition(), "inline");
               
                // Inline Attachment 1 // Attachment Jboss Logo
                Assert.assertNotNull(attachments.getBodyPart(1));               
                Assert.assertTrue(attachments.getBodyPart(1) instanceof MimeBodyPart);
                bodyPart = (MimeBodyPart) attachments.getBodyPart(1);
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertTrue(bodyPart.getContent() instanceof InputStream);
                Assert.assertEquals(bodyPart.getFileName(), "jboss.jpg");
                Assert.assertTrue(bodyPart.isMimeType("image/jpeg"));
                Assert.assertEquals(bodyPart.getDisposition(), "inline");
                Assert.assertNotNull(bodyPart.getContentID());
               
                // Inline Attachment 1 // Attachment Gavin Pic
                Assert.assertNotNull(attachments.getBodyPart(2));               
                Assert.assertTrue(attachments.getBodyPart(2) instanceof MimeBodyPart);
                bodyPart = (MimeBodyPart) attachments.getBodyPart(2);
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertTrue(bodyPart.getContent() instanceof InputStream);
                Assert.assertEquals(bodyPart.getFileName(), "Gavin_King.jpg");
                Assert.assertTrue(bodyPart.isMimeType("image/png"));
                Assert.assertEquals(bodyPart.getDisposition(), "inline");
                Assert.assertNotNull(bodyPart.getContentID());
               
                // Root Attachment 0
                Assert.assertNotNull(body.getBodyPart(1));               
                Assert.assertTrue(body.getBodyPart(1) instanceof MimeBodyPart);
                bodyPart = (MimeBodyPart) body.getBodyPart(1);
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertTrue( bodyPart.getContent() instanceof InputStream);
                Assert.assertEquals(bodyPart.getFileName(), "numbers.csv");
                Assert.assertTrue(bodyPart.isMimeType("content/unknown"));
                Assert.assertEquals(bodyPart.getDisposition(), "attachment");
                Assert.assertNull(bodyPart.getContentID());               
               
                // Root Attachment 1
                Assert.assertNotNull(body.getBodyPart(2));               
                Assert.assertTrue(body.getBodyPart(2) instanceof MimeBodyPart);
                bodyPart = (MimeBodyPart) body.getBodyPart(2);
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertTrue(bodyPart.getContent() instanceof String);
                Assert.assertEquals(bodyPart.getFileName(), "whyseam.pdf");
                Assert.assertEquals(bodyPart.getDisposition(), "attachment");
                Assert.assertNull(bodyPart.getContentID());
               
                // Root Attachment 3
                Assert.assertNotNull(body.getBodyPart(3));               
                Assert.assertTrue(body.getBodyPart(3) instanceof MimeBodyPart);
                bodyPart = (MimeBodyPart) body.getBodyPart(3);
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertEquals(bodyPart.getFileName(), "excel.xls");
                Assert.assertTrue(bodyPart.isMimeType("application/vnd.ms-excel"));
                Assert.assertEquals(bodyPart.getDisposition(), "attachment");
                Assert.assertNull(bodyPart.getContentID());               
               
            }           
        }.run();
      
    }
View Full Code Here

    Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        try {
          Multipart mp = new MimeMultipart();
          MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(htmlBody, "text/html");
            htmlPart.setHeader("Content-type", "text/html; charset=UTF-8");
            mp.addBodyPart(htmlPart);
            for (FileItem item : files) {
              MimeBodyPart attachment = new MimeBodyPart();
                attachment.setFileName(item.getFilename());
                String mimeType = MimeType.getContentTypeByExt(
                    FolderUtil.getFileExt(item.getFilename()));
                DataSource ds = new ByteArrayDataSource(item.getData(), mimeType);
                attachment.setDataHandler(new DataHandler(ds));
                mp.addBodyPart(attachment);
            }
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(fromAddress, fromText));
            msg.addRecipient(Message.RecipientType.TO,
View Full Code Here

    private void add_file( SOSMailAttachment att throws Exception
    {
        if( !att.getFile().exists() )  throw new Exception( "Datei "+ att.getFile().getAbsolutePath() + " fehlt" );
       
       
        MimeBodyPart attachment = new MimeBodyPart();

        DataSource  data_source  = new File_data_source( att.getFile(), att.getContentType() );
        DataHandler data_handler = new DataHandler( data_source );

        attachment.setDataHandler( data_handler );
        attachment.setFileName   ( att.getFile().getName() );

        //Charset des Attachments setzen, wenn content_type text/ ist
        if (att.getContentType().startsWith("text/")){
          // Charset macht nur Sinn bei text/

            String s = "";
            FileReader fr = new FileReader( att.getFile() );
          
            for ( int c; ( c = fr.read() ) != -1; )
              s += ( (char) c );
            attachment.setText(s,att.getCharset());
           fr.close();
        }       
       
        Object m = message.getContent();
        if( !( m instanceof MimeMultipart ) )  throw new RuntimeException( getClass().getName() + "mime_message.getContent() liefert nicht MimeMultiPart" );
        ((MimeMultipart)m).addBodyPart( attachment );
        attachment.setHeader("Content-Transfer-Encoding", att.getEncoding());        
    }
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.