67,512
社区成员
发帖
与我相关
我的任务
分享
import java.util.Date;
import java.util.Properties;
import javax.mail.AuthenticationFailedException;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.mail.smtp.SMTPTransport;
public final class EmailHandle {
//log处理
protected static Log log = LogFactory.getLog(EmailHandle.class);
private String serverIP = ""; //邮件服务器IP
private String serverPort = ""; //邮件服务器端口
private String user = ""; //邮件服务器用户
private String password = ""; //邮件服务器密码
private boolean ssl = false;
private Session session =null;
public EmailHandle(String serverIp,String serverPort,String user,String password,boolean ssl) {
this.serverIP = serverIp;
this.serverPort = serverPort;
this.user = user;
this.password = password;
this.ssl = ssl;
}
/**
* 开始邮件会话
*/
public void beginMailSession() {
if (session==null) {
Properties props = System.getProperties();
props.put("mail.smtp.host", serverIP);
props.put("mail.smtp.port", serverPort);
props.put("mail.smtp.auth", "true");
session = Session.getInstance(props, new MyAuthenticator(user,password));
}
}
/**
* 发送文本邮件
*
* @param to
* @param subject
* @param from
* @param cc //可空
* @param bcc //可空
* @param body
* @throws AddressException
* @throws MessagingException
*/
public void sentTextMail(String to,String subject,String from,String cc,String bcc,String body) throws AddressException, MessagingException {
if (session!=null) {
Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));
else
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null)
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(bcc, false));
msg.setSubject(subject);
msg.setText(body);
msg.setHeader("X-Mailer", "邮件头");
msg.setSentDate(new Date());
SMTPTransport t =
(SMTPTransport)session.getTransport(ssl ? "smtps" : "smtp");
try {
if (user!=null && password!=null)
t.connect(serverIP, user, password);
else
t.connect();
t.sendMessage(msg, msg.getAllRecipients());
}catch(AuthenticationFailedException e){
log.error(e, e.getCause());
}
catch(MessagingException e){
log.error(e, e.getCause());
}
finally {
t.close();
}
}
}
/**
* 邮件授权认证类
*
*/
private class MyAuthenticator extends javax.mail.Authenticator{
private String userName;
private String password;
public MyAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName,password);
}
}
}
EmailHandle handle = new EmailHandle(mailServerIp,mailServerPort,mailUser,mailPassword,false);
handle.beginMailSession();
handle.sentTextMail(email1, "邮件主题", mailUser,null,null,"其他内容");