JavaMail应用
在日常开发中,可能会引用到发邮件功能,例如在持续集成中,自动化测试运行完毕,自动将测试结果以报表的形式发送邮件给相关人。那么在Java中如何实现发邮件呢?
在java EE 中有个JavaMail插件 是专门解决java收发邮件功能的,下载地址:
http://www.oracle.com/technetwork/java/index-138643.html
下载后我们解压压缩包,提取里面的mail.jar,引入到工程中,便可以使用JavaMail API 进行发送邮件了。
下面是我根据JavaMail封装的用于发送邮件的方法,使用简单方便,只要填写SMTP 服务器地址,端口,用户名,密码,发送内容,发送人地址,收件人地址即可发送邮件
package net.hk515.utils;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
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 java.util.ArrayList;
import com.sun.mail.smtp.*;
public class SendMail {
private Log log=new Log(SendMail.class);
public static void main(String[] args) {
// TODO 自动生成的方法存根
SendMail sendMail=new SendMail();
sendMail.sendmessage("zhengshuheng@XXX.com", "XXX", "smtp.mxhichina.com", "25", "zhengshuheng@XXX.com", "XXX@XX.com,XXX@XX.com", "测试邮件发送函数", "测试邮件发送函数");
}
/**
*
* @param userName 发送邮箱账号 xxx@xxx.com形式
* @param passWord 发送邮件密码
* @param smtpHost stmp服务器地址
* @param smtpPort smtp服务器端口
* @param from 发件人地址
* @param tos 收件人地址
* @param title 标题
* @param content 内容
*/
public void sendmessage(String userName,String passWord,String smtpHost,String smtpPort,String from,String tos ,String title,String content)
{
Properties smtpProper=setSmtp(smtpHost, smtpPort, userName, passWord);
Auth authenticator=new Auth(userName, passWord);
try {
//创建session实例
Session session=Session.getInstance(smtpProper, authenticator);
MimeMessage message=new MimeMessage(session);
session.setDebug(true);
//设置from发件人邮箱地址
message.setFrom(new InternetAddress(from));
//收件人to LIST
ArrayList<Address> toList=new ArrayList<Address>();
//收件人字符串通过,号分隔收件人
String[] toArray=tos.split(",");
for (int i = 0; i < toArray.length; i++)
{
String str = toArray[i];
toList.add(new InternetAddress(str));
}
//将收件人列表转换为收件人数组
Address[] addresses=new Address[toList.size()];
addresses=toList.toArray(addresses);
//设置to收件人地址
message.setRecipients(MimeMessage.RecipientType.TO,addresses);
//设置邮件标题
message.setSubject(title);
//设置邮件内容
message.setContent(content, "text/html;charset=UTF-8");
//Transport.send(message);
Transport transport=session.getTransport();
transport.connect(smtpHost,userName, passWord);
transport.sendMessage(message,addresses);
log.info("发送邮件成功!");
} catch (Exception e) {
// TODO: handle exception
log.error("发送邮件失败!");
e.printStackTrace();
}
}
private Properties setSmtp(String smtpHost,String smtpPort,String userName,String passWord)
{
//创建邮件配置文件
Properties maiProperties=new Properties();
//配置smtp发件服务器
maiProperties.put("mail.transport.protocol", "smtp");
//配置smtp服务器
maiProperties.put("mail.smtp.host", smtpHost);
//配置smtp服务器端口
maiProperties.put("mail.smtp.port", smtpPort);
//配置smtp用户名
maiProperties.put("mail.user", userName);
//配置smtp身份验证
maiProperties.put("mail.smtp.auth", "true");
return maiProperties;
}
}
//smtp 身份验证类
class Auth extends Authenticator
{
Properties pwdProperties;
//构造函数
public Auth(String userName,String passWord)
{
pwdProperties=new Properties();
try {
pwdProperties.put(userName,passWord);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//必须实现 PasswordAuthentication 方法
public PasswordAuthentication passwordAuthentication()
{
String userName=getDefaultUserName();
//当前用户在密码表里
if (pwdProperties.containsKey(userName)) {
//取出密码,封装好后返回
return new PasswordAuthentication(userName, pwdProperties.getProperty(userName).toString());
}
else {
//如果当前用户不在密码表里就返回Null
System.out.println("当前用户不存在");
return null;
}
}
}