在 java 中下载电子邮件附件



以下代码应该从邮件中下载任何附件(如果有(。Gmali , pop3 , javax.mail ...任何想法如何做到这一点,也许是针对邮件的特定发件人,或者检查该邮件标题中是否有特定的单词或类似的东西?

package emailattachmentsdownloader;
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.
 *
 * 
 *
 */
public class EmailAttachmentsDownloader {
    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.pop3.host", host);
        properties.put("mail.pop3.port", port);
        // SSL setting
        properties.setProperty("mail.pop3.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.pop3.socketFactory.fallback", "false");
        properties.setProperty("mail.pop3.socketFactory.port",
                String.valueOf(port));
        Session session = Session.getDefaultInstance(properties);
        try {
            // connects to the message store
            Store store = session.getStore("pop3");
            store.connect(userName, password);
            // opens the inbox folder
            Folder folderInbox = store.getFolder("INBOX");
            folderInbox.open(Folder.READ_ONLY);
            // 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 pop3.");
            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 Gmail POP3 server
     */
    public static void main(String[] args) {
        String host = "pop.gmail.com";
        String port = "995";
        String userName = "MYMAIL";
        String password = "MYPASS";
        String saveDirectory = "D:/Attachments";
        EmailAttachmentsDownloader receiver = new 
        EmailAttachmentsDownloader();
        receiver.setSaveDirectory(saveDirectory);
        receiver.downloadEmailAttachments(host, port, userName, password);
    }
}

但是当我运行它时,我收到以下错误。有什么问题,我该怎么办?

Exception in thread "main" java.lang.NoClassDefFoundError: 
com/sun/mail/util/MailLogger
at javax.mail.Session.initLogger(Session.java:227)
at javax.mail.Session.<init>(Session.java:212)
at javax.mail.Session.getDefaultInstance(Session.java:315)
at javax.mail.Session.getDefaultInstance(Session.java:355)
at emailattachmentsdownloader.EmailAttachmentsDownloader.downloadEmailAttachments(EmailAttachmentsDownloader.java:53)
at emailattachmentsdownloader.EmailAttachmentsDownloader.main(EmailAttachmentsDownloader.java:144)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
C:UsersAcaAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: 
Java returned: 1
BUILD FAILED (total time: 0 seconds)

在你的pom中添加下面的依赖项.xml然后再次尝试重建:

<dependencies>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.1</version>
    </dependency>
</dependencies>

相关内容

  • 没有找到相关文章

最新更新