分享一个收邮件的类,演示Iterable的用法,实现自己的Iterator, 该类使用非常简单,如下:
Properties mailPr = new Properties();
mailPr.setProperty("Pop3Host","pop3.sina.com.cn");
mailPr.setProperty("User",” ××××@sina.com.cn“);
mailPr.setProperty("Password", “×××××”);
mr = new MailReceiver(mailPr);
mr = new MailReceiver(mailPr);
try {
mr.read();
//遍历所有邮件
for (Iterator<Message> iter = mr.iterator(); iter.hasNext();) {
Message msg = iter.next();
//msg 就是读出的邮件
}
} catch (MessagingException e1) {
e1.printStackTrace();
return;
}
主要包含了两个类,如下,
1 . MailReceiver.java
package zhang.stony.mail;
import java.util.Iterator;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
/**
* @author Stony Zhang
* @E-Mail stonyz@live.com
* @QQ 55279427
* @Createdate 2009-9-5
*
* @Copyright the author, If you want to use the code, you must keep the
* author's information.
*/
public class MailReceiver implements Iterable<Message> {
private Session session;
MailSetting mSetting;
public MailReceiver(Properties p) {
mSetting = new MailSetting(p);
final String username = mSetting.getUser();
final String password = mSetting.getPassword();
Properties props = new Properties();
props.put("mail.smtp.host", mSetting.getPop3Host());
props.put("mail.smtp.auth", "true");
session = Session.getDefaultInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
Folder folder = null;
Message[] message;
Store store;
public void read() throws MessagingException {
store = session.getStore("pop3");
store.connect(mSetting.getPop3Host(), mSetting.getUser(), mSetting
.getPassword());
folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
message = folder.getMessages();
}
public void close() throws MessagingException {
if (folder != null) {
try {
folder.close(true);
} catch (Exception e) {
}
store.close();
}
}
public Iterator<Message> iterator() {
return new Iter();
}
private class Iter implements Iterator<Message> {
int currrent_msg_index=0;
Message curMsg;
public boolean hasNext() {
return currrent_msg_index <= message.length - 1;
}
public Message next() {
curMsg = message[currrent_msg_index++];
return curMsg;
}
public void remove() {
try {
curMsg.setFlag(Flags.Flag.DELETED, true);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
2. MailSetting.java
package zhang.stony.mail;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @author Stony Zhang
* @E-Mail stonyz@live.com
* @QQ 55279427
* @Createdate 2009-9-5
*
* @Copyright, everyone can use the code freely, but you must keep the author's information.
*/
public class MailSetting {
private String smtpHost;
private String pop3Host;
private String user ;
private String password;
public MailSetting(String configFile){
try {
InputStream in = new FileInputStream(configFile);
Properties prop = new Properties();
prop.load(in);
setPropertiesAttri(prop);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public MailSetting(Properties pr){
setPropertiesAttri(pr);
}
public MailSetting(String host,String u,String p){
this.smtpHost=host;
this.user=u;
this.password=p;
}
private void setPropertiesAttri(Properties prop) {
try {
// String conf = AppContest.getStartupPath() + "config/MailSetting.properties";
this.setSmtpHost(prop.getProperty("SmtpHost"));
this.setPop3Host(prop.getProperty("Pop3Host").toString());
this.setUser(prop.getProperty("User").toString());
this.setPassword(prop.getProperty("Password").toString());
} catch (Exception ex) {
System.err.println("ex1 in MailSetting:" + ex.toString());
}
}
public String getSmtpHost() {
return smtpHost;
}
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
}
public String getPop3Host() {
return pop3Host;
}
public void setPop3Host(String pop3Host) {
this.pop3Host = pop3Host;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
关于JavaMail发送邮件,请参阅 JavaMail发送html格式的邮件