| | |
| | | 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; |
| | | |
| | | /** |
| | |
| | | **/ |
| | | public class GmailSender { |
| | | |
| | | private static final String API_URL = "https://www.aoksend.com/index/api/send_email"; |
| | | |
| | | 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", "")); |
| | | params.add(new BasicNameValuePair("template_id", "")); |
| | | params.add(new BasicNameValuePair("to", "")); |
| | | params.add(new BasicNameValuePair("reply_to", "")); |
| | | params.add(new BasicNameValuePair("alias", "")); |
| | | params.add(new BasicNameValuePair("data", "{\"name\":\"张三\",\"address\":\"深圳\"}")); |
| | | 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"; |