需要导入javax.mail.jar
以qq邮箱发送到qq邮箱
Mail.jar
package com.lu.mail;
public class Mail {
/**
* 邮件标题
*/
private String subject;
/**
* 邮件内容
*/
private String content;
public Mail(){
}
public Mail(String subject, String content) {
this.subject = subject;
this.content = content;
}
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;
}
}
MailAuthenticator.jar 继承Authenticator
package com.lu.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* 服务器邮箱
*/
public class MailAuthenticator extends Authenticator{
/**
* 用户名
*/
private String username;
/**
* 登录密码
*/
private String password;
/**
* 继承Authenticator类需重写此方法
*/
@Override
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
public MailAuthenticator(){
}
public MailAuthenticator(String username, String password){
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
MailSender.java
package com.lu.mail;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
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;
import javax.mail.internet.MimeMessage.RecipientType;
import com.lu.mail.Mail;
import com.lu.mail.MailAuthenticator;
/**
* 邮件发送器:可单发,可群发
*/
public class MailSender {
/**
* 发送邮件的Properties文件
*/
private static Properties props;
static{
props =new Properties();
props.put("mail.smtp.host", "smtp.exmail.qq.com");
props.put("mail.smtp.port", "465");
}
/**
* 服务器邮箱登录验证
*/
private transient MailAuthenticator mailAuthenticator;
/**
* 邮箱Session
*/
private transient Session session;
/**
* 初始化邮箱发送器
*
* @param mailServiceUrl
* 服务器邮箱地址
* @param username
* 服务器邮箱用户名
* @param password
* 服务器邮箱登录密码
*/
public MailSender(final String mailServiceUrl,final String username, final String password){
init(mailServiceUrl, username, password);
}
/**
* 初始化邮箱发送器
*
* @param username
* 服务器邮箱用户名
* @param password
* 服务器邮箱登录密码
*/
public MailSender(final String username, final String password){
//通过邮箱地址解析smtp服务器,对大多数邮箱都管用
final String mailServiceUrl = "smtp.exmail." + username.split("@")[1];
init(mailServiceUrl, username, password);
}
/**
* 初始化操作
*
* @param mailServiceUrl
* 服务器邮箱地址
* @param username
* 服务器邮箱用户名
* @param password
* 服务器邮箱登录密码
*/
public void init(String mailServiceUrl, String username, String password){
//初始化props
props.put("mail.smtp.host", mailServiceUrl);
props.put("mail.smtp.auth", "true");
//服务器邮箱验证
mailAuthenticator = new MailAuthenticator(username, password);
//创建session
session = Session.getInstance(props, mailAuthenticator);
}
/**
* 发送邮件
*
* @param recipient
* 收信人邮箱地址
* @param subject
* 邮件标题
* @param content
* 邮件内容
* @throws AddressException
* @throws MessagingException
*/
public void send(String recipient, String subject, String content) throws AddressException, MessagingException{
//创建mime类型邮件
final MimeMessage msg = new MimeMessage(session);
//设置发信人
msg.setFrom(new InternetAddress(mailAuthenticator.getUsername()));
//设置收信人
msg.setRecipient(RecipientType.TO, new InternetAddress(recipient));
//设置邮件标题
msg.setSubject(subject);
//设置邮件内容
msg.setContent(content, "text/html;charset=utf-8");
//发送邮件
Transport.send(msg);
}
/**
* 群发邮件
*
* @param recipients
* 收信人们的邮箱地址
* @param subject
* 邮件标题
* @param content
* 邮件内容
* @throws AddressException
* @throws MessagingException
*/
public void send(List<String> recipients, String subject, String content) throws AddressException, MessagingException{
//创建mime类型邮件
final MimeMessage msg = new MimeMessage(session);
//设置发信人
msg.setFrom(new InternetAddress(mailAuthenticator.getUsername()));
//设置收信人们
int num = recipients.size();
InternetAddress[] addresses = new InternetAddress[num];
for (int i = 0; i < num; i++) {
addresses[i] = new InternetAddress(recipients.get(i));
}
msg.setRecipients(RecipientType.TO, addresses);
//设置邮件标题
msg.setSubject(subject);
//设置邮件内容
msg.setContent(content, "text/html;charset=utf-8");
Transport.send(msg);
}
/**
* 发送邮件
*
* @param recipient
* 收信人邮箱地址
* @param mail
* 邮件对象
* @throws AddressException
* @throws MessagingException
* @throws
*/
public void send(String recipient, Mail mail) throws AddressException, MessagingException{
this.send(recipient, mail.getSubject(), mail.getContent());
}
/**
* 群发邮件
*
* @param recipients
* 收信人们的邮箱地址
* @param mail
* 邮件对象
* @throws AddressException
* @throws MessagingException
* @throws
*/
public void send(List<String> recipients, Mail mail) throws AddressException, MessagingException{
this.send(recipients, mail.getSubject(), mail.getContent());
}
}
controller 控制器
//获取jsp发送来的邮箱检查后发送邮箱
@RequestMapping("/getEmail.do")
protected String sendTheMail(String teacher_email) throws Exception
{
Teacher teacher=passwordMapper.findEmail(teacher_email);//检查是否有该邮箱
if(teacher!=null){
//发件人邮箱地址:这里填写要发送出去的邮箱地址即可
String username = "xx@qq.com";
//发件人邮箱登录密码:这里填写要发送出去的邮箱的登录密码即可
String password = "*********";
//创建邮箱发送器
MailSender mailSender = new MailSender(username,password);
//邮箱对象
Mail mail = new Mail("主题","发送内容");
//收件人邮箱地址:这里填写收件的邮箱的地址即可
String recipient = teacher_email;
try {
mailSender.send(recipient, mail);
} catch (AddressException e) {
System.out.println("发信人邮箱路径不正确........");
e.printStackTrace();
} catch (MessagingException e) {
System.out.println("邮件发送失败........");
e.printStackTrace();
return "forget/error";
}
System.out.println("发送成功........");
return "forget/success";
}else{
return "forget/error";
}
}