package com.cn.mail;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
/**
* 发送邮件工具类 Created by RX on 2020/04/3.
*/
public class MailTo {
/**
* 发送邮件到自己的126邮箱
*
* @param title
* 需要传输的标题
* @param body
* 需要传输的内容
* @return
*/
public static boolean sendMail126(String title, String body) {
// 设置参数
Properties props = new Properties();
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.126.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", 465);
// 自定义信息
props.put("username", "Renxingforever@126.com");// 你的邮箱
props.put("password", "OCWLULNDEDTJJSLY");// 你的密码(此处密码为POP3/SMTP/IMAP授权密码)
props.put("to", "767023158@qq.com");// 接收的邮箱
return MailTo.send(props, title, body);
}
/**
* 发送邮件到gmail 国内网络无法访问(因为众所周知的原因)
*
* @param title
* 标题
* @param body
* 内容
* @return
*/
public static boolean sendMailGmail(String title, String body) {
// 设置参数
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// 自定义信息
props.put("username", "Renxingforever@163.com");// 你的邮箱
props.put("password", "renxing19920719");// 你的密码
props.put("to", "767023158@qq.com");// 接收的邮箱
return MailTo.send(props, title, body);
}
/**
* 发送邮件到outlook
*
* @param title
* 标题
* @param body
* 内容
* @return
*/
public static boolean sendMailOutLook(String title, String body) {
// 设置参数
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.outlook.com");
props.put("mail.smtp.port", "587");
// 自定义信息
props.put("username", "Renxingforever@163.com");// 你的邮箱
props.put("password", "renxing19920719");// 你的密码
props.put("to", "767023158@qq.com");// 接收的邮箱
return MailTo.send(props, title, body);
}
/**
* 获取系统当前的时间 以传入时间格式返回,传空返回默认格式
*
* @param format
* 时间格式
* @return
*/
private static String getTitleTimeFormat(String format) {
if (format == null) {
format = "yyyy-MM-dd HH:mm:ss/SSS";
}
SimpleDateFormat df = new SimpleDateFormat(format);// 设置日期格式
return df.format(new Date());// new Date()为获取当前系统时间
}
/**
* 发送邮件,获取参数,和标题还有内容
*
* @param props
* 参数
* @param title
* 标题
* @param body
* 内容
* @return
*/
private static Boolean send(Properties props, String title, String body) {
// 发送邮件地址
final String username = props.getProperty("username");
// 发送邮件名称
final String password = props.getProperty("password");
// 接收邮件地址
String to = props.getProperty("to");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(title + "(" + MailTo.getTitleTimeFormat(null) + ")");
message.setContent(body, "text/html;charset=utf-8");
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("发送完毕!");
return true;
}
}
|