Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

javax.mail.internet.ParseException

Posted on 2004-10-27
4
Medium Priority
?
2,832 Views
Last Modified: 2010-05-19
I am trying to attach a PDF generated using itext as an attachment with email. I get a javax.mail.internet.ParseException.

Here is my code:
            

      Session session1 = Session.getDefaultInstance(props, null);
      Message message = new MimeMessage(session1);
      message.setFrom(new InternetAddress(from));
      message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject(sub);
                  
      //Create the message part
      BodyPart messageBodyPart = new MimeBodyPart();

               //Fill the message
      messageBodyPart.setText("Here's the file");
                  
      
             //      Create a Multipart
      Multipart multipart = new MimeMultipart();
          multipart.addBodyPart(messageBodyPart);

           // Part two is attachment
            messageBodyPart = new MimeBodyPart();
            javax.activation.DataSource ds = new ByteArrayDataSource(baosPDF,"application/pdf");
            messageBodyPart.setDataHandler(new DataHandler(ds));
                  
      messageBodyPart.setFileName("test.pdf");
                  
        // Add part two
                  
         multipart.addBodyPart(messageBodyPart);
     
 // Put parts in message
message.setContent(multipart);
 // Send the message
Transport.send(message);


in the ByteArrayDataSoruce if I change from boasPDF.toByteArrya() it still gives me this exception.

WHERE baosPDF is the ByteArrayOutputStream().

If I change it to boasPDF.toString() it sends a PDF but does not have any content.

Can some please say what is wrong in the above block of code.

0
Comment
Question by:new2j4v4
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:tdisessa
ID: 12425941
How do you build "baosPDF"?
0
 

Author Comment

by:new2j4v4
ID: 12426153
protected ByteArrayOutputStream generatePDFDocumentBytes(final HttpServletRequest req,final ServletContext ctx)
                  throws DocumentException {
                  Document doc = new Document();

                  ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
                  PdfWriter docWriter = null;

                  try {
                        docWriter = PdfWriter.getInstance(doc, baosPDF);

                        doc.addAuthor(this.getClass().getName());
                        doc.addCreationDate();
                        doc.addProducer();
                        doc.addCreator(this.getClass().getName());
                        doc.addTitle("Report");
                        doc.addKeywords("pdf, itext, Java, open source, http");

                        doc.setPageSize(PageSize.LETTER);
                        
                        HeaderFooter footer = new HeaderFooter(new Phrase("What to display",colFontInit),false);
                        doc.setFooter(footer);
                        Image gif = Image.getInstance("C:/Program Files/Apache Group/Tomcat 4.1/webapps/Test/images/tt.gif");
                        Chunk ch = new Chunk(gif,0,-5);
                        Chunk chHeader = new Chunk("The title",FontFactory.getFont(FontFactory.HELVETICA,12,Font.BOLDITALIC));
                        Chunk chHeader1 = new Chunk("Company title",FontFactory.getFont(FontFactory.HELVETICA,9,Font.BOLD));
                  
                        Paragraph myParagraph = new Paragraph();
                        myParagraph.add(gif);
                        int x = myParagraph.size();
                        Paragraph para = new Paragraph();
                                    
                        HeaderFooter header = new HeaderFooter(myParagraph,false);
                        header.setBorder(0);
                        doc.setHeader(header);
                        doc.open();
                        doc.add(new Paragraph("\n"));
                        doc.add(new Paragraph("The column name"));
                        doc.close();
                        docWriter.flush();
                        docWriter.close();
                  } catch (DocumentException dex) {
                        baosPDF.reset();
                        throw dex;
                  }catch (IOException ioe)
                  {
                        System.out.println("mdaPdf: "+ioe);
                  }
                  finally {
                        if (doc != null) {
                              doc.close();
                        }
                        if (docWriter != null) {
                              docWriter.close();
                        }
                  }

                  if (baosPDF.size() < 1) {
                        throw new DocumentException(
                              "document has " + baosPDF.size() + " bytes");
                  }
                  return baosPDF;
            }

Also the ByteArrayDataSource code:

package com.mda.util;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.activation.DataSource;


/**
 * Used to create a DataSource for the mail message.
 * @see MailHelper
 */
public class ByteArrayDataSource implements DataSource {
   

      /**
       * @param baosPDF
       * @param applicationType
       */
      public ByteArrayDataSource(ByteArrayOutputStream baosPDF, String applicationType) {
            
            this.data = baosPDF.toByteArray();
            this.type = type;
      }

      private byte[] data; // data for mail message
      private String type; // content type/mime type

   /**
      * Create a DataSource from a String
      * @param data is the contents of the mail message
      * @param type is the mime-type such as text/html
      */
      public ByteArrayDataSource(String data, String type) {
            try {
                   this.data = data.getBytes("UTF-8");
                  } catch (UnsupportedEncodingException uex) { }
            this.type = type;
      }

      /**
            * @param bs
            * @param applicationType
            */
         public ByteArrayDataSource(byte[] bs, String applicationType) {
            
              try{
                  this.data = bs;
                  } catch(Exception e){
                  System.out.println(e);
              }
               this.type = type;
         }
      //DataSource interface methods

      public InputStream getInputStream() throws IOException {
            if (data == null)
                  throw new IOException("no data");
            return new ByteArrayInputStream(data);
      }

      public OutputStream getOutputStream() throws IOException {
            throw new IOException("cannot do this");
      }

      public String getContentType() {
            return type;
      }

      public String getName() {
            return "dummy";
      }
}


0
 
LVL 2

Accepted Solution

by:
tdisessa earned 280 total points
ID: 12426505
First of all, a couple of the constructors for ByteArrayDataSource are specified incorrectly:

      public ByteArrayDataSource(ByteArrayOutputStream baosPDF, String applicationType) {
         
          this.data = baosPDF.toByteArray();
          this.type = type;
     }

        public ByteArrayDataSource(byte[] bs, String applicationType) {
         
            try{
               this.data = bs;
               } catch(Exception e){
               System.out.println(e);
            }
             this.type = type;
        }

should be:

      public ByteArrayDataSource(ByteArrayOutputStream baosPDF, String applicationType) {
         
          this.data = baosPDF.toByteArray();
          this.type = applicationType;
     }

        public ByteArrayDataSource(byte[] bs, String applicationType) {
         
            try{
               this.data = bs;
               } catch(Exception e){
               System.out.println(e);
            }
             this.type = applicationType;
        }

Try that and I'll keep looking for more.
0
 

Author Comment

by:new2j4v4
ID: 12427831
That worked thanks for the help
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question