javamail接受

package com.hygj.mail;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

/**
* <p>Title:</p>
* <p>Description:</p>
* <p>Copyright:Copyright(c)2009</p>
* <p>Company:华育国际教育集团-济南分校-软件工厂</p>
* <p>Created By:ZhangKe</p>
* <p>First Created:2009-6-5</p>
* @author: <a href="mailto:jasonyp@126.com">ZhangKe</a>
* @version: 1.0
*
*/
public class POP3Mail {

private String host = "";
private String username = "";
private String password = "";
private boolean auth = false;
private int allmailcount = 0;
private int noreadcount = 0;
private int newcount = 0;

public boolean receiveMail(){
boolean isreceive = false;

try {
//1.创建会话
Properties properties = new Properties();
properties.put("mail.pop3.host",host);
properties.put("mail.pop3.auth",String.valueOf(auth));
Session session = Session.getDefaultInstance(properties,new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});

//2.连接pop3服务器
Store store = session.getStore("pop3");
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
//3.收取邮件
Message [] mail = folder.getMessages();
//所有邮件个数
setAllmailcount(folder.getMessageCount());
setNewcount(folder.getNewMessageCount());
setNoreadcount(folder.getUnreadMessageCount());
System.out.println("邮件主题:"+mail[0].getSubject());
System.out.println("邮件内容:"+mail[0].getContent());
// mail[0].getInputStream()
System.out.println("邮件类型:"+mail[0].getContentType());
//4.保存邮件到本地
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


return isreceive;
}


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
POP3Mail mail = new POP3Mail();
mail.setHost("pop3.126.com");
mail.setUsername("jasonyp");
mail.setPassword("qaz123");
mail.setAuth(true);
mail.receiveMail();
}


public int getAllmailcount() {
return allmailcount;
}


public void setAllmailcount(int allmailcount) {
this.allmailcount = allmailcount;
}


public boolean isAuth() {
return auth;
}


public void setAuth(boolean auth) {
this.auth = auth;
}


public String getHost() {
return host;
}


public void setHost(String host) {
this.host = host;
}


public int getNewcount() {
return newcount;
}


public void setNewcount(int newcount) {
this.newcount = newcount;
}


public int getNoreadcount() {
return noreadcount;
}


public void setNoreadcount(int noreadcount) {
this.noreadcount = noreadcount;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public String getUsername() {
return username;
}


public void setUsername(String username) {
this.username = username;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值