Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Javamail API to download attachment from inbox

1060944Dec 17 2013 — edited Dec 20 2013

Hi All,

I have java code that I am using to connect to exchange server that uses smtp protocol and download attachment from inbox. In Ecllipse I have imported external jar files activation.jar and javamail1.4.7.zip. I am getting below errors:

No provider for smtp.

javax.mail.NoSuchProviderException: invalid provider

    at javax.mail.Session.getStore(Session.java:582)

    at javax.mail.Session.getStore(Session.java:548)

    at javax.mail.Session.getStore(Session.java:527)

    at EmailAttachmentReceiver.downloadEmailAttachments(EmailAttachmentReceiver.java:70)

    at EmailAttachmentReceiver.main(EmailAttachmentReceiver.java:157)

appreciate all your responses.

Java code is as below:

import java.io.File;

import java.io.IOException;

import java.util.Properties;

import javax.mail.Address;

import javax.mail.Folder;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.NoSuchProviderException;

import javax.mail.Part;

import javax.mail.Session;

import javax.mail.Store;

import javax.mail.internet.MimeBodyPart;

/**

* This program demonstrates how to download e-mail messages and save

* attachments into files on disk.

*

* @author www.codejava.net

*

*/

public class EmailAttachmentReceiver {

    private String saveDirectory;

    /**

     * Sets the directory where attached files will be stored.

     * @param dir absolute path of the directory

     */

    public void setSaveDirectory(String dir) {

        this.saveDirectory = dir;

    }

    /**

     * Downloads new messages and saves attachments to disk if any.

     * @param host

     * @param port

     * @param userName

     * @param password

     */

    public void downloadEmailAttachments(String host, String port,

            String userName, String password) {

        Properties properties = new Properties();

        // server setting

        properties.put("mail.smtp.host", host);

        properties.put("mail.smtp.port", port);

        properties.put("mail.smtp.auth", "true");

        // SSL setting

        //properties.setProperty("mail.smtp.socketFactory.class",

        ///        "javax.net.ssl.SSLSocketFactory");

       // properties.setProperty("mail.smtp.socketFactory.fallback", "false");

       // properties.setProperty("mail.smtp.socketFactory.port",

        //        String.valueOf(port));

        //Session session = Session.getDefaultInstance(properties,

        //        new javax.mail.Authenticator() { 

         //   protected PasswordAuthentication getPasswordAuthentication() { 

         //       return new PasswordAuthentication(user,password); 

          //     } 

          //    });

        Session session = Session.getDefaultInstance(properties);

        try {

            // connects to the message store

            Store store = session.getStore("smtp");

            store.connect(host,userName, password);

            // opens the inbox folder

            Folder folderInbox = store.getFolder("INBOX");

            folderInbox.open(Folder.READ_WRITE);

            // fetches new messages from server

            Message[] arrayMessages = folderInbox.getMessages();

            for (int i = 0; i < arrayMessages.length; i++) {

                Message message = arrayMessages[i];

                Address[] fromAddress = message.getFrom();

                String from = fromAddress[0].toString();

                String subject = message.getSubject();

                String sentDate = message.getSentDate().toString();

                String contentType = message.getContentType();

                String messageContent = "";

                // store attachment file name, separated by comma

                String attachFiles = "";

                if (contentType.contains("multipart")) {

                    // content may contain attachments

                    Multipart multiPart = (Multipart) message.getContent();

                    int numberOfParts = multiPart.getCount();

                    for (int partCount = 0; partCount < numberOfParts; partCount++) {

                        MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);

                        if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {

                            // this part is attachment

                            String fileName = part.getFileName();

                            attachFiles += fileName + ", ";

                            part.saveFile(saveDirectory + File.separator + fileName);

                        } else {

                            // this part may be the message content

                            messageContent = part.getContent().toString();

                        }

                    }

                    if (attachFiles.length() > 1) {

                        attachFiles = attachFiles.substring(0, attachFiles.length() - 2);

                    }

                } else if (contentType.contains("text/plain")

                        || contentType.contains("text/html")) {

                    Object content = message.getContent();

                    if (content != null) {

                        messageContent = content.toString();

                    }

                }

                // print out details of each message

                System.out.println("Message #" + (i + 1) + ":");

                System.out.println("\t From: " + from);

                System.out.println("\t Subject: " + subject);

                System.out.println("\t Sent Date: " + sentDate);

                System.out.println("\t Message: " + messageContent);

                System.out.println("\t Attachments: " + attachFiles);

            }

            // disconnect

            folderInbox.close(false);

            store.close();

        } catch (NoSuchProviderException ex) {

            System.out.println("No provider for smtp.");

            ex.printStackTrace();

        } catch (MessagingException ex) {

            System.out.println("Could not connect to the message store");

            ex.printStackTrace();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }

    /**

     * Runs this program with SMTP server

     */

    public static void main(String[] args) {

        String host = "smtpgate.domain.com";

        String port = "25";

        String userName = "example@domain.com";

        String password = "password";

        String saveDirectory = "C:\\Temp";

        EmailAttachmentReceiver receiver = new EmailAttachmentReceiver();

        receiver.setSaveDirectory(saveDirectory);

        receiver.downloadEmailAttachments(host, port, userName, password);

    }

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 17 2014
Added on Dec 17 2013
5 comments
4,172 views