1、第一个文件 MailSenderInfo.java
2、第二个文件 MyAuthenticator.java
-
import javax.mail.*;
-
-
public class MyAuthenticator extends Authenticator{
-
String userName=null;
-
String password=null;
-
-
public MyAuthenticator(){
-
}
-
public MyAuthenticator(String username, String password) {
-
this.userName = username;
-
this.password = password;
-
}
-
protected PasswordAuthentication getPasswordAuthentication(){
-
return new PasswordAuthentication(userName, password);
-
}
-
}
3、第三个文件 SimpleMailSender.java
-
import java.util.Date;
-
import java.util.Properties;
-
import javax.mail.Address;
-
import javax.mail.BodyPart;
-
import javax.mail.Message;
-
import javax.mail.Multipart;
-
import javax.mail.Session;
-
import javax.mail.Transport;
-
import javax.mail.internet.InternetAddress;
-
import javax.mail.internet.MimeBodyPart;
-
import javax.mail.internet.MimeMessage;
-
import javax.mail.internet.MimeMultipart;
-
-
/**
-
* 简单邮件(不带附件的邮件)发送器
-
*/
-
public class SimpleMailSender {
-
/**
-
* 以文本格式发送邮件
-
* @param mailInfo 待发送的邮件的信息
-
*/
-
public void sendTextMail(MailSenderInfo mailInfo) throws Exception{
-
MyAuthenticator authenticator = null;
-
Properties pro = mailInfo.getProperties();
-
if (mailInfo.isValidate()) {
-
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
-
}
-
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
-
Message mailMessage = new MimeMessage(sendMailSession);
-
Address from = new InternetAddress(mailInfo.getFromAddress());
-
mailMessage.setFrom(from);
-
Address to = new InternetAddress(mailInfo.getToAddress());
-
mailMessage.setRecipient(Message.RecipientType.TO,to);
-
mailMessage.setSubject(mailInfo.getSubject());
-
mailMessage.setSentDate(new Date());
-
String mailContent = mailInfo.getContent();
-
mailMessage.setText(mailContent);
-
Transport.send(mailMessage);
-
}
-
-
/**
-
* 以HTML格式发送邮件
-
* @param mailInfo 待发送的邮件信息
-
*/
-
public static void sendHtmlMail(MailSenderInfo mailInfo) throws Exception {
-
MyAuthenticator authenticator = null;
-
Properties pro = mailInfo.getProperties();
-
if (mailInfo.isValidate()) {
-
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
-
}
-
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
-
Message mailMessage = new MimeMessage(sendMailSession);
-
Address from = new InternetAddress(mailInfo.getFromAddress());
-
mailMessage.setFrom(from);
-
Address to = new InternetAddress(mailInfo.getToAddress());
-
mailMessage.setRecipient(Message.RecipientType.TO,to);
-
mailMessage.setSubject(mailInfo.getSubject());
-
mailMessage.setSentDate(new Date());
-
Multipart mainPart = new MimeMultipart();
-
BodyPart html = new MimeBodyPart();
-
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
-
mainPart.addBodyPart(html);
-
mailMessage.setContent(mainPart);
-
Transport.send(mailMessage);
-
}
-
}
4、测试Main Test.java
-
import org.apache.commons.logging.Log;
-
import org.apache.commons.logging.LogFactory;
-
-
public class Test {
-
-
/**
-
* 日志输出对象
-
*/
-
private static final Log log = LogFactory.getLog(Test.class);
-
-
/**
-
* 测试Main方法
-
* @param args
-
*/
-
public static void main(String [] args){
-
try {
-
sendEmail("test1@126.com","test2@126.com","邮件标题","邮件内容");
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* 邮件发送工具方法
-
* @param fromAddress
-
* @param toAddress
-
* @param content
-
* @throws Exception
-
*/
-
public static void sendEmail(String fromAddress, String toAddress,String title ,String content) throws Exception{
-
log.info("执行邮件发送(发送地址:"+fromAddress+ " 接收地址:" + toAddress +")");
-
log.debug("发送邮件标题:" + title + " 邮件内容:" + content);
-
SimpleMailSender sms = new SimpleMailSender();
-
MailSenderInfo mailInfo = new MailSenderInfo();
-
mailInfo.setMailServerHost("smtp.domain.com"); //邮件服务器地址
-
-
mailInfo.setMailServerPort("25"); //邮件服务器端口
-
-
mailInfo.setValidate(true); //是否进行用户名密码验证
-
-
mailInfo.setUserName("username"); //用户名
-
-
mailInfo.setPassword("passwd"); //密码
-
-
mailInfo.setFromAddress(fromAddress); //发送地址
-
-
mailInfo.setToAddress(toAddress); //接收地址
-
-
mailInfo.setSubject(title);
-
mailInfo.setContent(content);
-
sms.sendHtmlMail(mailInfo);
-
}
-
}
阅读(2280) | 评论(0) | 转发(1) |