Select Page

Send created pdf with javamail

Send created pdf with javamail
public void testSendPDWithJavamail() {
        String host="";
        String from="[email protected]";
        String to="[email protected]";
        // Get system properties
        Properties props = System.getProperties();
        // Setup mail server
        // Get session
        Session session = Session.getInstance(props,null);
        try {
            // Define message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
            message.setSubject("Hello #JavaMail Attachment subject");
            Multipart multipart = new MimeMultipart();

            // Part one is a text
            MimeBodyPart textMessageBodyPart = new MimeBodyPart();
            textMessageBodyPart.setText("Hi hello this is the msg in mail");

            // Part two is attachment
            MimeBodyPart attachmentMessageBodyPart=new MimeBodyPart();
            byte[] pdf =new Resource("A1U0E0.pdf").getRawContent();

            new DataHandler(new ByteArrayDataSource(pdf,"application/pdf")));

            // Put parts in message
            message.setContent(multipart); // Send the message
        catch(MessagingException e){;}
        catch(IOException e){;}
        System.out.println("sent msg");

About The Author

Cédric Walter

I worked with various Insurances companies across Switzerland on online applications handling billion premium volumes. I love to continuously spark my creativity in many different and challenging open-source projects fueled by my great passion for innovation and blockchain technology.In my technical role as a senior software engineer and Blockchain consultant, I help to define and implement innovative solutions in the scope of both blockchain and traditional products, solutions, and services. I can support the full spectrum of software development activities, starting from analyzing ideas and business cases and up to the production deployment of the solutions.I'm the Founder and CEO of Disruptr GmbH.