当前位置:我的异常网» 编程 » JAVA运用IMAP、POP3、SMTP协议收发邮件
JAVA运用IMAP、POP3、SMTP协议收发邮件
www.myexceptions.net 网友分享于:2013-11-08 浏览:645次
JAVA使用IMAP、POP3、SMTP协议收发邮件
package com.gatgets.mail;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
/**
* 邮件工具类
* 需要导入mail.jar包(j2ee自带)
* @author 王正镇
* @date 2011-8-14
*/
public class EmailUtil {
private String username = null; // 邮箱用户名
private String password = null; // 邮箱密码
/**
* 传入自己的邮箱用户名和密码
* @param username
* @param password
*/
public EmailUtil(String username, String password) {
this.username = username;
this.password = password;
}
/**
* 创建连接会话
* @param props
* @return
*/
public Session createSession(Properties props) {
Session session = Session.getInstance(props, new Authenticator() {
// 返回用户名密码认证
@Override
public PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication(username, password);
ret