Files
alipayDemo/src/sc545/pay/utils/MailUtils.java
2022-09-13 16:43:11 +08:00

86 lines
3.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package sc545.pay.utils;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailUtils {
private static final String USER = "scboxs@163.com"; // 发件人称号,同邮箱地址
private static final String HOST = "smtp.163.com"; // SMTP服务器地址
private static final String PASSWORD = "FUTSRKZXNDRSFXUW"; // 如果是qq邮箱可以使户端授权码或者登录密码
public static void main(String[] args) {
boolean t = sendMail("1782158860@qq.com", "这是一个测试邮件", "【顺诚百宝箱】测试邮件");
System.out.println(t);
}
/**
*
* @param to 收件人邮箱
* @param text 邮件正文
* @param title 标题
*/
/* 发送验证信息的邮件 */
public static boolean sendMail(String to, String text, String title){
try {
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", HOST);
// 发件人的账号
props.put("mail.user", USER);
//发件人的密码
props.put("mail.password", PASSWORD);
// 构建授权信息用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
String username = props.getProperty("mail.user");
InternetAddress form = new InternetAddress(username);
message.setFrom(form);
message.setDescription("欢迎关注微信公众号【顺诚百宝箱】\n全网VIP视频、聚合全网小说音乐等APP破解软件游戏大额优惠券福利更多功能等你发现");
// 设置收件人
InternetAddress toAddress = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, toAddress);
// 设置邮件标题
message.setSubject(title);
// 设置邮件的内容体
message.setContent(text, "text/html;charset=UTF-8");
// 发送邮件
Transport transport = mailSession.getTransport("smtp");
//设置授权码
transport.connect(username,"授权码");
transport.sendMessage(message,message.getAllRecipients());
transport.close();
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
}