最近在写mail系统,在网上搜索了一些代码,整理如下,供大家参考
package cn.com.andy.mail;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
public class MailServer extends Authenticator{
public Session session;
protected PasswordAuthentication authentication;
public MailServer(String user, String host,String pass)
{
this(user,host,pass, true);
}
/**
*
* @param user
* @param host
* @param pass
* @param debug
*
* @ 【用户连接邮件服务器基本验证】
*/
public MailServer(String user,String host, String pass, boolean debug)
{
authentication = new PasswordAuthentication(user, pass);
Properties props = System.getProperties();
props.put("mail.user", user);
props.put("mail.host", host);
props.put("mail.password", pass);
props.put("mail.debug", debug ? "true" : "false");
props.put("mail.store.protocol", "imap");
props.put("mail.transport.protocol", "smtp");
session = Session.getInstance(props, this);
}
/**
* @功能 【没有认证的邮件发送,方便确认邮件发送】
* @param user
* @param host
*/
public MailClient(String user,String host)
{
Properties props = new Properties();
props.put("mail.user", user);
props.put("mail.host", host);
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
session = Session.getInstance(props, this);
}
public PasswordAuthentication getPasswordAuthentication()
{
return authentication;
}
}