Dear Sir,

 

Attached please find two Java files, PageXofY.java (sample code from 
itextpdf.com with a minor addition to email) and MailerTest.java

 

I tried to send generated PDF through SMTP using Java mail api. It puzzled me 
that the content of PDF once received in email is blank unless the 
Content-Transfer-Encoding is set to base64. For example,

 

bodyPart.setHeader("Content-Transfer-Encoding", "base64");  // in 
MailerTest.java attached

 

Does iText support other Content-Transfer-Encoding like quoted-printable? Or is 
there anything wrong with my usage of Java mail?

 

Regards,

 

Jiangang Song

 

 
                                          
_________________________________________________________________
Hotmail is redefining busy with tools for the New Busy. Get more from your 
inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2
import java.io.*;
import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;



public class MailerTest
{
    private Session       session;

    public MailerTest(Properties props)
    {
                session = Session.getInstance(props,null);
        }

    public void sendMail(Address to, Address from, String subject, String 
msgText, byte[] fileData,
        String fileName) throws MessagingException, IOException
    {
        // Create a message.
        MimeMessage msg = new MimeMessage(session);

        // Set basic message info.
        msg.setFrom(from);
        msg.setSubject((subject == null) ? "" : subject, "UTF-8");
        msg.setSentDate(new Date());

        // Create main text part of message
        MimeBodyPart bodyPart = new MimeBodyPart();
        //bodyPart.setText((msgText == null) ? "" : msgText, "UTF-8");
        bodyPart.setText(msgText);

        // Add to multipart
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(bodyPart);

        // Create a new part for the attachment, and set the file data
        bodyPart = new MimeBodyPart();

        ByteArrayDataSource bads = new ByteArrayDataSource(fileData, 
"application/octet-stream");
        bodyPart.setDataHandler(new DataHandler(bads));
        bodyPart.setFileName(fileName);
        bodyPart.setHeader("Content-Type","text/plain; charset=\"utf-8\"");
        //bodyPart.setHeader("Content-Transfer-Encoding", "base64");
        bodyPart.setHeader("Content-Transfer-Encoding", "quoted-printable");

        // Add it to multipart, and set the multipart as content for the mail 
message
        multipart.addBodyPart(bodyPart);
        msg.setContent(multipart);

                msg.setRecipient(Message.RecipientType.TO, to);
                Transport.send(msg);
    }


        private class ByteArrayDataSource implements DataSource
            {
                private byte[] data; // data
                private String type; // content-type

                // Create a DataSource from a byte array
                public ByteArrayDataSource(byte[] content, String type)
                {
                    System.out.println("call constructer");
                    if (content != null)
                    {
                        this.data = content;
                    }

                    this.type = type;
                }

                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";
                }
    }
}
import java.io.*;
import java.util.*;


import javax.mail.internet.*;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;



/**
 * This example was written by Bruno itextpdf. It is part of the book 'iText in
 * Action' by Manning Publications.
 * ISBN: 1932394796
 * http://www.1t3xt.com/docs/book.php
 * http://www.manning.com/itextpdf/


 *  align


 */

public class PageXofY extends PdfPageEventHelper {



        /** The PdfTemplate that contains the total number of pages. */
        protected PdfTemplate total;

        /** The font that will be used. */
        protected BaseFont helv;

        /**
         * @see 
com.itextpdf.text.pdf.PdfPageEvent#onOpenDocument(com.itextpdf.text.pdf.PdfWriter,
         *      com.itextpdf.text.Document)
         */
        public void onOpenDocument(PdfWriter writer, Document document) {
                total = writer.getDirectContent().createTemplate(50, 50);
                total.setBoundingBox(new Rectangle(-20, -20, 50, 50));
                try {
                        helv = BaseFont.createFont(BaseFont.HELVETICA, 
BaseFont.WINANSI,
                                        BaseFont.NOT_EMBEDDED);
                } catch (Exception e) {
                        throw new ExceptionConverter(e);
                }
        }

        /**
         * @see 
com.itextpdf.text.pdf.PdfPageEvent#onEndPage(com.itextpdf.text.pdf.PdfWriter,
         *      com.itextpdf.text.Document)
         */
        public void onEndPage(PdfWriter writer, Document document) {
                PdfContentByte cb = writer.getDirectContent();
                cb.saveState();
                String text = writer.getPageNumber() + "/";
                float textBase = document.bottom() - 20;
                float textSize = helv.getWidthPoint(text, 12);
                cb.beginText();
                cb.setFontAndSize(helv, 12);

                        cb.setTextMatrix((document.left()+document.right())/2, 
textBase);
                        cb.showText(text);
                        cb.endText();
                        cb.addTemplate(total, 
(document.left()+document.right())/2 + textSize, textBase);

                cb.restoreState();
        }

        /**
         * @see 
com.itextpdf.text.pdf.PdfPageEvent#onCloseDocument(com.itextpdf.text.pdf.PdfWriter,
         *      com.itextpdf.text.Document)
         */
        public void onCloseDocument(PdfWriter writer, Document document) {
                total.beginText();
                total.setFontAndSize(helv, 12);
                total.setTextMatrix(0, 0);
                total.showText(String.valueOf(writer.getPageNumber() - 1));
                total.endText();
        }

        /**
         * Generates a file with a header and a footer.
         *
         * @param args
         *            no arguments needed here
         */
        public static void main(String[] args) {
                System.out.println("Chapter 14: Page X of Y Example");
                System.out.println("-> Creates a PDF file with page numbers.");
                System.out.println("-> jars needed: iText.jar");
                System.out.println("-> files generated in /results 
subdirectory:");
                System.out.println("   page_numbers.pdf");
                // step 1: creation of a document-object
                Document document = new Document(PageSize.LETTER);
                ByteArrayOutputStream byteArrayOutputStream = new 
ByteArrayOutputStream();
                try {
                        // step 2:
                        PdfWriter writer = PdfWriter.getInstance(document,new 
BufferedOutputStream(byteArrayOutputStream));
                        
writer.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
                        writer.setPageEvent(new PageXofY());
                        document.setMargins(36, 36, 36, 54);
                        // step 3:
                        document.open();
                        Paragraph p = new Paragraph();
                        // step 4: we grab the ContentByte and do some stuff 
with it
                        for (int k = 1; k <= 30; ++k) {
                                p.add(new Phrase("Quick brown fox jumps over 
the lazy dog. "));
                        }
                        p.setAlignment(Element.ALIGN_JUSTIFIED);
                        for (int k = 1; k <= 12; ++k) {
                                document.add(p);
                        }

                        document.close();
                        writer.close();
                } catch (DocumentException de) {
                        System.err.println(de.getMessage());
                }


                try
                {
                        Properties props = new Properties();
                        props.put("mail.smtp.host", "dipsy");
                        props.put("mail.smtp.port", "25");

                        MailerTest mailer = new MailerTest(props);

                        InternetAddress from = new 
InternetAddress("[email protected]");
                        InternetAddress to = new 
InternetAddress("[email protected]");



                        mailer.sendMail(to, from, "test", "This is a test", 
byteArrayOutputStream.toByteArray(), "test.pdf");
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }

        }



}
------------------------------------------------------------------------------

_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to