用java写接收邮件,总报错,求各位大大指导。另外求能运行成功的代码

jkfjsyc 2014-02-11 11:02:03
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)

...全文
460 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunskyii 2014-03-13
  • 打赏
  • 举报
回复
为什么我的jar包不能调用inbox.getUID 你们都是什么版本的?
Railre 2014-02-18
  • 打赏
  • 举报
回复
楼上正解啊! 一般按照楼上说的都能解决~
  • 打赏
  • 举报
回复
加上 props.put("mail.smtp.auth", "true"); 看看
jkfjsyc 2014-02-11
  • 打赏
  • 举报
回复
就是不知道怎么连接了,试验了很多都是连接不上 不知道怎么回事!
teemai 2014-02-11
  • 打赏
  • 举报
回复
连接问题啊。怎么可能不能用POP3?我客户端就是使用的POP3协议
Hanlos 2014-02-11
  • 打赏
  • 举报
回复
pop貌似 是不能用了阿 !!!!
jkfjsyc 2014-02-11
  • 打赏
  • 举报
回复
求能运行成功的代码和邮件,或邮箱,听说网易这种06年之后注册的都不支持pop了
jkfjsyc 2014-02-11
  • 打赏
  • 举报
回复
已经试验过163、sina、sohu等pop服务器,都连接不上,都大神看看哪里有问题。菜鸟什么都不会,求大神指导啊
幻夢之葉 2014-02-11
  • 打赏
  • 举报
回复
不能连接的情况可能为以下: 1.服务器,用户名,密码等错误! 2.部分邮箱服务商不支持mail.jar这种简单不安全的方式连接pop3服务器! 3.部分邮箱服务器默认不开通pop3,smtp等服务,需登录到邮箱设置! 4.邮箱有独立密码的,如QQ邮箱开通独立密码时,不能登录成功! QQ,163应该是支持的(去年来说还是可以的,亲测) google,sohu,微软那个叫啥啥的等不支持!
幻夢之葉 2014-02-11
  • 打赏
  • 举报
回复

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++;

      

     } else {

      break;

     }

    }

    int reallength = 0;

    for(int i = 0;i < newMessages.length; i++){//取得newMessage的长度

     if(newMessages[i] != null){

      reallength++;

     }else{

      break;

     }

    }

    if(reallength > 0 ){//遍历新邮件并保存

     for(int j = reallength-1;j >= 0; j--){

      ReceiveMailUID rmu = new ReceiveMailUID();

//对应邮件uid实际位置mailnum-j-1

      rmu.addUID(uid[mailnum-j-1]);

      SaveMail savemail = new SaveMail(newMessages[j]);

      savemail.saveMail();

     }

    }

   } catch (MessagingException e) {

    e.printStackTrace();

   }

  } finally {

   try {

    inbox.close(false);

    store.close();

    isEnd = true;

   } catch (Exception e) {

   }

  }

  isReceiveSuccess = true;

 }
以前写的,你可以忽略某些部分!!
jkfjsyc 2014-02-11
  • 打赏
  • 举报
回复
谢谢各位啊,我找找其他的方式试试。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧