package com.nq.utils.email;
|
|
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpResponse;
|
import org.apache.http.NameValuePair;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.util.EntityUtils;
|
import org.springframework.mail.SimpleMailMessage;
|
import org.springframework.mail.javamail.JavaMailSender;
|
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
import javax.mail.*;
|
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.MimeMessage;
|
import java.io.IOException;
|
import java.security.SecureRandom;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Properties;
|
|
/**
|
* @program: dabaogp
|
* @description:
|
* @create: 2025-09-16 13:44
|
**/
|
public class GmailSender {
|
|
private static final String API_URL = "https://www.aoksend.com/index/api/send_email";
|
|
// public static void main(String[] args) {
|
// aokSend("9e0y5ycs8@tempmail.cn","verification code","33456");
|
// }
|
public static void aokSend(String to, String subject, String body) {
|
// 创建一个CloseableHttpClient实例
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
// 创建一个HttpPost实例
|
HttpPost httpPost = new HttpPost(API_URL);
|
// 创建一个参数列表
|
List<NameValuePair> params = new ArrayList<>();
|
params.add(new BasicNameValuePair("app_key", "d7b800835c593a4c393d85521b72d6f3"));
|
params.add(new BasicNameValuePair("template_id", "E_133166271351"));
|
params.add(new BasicNameValuePair("to", to));
|
params.add(new BasicNameValuePair("reply_to", ""));
|
params.add(new BasicNameValuePair("alias", "Stock"));
|
params.add(new BasicNameValuePair("data", "{\"code\":"+body+"}"));
|
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
|
httpPost.setEntity(formEntity);
|
// 发送请求并获取响应
|
HttpResponse response = httpClient.execute(httpPost);
|
// 读取响应内容
|
HttpEntity responseEntity = response.getEntity();
|
if (responseEntity != null) {
|
String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
|
System.out.println("Response: " + responseBody);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
public static void sendEmailAli(String to, String subject, String body) {
|
// 初始化邮件发送器
|
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
|
// 设置邮箱服务器配置
|
mailSender.setHost("smtp.mxhichina.com");
|
mailSender.setPort(465);
|
mailSender.setUsername("ipcptvzpd@candyyun.com");
|
mailSender.setPassword("hdj$KDL#994");
|
mailSender.setProtocol("smtp");
|
mailSender.setDefaultEncoding("UTF-8");
|
|
// 设置JavaMail属性
|
Properties props = mailSender.getJavaMailProperties();
|
props.put("mail.smtp.auth", "true");
|
props.put("mail.smtp.ssl.enable", "true");
|
// 移除重复的socketFactory配置,避免冲突
|
|
try {
|
SimpleMailMessage message = new SimpleMailMessage();
|
message.setFrom("ipcptvzpd@candyyun.com");
|
message.setTo(to);
|
message.setSubject(subject);
|
message.setText(body);
|
|
mailSender.send(message);
|
System.out.println("验证码邮件发送成功!");
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new RuntimeException("邮件发送失败", e);
|
}
|
}
|
// public static void sendEmailAli(String to, String subject, String body) {
|
// // 初始化邮件发送器
|
// JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
//
|
// // 设置邮箱服务器配置
|
// mailSender.setHost("smtp.mxhichina.com"); // 阿里企业邮箱SMTP服务器
|
// mailSender.setPort(465); // SSL端口
|
// mailSender.setUsername("ipcptvzpd@candyyun.com"); // 发件邮箱
|
// mailSender.setPassword("g83r022ll6"); // 邮箱密码或授权码
|
// mailSender.setProtocol("smtp");
|
// mailSender.setDefaultEncoding("UTF-8");
|
//
|
// // 设置JavaMail属性
|
// Properties props = mailSender.getJavaMailProperties();
|
// props.put("mail.smtp.auth", "true");
|
// props.put("mail.smtp.ssl.enable", "true");
|
// props.put("mail.smtp.socketFactory.port", "465");
|
// props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
// props.put("mail.smtp.socketFactory.fallback", "false");
|
//
|
// try {
|
// SimpleMailMessage message = new SimpleMailMessage();
|
// message.setFrom("ipcptvzpd@candyyun.com"); // 发件人
|
// message.setTo(to);
|
// message.setSubject(subject);
|
// message.setText(body);
|
//
|
// mailSender.send(message);
|
// System.out.println("验证码邮件发送成功!");
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// throw new RuntimeException("邮件发送失败", e);
|
// }
|
// }
|
|
|
|
public static void sendEmail(String to, String subject, String body) {
|
// 发件人Gmail地址
|
String from = "coinzne.com@gmail.com";
|
// 使用应用专用密码,而不是常规的Gmail密码
|
String password = "pqupwyxoqedhxlfq";
|
|
// 设置邮件服务器属性
|
Properties props = new Properties();
|
props.put("mail.smtp.host", "smtp.gmail.com");
|
props.put("mail.smtp.port", "587");
|
props.put("mail.smtp.auth", "true");
|
props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密
|
|
// 获取Session对象
|
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
|
protected PasswordAuthentication getPasswordAuthentication() {
|
return new PasswordAuthentication(from, password);
|
}
|
});
|
|
try {
|
// 创建MimeMessage对象
|
Message message = new MimeMessage(session);
|
|
// 设置发件人
|
message.setFrom(new InternetAddress(from));
|
|
// 设置收件人
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
|
|
// 设置邮件主题
|
message.setSubject(subject);
|
|
// 设置邮件正文
|
message.setText(body);
|
|
// 发送邮件
|
Transport.send(message);
|
|
System.out.println("邮件发送成功!");
|
|
} catch (MessagingException e) {
|
throw new RuntimeException("邮件发送失败: " + e.getMessage(), e);
|
}
|
}
|
|
/**
|
* 生成6位随机数字验证码(使用安全随机数生成器)
|
* @return 6位数字字符串
|
*/
|
public static String generateSecureSixDigitCode() {
|
SecureRandom random = new SecureRandom();
|
int code = random.nextInt(900000) + 100000;
|
return String.valueOf(code);
|
}
|
}
|