一.下载jar包
二.实现步骤
1、自定义UserAuthenticator继承抽象类Authenticator,用于用户鉴权
2、设置SMTP服务器地址、端口等信息
3、创建邮件会话,即登录邮箱
4、创建邮件消息,即编写邮件内容
5、发送邮件
三.实现代码
写一个邮件内容的是实体类
package com.xfz.util;
import java.util.Properties;
public class MailSenderInfo {
//发送邮件的服务器的IP和端口
private String mailServerHost;
private String mailServerPort="25";
//邮件发送的地址
private String formAddress;
//邮件接收者的地址
private String toAddress;
//登陆邮件发送服务器的用户名和密码
private String userName;
private String passWord;
//是否需要身份验证
private boolean validate=false;
//邮件主题
private String subject;
//邮件的文本内容
private String content;
//邮件附件的文件名
private String[] attachFileNames;
/**
* @return
* 获得邮件会话属性
*/
public Properties getProperties() {
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public String getFormAddress() {
return formAddress;
}
public void setFormAddress(String formAddress) {
this.formAddress = formAddress;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
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;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] attachFileNames) {
this.attachFileNames = attachFileNames;
}
}
发送邮件时的身份验证器
package com.xfz.util;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
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);
}
}
邮件发送器
package com.xfz.util;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author mailinfo 待发送邮件的信息
* 以文本格式发送邮件
*/
public class SimpleMailSender {
public boolean sendTextMail(MailSenderInfo mailinfo) {
//判断是否需要身份验证
MyAuthenticator authenticator =null;
Properties pro=mailinfo.getProperties();
if(mailinfo.isValidate()) {
//如果需要身份验证,则创建一个密码验证器
authenticator=new MyAuthenticator(mailinfo.getUserName(), mailinfo.getPassWord());
}
//根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession=Session.getDefaultInstance(pro, authenticator);
try {
//根据session创建一个邮件消息
Message mailMessage=new MimeMessage(sendMailSession);
//创建邮件发送者的地址
Address from=new InternetAddress(mailinfo.getFormAddress());
//设置邮件消息的发送者
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);
return true;
} catch (MessagingException e) {
e.printStackTrace();
}
return false;
}
public void send() {
//这个类主要是设置邮件
MailSenderInfo mailInfo=new MailSenderInfo();
//获取IP和端口
mailInfo.setMailServerHost("smtp.qq.com");
mailInfo.setMailServerPort("465");
//是否进行身份验证
mailInfo.setValidate(true);
//获取邮箱用户名和密码
mailInfo.setUserName("2320747615@qq.com");
mailInfo.setPassWord("密码");
//获取邮箱地址
mailInfo.setFormAddress("2320747615@qq.com");
//获取接收者的邮箱地址
mailInfo.setToAddress("2644598588@qq.com");
mailInfo.setSubject("主题");
//内容
String body="今天天气真好!词穷";
//这个类主要用来发送邮件
SimpleMailSender sms=new SimpleMailSender();
//发送文体格式
sms.sendTextMail(mailInfo);
}
public static void main(String[] args) {
}
}