正确导入JavaMail

时间:2014-11-15 13:02:31

标签: java email javamail

好的,那我该如何导入JavaMail?我已经阅读了至少15个关于如何解决此问题的不同想法,并尝试将javax.mail.jar移动到导出列表的顶部,依此类推。显然,正在抛出一个ClassNotFoundException来启动一个新的Session,并且根据我读过的所有其他帖子,它应该可以工作。这是我的代码:

package y.mail;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Session;
import javax.mail.Store;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class MailReader extends JFrame {
private static final long serialVersionUID = 1L;

public static void main(String[] args) {
     new MailReader();
    }

 public MailReader() {
     this.setSize(800, 500);
     this.setLocationRelativeTo(null);
     this.setResizable(false);
     this.setLayout(new BorderLayout());
     this.setTitle("MailReader");
     JTextArea area = new JTextArea();
     area.setEditable(false);
     area.setForeground(Color.GREEN);
     area.setBackground(Color.BLACK);
     this.add(new JScrollPane(area),BorderLayout.CENTER);
     this.setVisible(true);
     area.append("Attempting to access inbox..\n");
     Properties props = System.getProperties();
     props.setProperty("mail.store.protocol", "imaps");
     try {
       Session session = Session.getDefaultInstance(props, null);
       Store store = session.getStore("imaps");
       store.connect("imap.googlemail.com", "myemailid@gmail.com", "password");
       Folder f = store.getFolder("INBOX");
       area.append("Connected to inbox!");
       area.append(f.getMessageCount() + "");
     } catch (Exception e) {
       area.append("===========Error===========\n");
       area.append(e.getStackTrace() + "\n");
     }
 }

}

1 个答案:

答案 0 :(得分:1)

您是否按照说明here设置了CLASSPATH?

您会在JavaMail FAQ中找到许多其他有用的信息,包括common mistakes的此列表。