【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
先引入插件mail-1.5.0-b01.jar 下载地址: https://mvnrepository.com/artifact/javax.mail/mail
========================================MailServer =====================================================
package com.mail;
import java.util.List;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage.RecipientType;
/**
* 简单邮件发送器 可单发,群发
* @author xc
*
*/
public class MailServer {
/**
* 发送邮件的props邮件
*/
private final transient Properties props=System.getProperties();
/**
* 邮件服务器登录验证
*/
private transient MailAuthenticator authenticator;
/**
* 邮箱session
*/
private transient Session session;
/**
* 初始化邮件发送器
* smtpHostName smtp邮件服务器地址
* username 发送邮件的用户名(地址)
* password 发送邮件的密码
*
*/
public MailServer(final String smtpHostName,final String smtpPort,final String username,
final String password){
init(username,password,smtpHostName,smtpPort);
}
/**
* 初始化
* username 发送邮件的用户名(地址)
* password 密码
* smtpHostName smtp主机地址
*
*/
private void init(String username,String password,String smtpHostName,String smtpPort){
//初始化props
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",smtpHostName);
if(smtpHostName==null)props.put("mail.smtp.host",smtpHostName);
props.put("mail.smtp.port",smtpPort);
//验证
authenticator = new MailAuthenticator(username,password);
//创建session
session = Session.getInstance(props,authenticator);
}
/**
* 发送邮件
*
* recipient 收件人邮箱地址
* subject 邮件主题
* content 邮件内容
*/
public void send(String recipient,String subject,Object content)
throws AddressException,MessagingException{
//创建mime类型邮件
final MimeMessage message = new MimeMessage(session);
//设置发信人
message.setFrom(new InternetAddress(authenticator.getUsername()));
//设置收件人
if(recipient!=null&&recipient.indexOf(";")!=-1){
//多收件人
String[] rec=recipient.split(";");
int len=rec.length;
InternetAddress[] iad=new InternetAddress[len];
for(int i=0;i<len;i++){
iad[i]=new InternetAddress(rec[i]);
}
message.setRecipients(RecipientType.TO,iad);
}else{
//单个收件人
message.setRecipient(RecipientType.TO,new InternetAddress(recipient));
}
//设置主题
message.setSubject(subject);
//设置邮件内容
message.setContent(content.toString(),"text/html;charset=utf-8");
//message.setText(content.toString(),"GBK");
//发送
Transport.send(message);
}
/**
* 群发邮件
* recipient 收件人邮箱地址
* subject 邮件主题
* content 邮件内容
*/
public void send(List<String> recipients,String subject,Object content)
throws AddressException,MessagingException{
//创建mime类型邮件
final MimeMessage message = new MimeMessage(session);
//设置发信人
message.setFrom(new InternetAddress(authenticator.getUsername()));
//设置收件人们
final int num=recipients.size();
InternetAddress[] addresses=new InternetAddress[num];
for (int i = 0; i < num; i++) {
addresses[i]=new InternetAddress(recipients.get(i));
}
message.setRecipients(RecipientType.TO,addresses);
//设置主题
message.setSubject(subject);
//设置邮件内容
message.setContent(content.toString(),"text/html;charset=utf-8");
//发送
Transport.send(message);
}
/**
* 服务器邮箱登录验证
*/
public class MailAuthenticator extends Authenticator{
/**
* 用户名(登录邮箱)
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 初始化邮箱和密码
* username 邮箱
* password 密码
*/
public MailAuthenticator(String username,String password){
this.username=username;
this.password=password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
}
===================================MailSender ===========================================
package com.mail;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
public class MailSender {
/**
* 服务邮箱
*/
private static MailServer mailServer=null;
private static String userName;
private static String password;
private static String stmp;
private static String port;
public void setPassword(String password){
if(MailSender.password==null){
MailSender.password=password;
}
}
public void setStmp(String stmp){
if(MailSender.stmp==null){
MailSender.stmp=stmp;
}
}
/**
* 使用默认的用户名和密码发送邮件
*/
public static void sendHtml(String recipient,String subject,Object content)
throws AddressException,MessagingException{
if(mailServer==null){
mailServer=new MailServer(stmp,port,userName,password);
}
mailServer.send(recipient,subject,content);
}
/**
* 使用指定的用户名和密码发送邮件
*/
public static void sendHtml(String stmpIp,String port,String userName,String password,String recipient,
String subject,Object content)throws AddressException,MessagingException{
new MailServer(stmpIp,port,userName,password).send(recipient,subject,content);
}
public static void main(String[] args) {
try {
String mailBody="发送邮件测试";
sendHtml("本机ip","端口(25)","发送人邮箱","发送人邮箱密码","收件人邮箱","标题",mailBody);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
来源:oschina
链接:https://my.oschina.net/u/3114501/blog/3147185