| | |
| | | package com.yami.trading.service.impl; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | |
| | | import javax.mail.MessagingException; |
| | | import javax.mail.internet.MimeMessage; |
| | | import javax.mail.internet.MimeUtility; |
| | | |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.yami.trading.common.manager.email.EmailMessage; |
| | | import com.yami.trading.common.util.ApplicationUtil; |
| | | import com.yami.trading.service.InternalEmailSenderService; |
| | | import freemarker.template.Template; |
| | | import freemarker.template.TemplateException; |
| | | import okhttp3.*; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.InitializingBean; |
| | | import org.springframework.core.io.FileSystemResource; |
| | | import org.springframework.mail.SimpleMailMessage; |
| | | import org.springframework.mail.javamail.JavaMailSenderImpl; |
| | | import org.springframework.mail.javamail.MimeMessageHelper; |
| | | import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; |
| | | import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; |
| | | |
| | | import com.yami.trading.common.manager.email.EmailMessage; |
| | | import com.yami.trading.common.util.ApplicationUtil; |
| | | import com.yami.trading.common.util.StringUtils; |
| | | import com.yami.trading.service.InternalEmailSenderService; |
| | | |
| | | import freemarker.template.Template; |
| | | import freemarker.template.TemplateException; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | |
| | | |
| | | public class InternalEmailSenderServiceImpl implements InternalEmailSenderService, InitializingBean { |
| | |
| | | mailSender = new JavaMailSenderImpl(); |
| | | mailSender.setUsername(ApplicationUtil.getProperty("email.username")); |
| | | mailSender.setPassword(ApplicationUtil.getProperty("email.password")); |
| | | mailSender.setHost(ApplicationUtil.getProperty("email.host")); |
| | | mailSender.setHost("smtp.gmail.com"); |
| | | mailSender.setPort(587); // 直接设置端口 |
| | | |
| | | Properties javaMailProperties = new Properties(); |
| | | javaMailProperties.setProperty("mail.smtp.port", "465"); |
| | | javaMailProperties.setProperty("mail.smtp.starttls.enable", "true"); |
| | | javaMailProperties.setProperty("mail.smtp.auth", "true"); |
| | | javaMailProperties.setProperty("mmail.debug", "true"); |
| | | javaMailProperties.setProperty("mail.smtp.host", "smtp.gmail.com"); |
| | | javaMailProperties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); |
| | | javaMailProperties.setProperty("mail.smtp.socketFactory.port", "465"); |
| | | javaMailProperties.setProperty("mail.smtp.starttls.enable", "true"); |
| | | javaMailProperties.setProperty("mail.smtp.starttls.required", "true"); |
| | | javaMailProperties.setProperty("mail.debug", "true"); // 修正拼写错误:mmail -> mail |
| | | |
| | | // 添加超时设置 |
| | | javaMailProperties.setProperty("mail.smtp.timeout", "30000"); |
| | | javaMailProperties.setProperty("mail.smtp.connectiontimeout", "30000"); |
| | | javaMailProperties.setProperty("mail.smtp.writetimeout", "30000"); |
| | | |
| | | |
| | | mailSender.setJavaMailProperties(javaMailProperties); |
| | | |
| | | mailMessage = new SimpleMailMessage(); |
| | | mailMessage.setFrom(ApplicationUtil.getProperty("email.from")); |
| | | |
| | | freeMarkerConfigurer = new FreeMarkerConfigurer(); |
| | | freeMarkerConfigurer.setTemplateLoaderPath("classpath:email/ftl"); |
| | | Properties settings = new Properties(); |
| | |
| | | freeMarkerConfigurer.setFreemarkerSettings(settings); |
| | | } |
| | | |
| | | |
| | | |
| | | private static final OkHttpClient client = new OkHttpClient.Builder() |
| | | .connectTimeout(10, java.util.concurrent.TimeUnit.SECONDS) // 设置连接超时 |
| | | .readTimeout(30, java.util.concurrent.TimeUnit.SECONDS) // 设置读取超时 |
| | | .writeTimeout(30, java.util.concurrent.TimeUnit.SECONDS) // 设置写入超时 |
| | | .build(); |
| | | |
| | | private static final String API_URL = "https://apiv2.aoksend.com/index/api/send_email"; |
| | | private static final String APP_KEY = "7c653bf4e8398f676de6182a5ac100ed"; // 从环境变量或配置文件中获取 |
| | | private static final String TEMPLATE_ID = "E_139512804023"; //模板 |
| | | @Override |
| | | public void send(EmailMessage emailMessage) { |
| | | // 验证邮件信息数据的有效性 |
| | | if (emailMessage == null || emailMessage.getTomail() == null || emailMessage.getContent() == null) { |
| | | logger.error("无效的邮件信息数据。"); |
| | | return; |
| | | } |
| | | |
| | | try { |
| | | MimeMessage mailMsg = this.mailSender.createMimeMessage(); |
| | | MimeMessageHelper messageHelper = new MimeMessageHelper(mailMsg, true, "UTF-8"); |
| | | messageHelper.setTo(emailMessage.getTomail());// 接收邮箱 |
| | | messageHelper.setFrom(this.mailMessage.getFrom());// 发送邮箱 |
| | | messageHelper.setSentDate(new Date());// 发送时间 |
| | | messageHelper.setSubject(emailMessage.getSubject());// 邮件标题 |
| | | if (StringUtils.isNullOrEmpty(emailMessage.getFtlname())) { |
| | | messageHelper.setText(emailMessage.getContent());// 邮件内容 |
| | | } else { |
| | | messageHelper.setText(this.getMailText(emailMessage.getFtlname(), emailMessage.getMap()), true);// 邮件内容 |
| | | logger.info("----- 开始发送邮件 -----"); |
| | | logger.info("发送邮件到: " + emailMessage.getTomail() + ", 来自: " + emailMessage.getContent()); |
| | | |
| | | // 使用 URL 构建器构建带有查询参数的 URL |
| | | HttpUrl.Builder urlBuilder = HttpUrl.parse(API_URL).newBuilder(); |
| | | urlBuilder.addQueryParameter("app_key", APP_KEY); |
| | | urlBuilder.addQueryParameter("template_id", TEMPLATE_ID); |
| | | urlBuilder.addQueryParameter("to", emailMessage.getTomail()); |
| | | |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | Map<String, String> dataMap = new HashMap<>(); |
| | | dataMap.put("code", emailMessage.getContent()); |
| | | |
| | | String json = objectMapper.writeValueAsString(dataMap); |
| | | urlBuilder.addQueryParameter("data", json); |
| | | |
| | | // 构建请求体,使用 POST 方法 |
| | | RequestBody body = RequestBody.create( |
| | | json, MediaType.parse("application/json; charset=utf-8") |
| | | ); |
| | | |
| | | // 构建 POST 请求 |
| | | Request request = new Request.Builder() |
| | | .url(urlBuilder.build()) |
| | | .post(body) // 使用 POST 方法,并传递请求体 |
| | | .addHeader("app_key", APP_KEY) |
| | | .build(); |
| | | |
| | | // 执行请求 |
| | | try (Response response = client.newCall(request).execute()) { |
| | | if (!response.isSuccessful()) { |
| | | logger.error("邮件发送失败。HTTP 响应码: " + response.code()); |
| | | return; |
| | | } |
| | | |
| | | logger.info("----- 邮件发送成功 -----"); |
| | | // 可选:记录响应体的内容(如果需要) |
| | | logger.info("响应内容: " + response.body().string()); |
| | | } |
| | | logger.info("邮件发送内容:"+emailMessage.getContent()); |
| | | // true 表示启动HTML格式的邮件 |
| | | if (emailMessage.getFile() != null) { |
| | | // 添加邮件附件 |
| | | FileSystemResource rarfile = new FileSystemResource(emailMessage.getFile()); |
| | | // addAttachment addInline 两种附件添加方式 |
| | | // 以附件的形式添加到邮件 |
| | | // 使用MimeUtility.encodeWord 解决附件名中文乱码的问题 |
| | | messageHelper.addAttachment(MimeUtility.encodeWord(emailMessage.getFilename()), rarfile); |
| | | } |
| | | this.mailSender.send(mailMsg);// 发送 |
| | | } catch (MessagingException e) { |
| | | logger.error(e.getMessage(), e); |
| | | } catch (UnsupportedEncodingException e) { |
| | | logger.error(e.getMessage(), e); |
| | | |
| | | } catch (IOException e) { |
| | | logger.error("邮件发送失败【IOException】", e); |
| | | } catch (Exception e) { |
| | | logger.error("邮件发送失败【Exception】", e); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | return html; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | EmailMessage emailMessage = new EmailMessage(); |
| | | emailMessage.setTomail("zz6490133@gmail.com"); |
| | | emailMessage.setContent("1341"); |
| | | // 验证邮件信息数据的有效性 |
| | | if (emailMessage == null || emailMessage.getTomail() == null || emailMessage.getContent() == null) { |
| | | logger.error("无效的邮件信息数据。"); |
| | | return; |
| | | } |
| | | |
| | | try { |
| | | logger.info("----- 开始发送邮件 -----"); |
| | | logger.info("发送邮件到: " + emailMessage.getTomail() + ", 来自: " + emailMessage.getContent()); |
| | | |
| | | // 使用 URL 构建器构建带有查询参数的 URL |
| | | HttpUrl.Builder urlBuilder = HttpUrl.parse(API_URL).newBuilder(); |
| | | urlBuilder.addQueryParameter("app_key", APP_KEY); |
| | | urlBuilder.addQueryParameter("template_id", TEMPLATE_ID); |
| | | urlBuilder.addQueryParameter("to", emailMessage.getTomail()); |
| | | |
| | | // 将邮件内容以 JSON 形式传递 |
| | | String json = "{\"code\":\"" + emailMessage.getContent() + "\"}"; |
| | | urlBuilder.addQueryParameter("data", json); // 确保正确编码 |
| | | |
| | | // 构建请求体,使用 POST 方法 |
| | | RequestBody body = RequestBody.create( |
| | | json, MediaType.parse("application/json; charset=utf-8") |
| | | ); |
| | | |
| | | // 构建 POST 请求 |
| | | Request request = new Request.Builder() |
| | | .url(urlBuilder.build()) |
| | | .post(body) // 使用 POST 方法,并传递请求体 |
| | | .addHeader("app_key", APP_KEY) |
| | | .build(); |
| | | |
| | | // 执行请求 |
| | | try (Response response = client.newCall(request).execute()) { |
| | | if (!response.isSuccessful()) { |
| | | logger.error("邮件发送失败。HTTP 响应码: " + response.code()); |
| | | return; |
| | | } |
| | | |
| | | logger.info("----- 邮件发送成功 -----"); |
| | | // 可选:记录响应体的内容(如果需要) |
| | | logger.info("响应内容: " + response.body().string()); |
| | | } |
| | | |
| | | } catch (IOException e) { |
| | | logger.error("邮件发送失败【IOException】", e); |
| | | } catch (Exception e) { |
| | | logger.error("邮件发送失败【Exception】", e); |
| | | } |
| | | } |
| | | } |