324 lines
12 KiB
Java
324 lines
12 KiB
Java
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
package com.ifish.API;
|
|
|
|
import java.util.Date;
|
|
import java.util.Properties;
|
|
import javax.mail.BodyPart;
|
|
import javax.mail.Message;
|
|
import javax.mail.Multipart;
|
|
import javax.mail.Session;
|
|
import javax.mail.Transport;
|
|
import javax.mail.internet.InternetAddress;
|
|
import javax.mail.internet.MimeBodyPart;
|
|
import javax.mail.internet.MimeMessage;
|
|
import javax.mail.internet.MimeMultipart;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
/**
|
|
*
|
|
* @author Administrator
|
|
*/
|
|
public class Mail {
|
|
|
|
private MimeMessage mimeMsg; //MIME邮件对象
|
|
private Session session; //邮件会话对象
|
|
private Properties props; //系统属性
|
|
private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
|
|
private static boolean needAuth = true;//smtp是否需要认证
|
|
private static boolean isDebug = false;//是否开启调试
|
|
//smtp认证用户名和密码
|
|
private static String smtp = "smtp.qq.com";//SMTP服务器
|
|
private static String username = "service@ifish7.com";
|
|
private static String password = "Ifish72015";
|
|
private static String from = "service@ifish7.com";
|
|
//private static String to = "fun@ifish7.com";
|
|
private static String to = "help@ifish7.com";
|
|
private static Logger log = LoggerFactory.getLogger(Mail.class);
|
|
private static HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
private static String registerEmail = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/api" + request.getContextPath() + "/registerEmail?md5=";
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param smtp 邮件发送服务器
|
|
*/
|
|
public Mail(String smtp) {
|
|
setSmtpHost(smtp);
|
|
createMimeMessage();
|
|
}
|
|
|
|
/**
|
|
* 设置邮件发送服务器
|
|
*
|
|
* @param hostName String
|
|
*/
|
|
public void setSmtpHost(String hostName) {
|
|
if (props == null) {
|
|
props = System.getProperties(); //获得系统属性对象
|
|
}
|
|
//设置SMTP主机
|
|
props.put("mail.smtp.host", hostName);
|
|
//设置SMTP是否需要验证
|
|
props.put("mail.smtp.auth", needAuth);
|
|
props.put("mail.smtp.starttls.enable", needAuth);
|
|
}
|
|
|
|
/**
|
|
* 创建MIME邮件对象
|
|
*
|
|
* @return
|
|
*/
|
|
public boolean createMimeMessage() {
|
|
try {
|
|
session = Session.getDefaultInstance(props, null); //获得邮件会话对象
|
|
} catch (Exception e) {
|
|
log.error("获取邮件会话对象时发生错误!" + e.toString());
|
|
return false;
|
|
}
|
|
try {
|
|
mimeMsg = new MimeMessage(session); //创建MIME邮件对象
|
|
mp = new MimeMultipart();
|
|
return true;
|
|
} catch (Exception e) {
|
|
log.error("创建MIME邮件对象失败!" + e.toString());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置邮件主题
|
|
*
|
|
* @param mailSubject
|
|
* @return
|
|
*/
|
|
public boolean setSubject(String mailSubject) {
|
|
try {
|
|
mimeMsg.setSubject(mailSubject);
|
|
return true;
|
|
} catch (Exception e) {
|
|
log.error("设置邮件主题失败" + e.toString());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置邮件正文
|
|
*
|
|
* @param mailBody String
|
|
*/
|
|
public boolean setBody(String mailBody) {
|
|
try {
|
|
BodyPart bp = new MimeBodyPart();
|
|
bp.setContent(mailBody, "text/html;charset=GBK");
|
|
mp.addBodyPart(bp);
|
|
return true;
|
|
} catch (Exception e) {
|
|
log.error("设置邮件正文失败" + e.toString());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置发信人
|
|
*
|
|
* @param from String
|
|
*/
|
|
public boolean setFrom(String from) {
|
|
try {
|
|
mimeMsg.setFrom(new InternetAddress(from)); //设置发信人
|
|
return true;
|
|
} catch (Exception e) {
|
|
log.error("设置发信人失败" + e.toString());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置收信人
|
|
*
|
|
* @param to String
|
|
*/
|
|
public boolean setTo(String to) {
|
|
try {
|
|
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
|
|
return true;
|
|
} catch (Exception e) {
|
|
log.error("设置收信人失败" + e.toString());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 发送邮件
|
|
*/
|
|
public boolean sendOut() {
|
|
try {
|
|
mimeMsg.setContent(mp);
|
|
mimeMsg.saveChanges();
|
|
Session mailSession = Session.getInstance(props, null);
|
|
mailSession.setDebug(isDebug);
|
|
Transport transport = mailSession.getTransport("smtp");
|
|
transport.connect((String) props.get("mail.smtp.host"), username, password);
|
|
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
|
|
//transport.send(mimeMsg);
|
|
transport.close();
|
|
return true;
|
|
} catch (Exception e) {
|
|
log.error("发送邮件失败" + e.toString());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 调用sendOut方法完成邮件发送
|
|
*
|
|
* @param smtp
|
|
* @param from
|
|
* @param to
|
|
* @param subject
|
|
* @param content
|
|
* @param username
|
|
* @param password
|
|
* @return boolean
|
|
*/
|
|
public static boolean send(String subject, String content) {
|
|
Mail theMail = new Mail(smtp);
|
|
|
|
if (!theMail.setSubject(subject)) {
|
|
return false;
|
|
}
|
|
if (!theMail.setFrom(from)) {
|
|
return false;
|
|
}
|
|
if (!theMail.setTo(to)) {
|
|
return false;
|
|
}
|
|
if (!theMail.setBody(content)) {
|
|
return false;
|
|
}
|
|
if (!theMail.sendOut()) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 发送邮箱验证码
|
|
*
|
|
* @param email
|
|
* @return
|
|
*/
|
|
public static String sendRegisterCode(String email) {
|
|
Mail theMail = new Mail(smtp);
|
|
|
|
if (!theMail.setSubject("邮箱注册验证码")) {
|
|
return "";
|
|
}
|
|
if (!theMail.setFrom(from)) {
|
|
return "";
|
|
}
|
|
if (!theMail.setTo(to)) {
|
|
return "";
|
|
}
|
|
Integer code = (int) (Math.random() * 9000 + 1000);
|
|
if (!theMail.setBody(code + "")) {
|
|
return "";
|
|
}
|
|
if (!theMail.sendOut()) {
|
|
return "";
|
|
}
|
|
return code + "";
|
|
}
|
|
|
|
/**
|
|
* 发送邮箱注册链接
|
|
*
|
|
* @param email 发送目标邮箱
|
|
* @param value 发送有效时间验证
|
|
* @return
|
|
*/
|
|
public static boolean sendRegisterCodeTest(String email, String value) {
|
|
try {
|
|
// 1. 创建参数配置, 用于连接邮件服务器的参数配置
|
|
Properties props = new Properties(); // 参数配置
|
|
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
|
|
props.setProperty("mail.smtp.host", smtp); // 发件人的邮箱的 SMTP 服务器地址
|
|
props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
|
|
|
|
// PS: 某些邮箱服务器要求 SMTP 连接需要使用 SSL 安全认证 (为了提高安全性, 邮箱支持SSL连接, 也可以自己开启),
|
|
// 如果无法连接邮件服务器, 仔细查看控制台打印的 log, 如果有有类似 “连接失败, 要求 SSL 安全连接” 等错误,
|
|
// 打开下面 /* ... */ 之间的注释代码, 开启 SSL 安全连接。
|
|
/*
|
|
// SMTP 服务器的端口 (非 SSL 连接的端口一般默认为 25, 可以不添加, 如果开启了 SSL 连接,
|
|
// 需要改为对应邮箱的 SMTP 服务器的端口, 具体可查看对应邮箱服务的帮助,
|
|
// QQ邮箱的SMTP(SLL)端口为465或587, 其他邮箱自行去查看)
|
|
final String smtpPort = "465";
|
|
props.setProperty("mail.smtp.port", smtpPort);
|
|
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
|
props.setProperty("mail.smtp.socketFactory.fallback", "false");
|
|
props.setProperty("mail.smtp.socketFactory.port", smtpPort);
|
|
*/
|
|
// 2. 根据配置创建会话对象, 用于和邮件服务器交互
|
|
Session session = Session.getDefaultInstance(props);
|
|
session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log
|
|
// 3. 创建一封邮件
|
|
MimeMessage message = createMimeMessage(session, email, value);
|
|
// 4. 根据 Session 获取邮件传输对象
|
|
Transport transport = session.getTransport();
|
|
|
|
// 5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错
|
|
//
|
|
// PS_01: 成败的判断关键在此一句, 如果连接服务器失败, 都会在控制台输出相应失败原因的 log,
|
|
// 仔细查看失败原因, 有些邮箱服务器会返回错误码或查看错误类型的链接, 根据给出的错误
|
|
// 类型到对应邮件服务器的帮助网站上查看具体失败原因。
|
|
//
|
|
// PS_02: 连接失败的原因通常为以下几点, 仔细检查代码:
|
|
// (1) 邮箱没有开启 SMTP 服务;
|
|
// (2) 邮箱密码错误, 例如某些邮箱开启了独立密码;
|
|
// (3) 邮箱服务器要求必须要使用 SSL 安全连接;
|
|
// (4) 请求过于频繁或其他原因, 被邮件服务器拒绝服务;
|
|
// (5) 如果以上几点都确定无误, 到邮件服务器网站查找帮助。
|
|
//
|
|
// PS_03: 仔细看log, 认真看log, 看懂log, 错误原因都在log已说明。
|
|
transport.connect(username, password);
|
|
// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
|
|
transport.sendMessage(message, message.getAllRecipients());
|
|
// 7. 关闭连接
|
|
transport.close();
|
|
return true;
|
|
} catch (Exception e) {
|
|
String a = e.getMessage();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static MimeMessage createMimeMessage(Session session, String receiveMail, String value) throws Exception {
|
|
// 1. 创建一封邮件
|
|
MimeMessage message = new MimeMessage(session);
|
|
// 2. From: 发件人
|
|
message.setFrom(new InternetAddress(from, "爱鱼奇", "UTF-8"));
|
|
// 3. To: 收件人(可以增加多个收件人、抄送、密送)
|
|
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "XX用户", "UTF-8"));
|
|
// 4. Subject: 邮件主题
|
|
message.setSubject("注册链接", "UTF-8");
|
|
// 5. Content: 邮件正文(可以使用html标签)
|
|
String content = "您的注册链接为:" + registerEmail + value + " ,请在半小时内进行激活!";
|
|
message.setContent(content, "text/html;charset=UTF-8");
|
|
// 6. 设置发件时间
|
|
message.setSentDate(new Date());
|
|
// 7. 保存设置
|
|
message.saveChanges();
|
|
|
|
return message;
|
|
}
|
|
}
|