用java写接收邮件,总报错,求各位大大指导。另外求能运行成功的代码
import java.io.BufferedReader;
import
java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import
javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
public class Pop21cn {
public static void main(String[] args) throws
MessagingException,
IOException {
String pop3Server="pop.21cn.com";
String protocol="pop3";
String user="qhcp_17e189165";
String pwd="123456a";
Properties props = new Properties();
props.setProperty("mail.store.protocol", protocol);
props.setProperty("mail.pop3.host", pop3Server);
Session session = Session.getInstance(props);
session.setDebug(true);
Store store = session.getStore();
System.out.println("开始连接服务器...");
store.connect(pop3Server, user, pwd);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message [] messages = folder.getMessages();
int mailCounts = messages.length;
for(int i = 0; i < mailCounts; i++) {
String subject = messages[i].getSubject();
String from = (messages[i].getFrom()[0]).toString();
System.out.println("第 "+(i+1)+"封邮件的主题:"+subject);
System.out.println("第 "+(i+1)+"封邮件的发件人地址:"+from);
System.out.println("是否打开该邮件(yes/no)?:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
if("yes".equalsIgnoreCase(input)) {
messages[i].writeTo(System.out);
}
folder.close(false);
store.close();
}
}
}
Exception in thread "main" javax.mail.MessagingException: Not connectedC: QUIT
at com.sun.mail.pop3.POP3Store.checkConnected(POP3Store.java:279)
at com.sun.mail.pop3.POP3Store.getFolder(POP3Store.java:261)
at com.e19e.mail.Pop21cn.main(Pop21cn.java:68)
------解决方案--------------------连接问题啊。怎么可能不能用POP3?我客户端就是使用的POP3协议
------解决方案--------------------加上 props.put("mail.smtp.auth", "true"); 看看
------解决方案--------------------
public void receive() {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "pop3");
Session session = Session.getDefaultInstance(props, null);
URLName urln = new URLName("pop3", popHost, 110, null, user, pass);
Store store = null;
try {
store = session.getStore(urln);
} catch (NoSuchProviderException e1) {
e1.printStackTrace();
}
POP3Folder inbox = null;
int mailnum = 0;
try {
try {
store.connect();//连接POP3服务器
inbox = (POP3Folder) store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
mailnum = inbox.getMessageCount();
String[] uid = new String[mailnum];
MimeMessage terpmsg;
MimeMessage[] newMessages =new MimeMessage[mailnum] ;
// 使用此办法来判断是否为新邮件
// 试接收一封新邮件,判断邮件是否已经被下载过,是则停止下载, //否则判断下一封邮件,下载次数为 新邮件数+1
for (int i = mailnum - 1; i >= 0; i--) {
// 下载当前最新未下载邮件
terpmsg = (MimeMessage) inbox.getMessage(i + 1);
uid[i] = inbox.getUID(terpmsg);// 取得邮件UID // 在接收过的记录找不到则对邮件进行操作
if (CheckMailUID.isNewUID(uid[i])) {
isNewMail = true;
newMessages[newMailNum] = terpmsg;
newMailNum++;