这里使用maven项目进行开发
1.首先pom中加载jar
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
</dependency>
2.定义3个类来辅助开发:
第一个类:MailServerConfig:用于定义发送邮件的基本信息
public class MailServerConfig {
private String mailServerUrl;
private String mailServerPort;
private String mailAccount;
private String password;
private String sender;
private Boolean proxySet;
private String proxyType;
private String proxyHost;
private String proxyPort;
}
第二个类SimpleAuthenticator:用于定义验证发送邮件的账号密码:
public class SimpleAuthenticator extends Authenticator {
String userName = null;
String password = null;
public SimpleAuthenticator() {
}
public SimpleAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
第三个类MailServerConfig用于使用代理的情况
public class MailServerConfig {
private String mailServerUrl;
private String mailServerPort;
private String mailAccount;
private String password;
private String sender;
private Boolean proxySet;
private String proxyType;
private String proxyHost;
private String proxyPort;
}
3.实现发送逻辑类:
public class SimpleMailSender {
/**
* 以HTML格式发送邮件
*
* @param mailInfo
* 待发送的邮件信息
*/
public boolean sendHtmlMail(MailSendInfo mailInfo) throws MessagingException{
// 判断是否需要身份认证
SimpleAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
// 代理设置
// pro.setProperty("proxySet", "true");
// pro.setProperty("socksProxyHost", "代理地址");
// pro.setProperty("socksProxyPort", "80");
if (mailInfo.getProxySet()) {
if (mailInfo.getProxyType().equals("socks")) {
pro.setProperty("proxySet", "true");
pro.setProperty("socksProxyHost", mailInfo.getProxyHost());
pro.setProperty("socksProxyPort", mailInfo.getProxyPort());
}
}
// 如果需要身份认证,则创建一个密码验证器
if (mailInfo.isValidate()) {
authenticator = new SimpleAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
System.out.println("mailContent:" + mailInfo.getContent());
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
mailMessage.saveChanges();
// 发送邮件
System.out.println("要发送邮件了!");
Transport.send(mailMessage);
System.out.println("邮件发完了!");
return true;
}
}
4.测试类test:
@Test
public void test(){
MailServerConfig mailServerConfig = new MailServerConfig();
mailServerConfig.setMailServerUrl("pop3邮件服务器地址");
mailServerConfig.setMailServerPort("pop3端口");
mailServerConfig.setMailAccount("邮件账号");
mailServerConfig.setPassword("发送邮件密码");
mailServerConfig.setSender("用户名");
mailServerConfig.setProxySet(false);
mailServerConfig.setProxyType("socks");
mailServerConfig.setProxyHost("代理地址");
mailServerConfig.setProxySet(false);
mailServerConfig.setProxyPort("80");
try {
setEmail(mailServerConfig, "接收邮箱地址", "邮件的主题名",
"邮件内容");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void setEmail(MailServerConfig mailServerConfig, String receiverEmail, String subject, String content)
throws MessagingException {
String mailServerHost = mailServerConfig.getMailServerUrl();
String mailServerPort = mailServerConfig.getMailServerPort();
String fromAddress = mailServerConfig.getMailAccount();
String userName = mailServerConfig.getSender();
String password = mailServerConfig.getPassword();
MailSendInfo mailSendInfo = new MailSendInfo();
mailSendInfo.setMailServerHost(mailServerHost);
mailSendInfo.setMailServerPort(mailServerPort);
mailSendInfo.setFromAddress(fromAddress);
mailSendInfo.setUserName(userName);
mailSendInfo.setPassword(password);
mailSendInfo.setToAddress(receiverEmail);
mailSendInfo.setValidate(true);
mailSendInfo.setSubject(subject);
mailSendInfo.setContent(content);
mailSendInfo.setProxySet(mailServerConfig.getProxySet());
mailSendInfo.setProxyType(mailServerConfig.getProxyType());
mailSendInfo.setProxyHost(mailServerConfig.getProxyHost());
mailSendInfo.setProxyPort(mailServerConfig.getProxyPort());
SimpleMailSender simpleMailSender = new SimpleMailSender();
System.out.println("邮件信息准备完毕,simpleMailSender开始发送邮件");
try {
simpleMailSender.sendHtmlMail(mailSendInfo);
} catch (javax.mail.MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("simpleMailSender邮件发送完毕");
}
这个只是简单的测试用例!