|
@@ -0,0 +1,159 @@
|
|
|
+package com.zbkj.common.utils;
|
|
|
+
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.mail.SimpleMailMessage;
|
|
|
+import org.springframework.mail.javamail.JavaMailSender;
|
|
|
+import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.mail.*;
|
|
|
+import javax.mail.internet.InternetAddress;
|
|
|
+import javax.mail.internet.MimeMessage;
|
|
|
+import java.io.File;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.UnknownHostException;
|
|
|
+import java.security.Security;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ClassName EmailUtil
|
|
|
+ * @Description 邮件发送工具
|
|
|
+ * @Author Sophia
|
|
|
+ * @Date 2022/4/6 16:06
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class EmailUtil {
|
|
|
+
|
|
|
+// @Value("${spring.mail.from}") // 从application.yml配置文件中获取
|
|
|
+// private String from; // 发送发邮箱地址
|
|
|
+//
|
|
|
+// @Autowired
|
|
|
+// private JavaMailSender mailSender;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送纯文本邮件信息
|
|
|
+ *
|
|
|
+ * @param to 接收方
|
|
|
+ * @param subject 邮件主题
|
|
|
+ * @param content 邮件内容(发送内容)
|
|
|
+ */
|
|
|
+ public void sendMessage(String username, String password, String smtpServer, String to, String subject, String content) {
|
|
|
+// Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
|
|
|
+ final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
|
|
|
+
|
|
|
+
|
|
|
+ // 创建属性对象
|
|
|
+ Properties props = new Properties();
|
|
|
+ props.put("mail.smtp.host", smtpServer);
|
|
|
+
|
|
|
+ props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
|
|
|
+ props.setProperty("mail.smtp.socketFactory.fallback", "false");
|
|
|
+ //邮箱发送服务器端口,这里设置为 587 端口
|
|
|
+
|
|
|
+ if ("smtp.163.com".equals(smtpServer)) {
|
|
|
+ props.setProperty("mail.smtp.port", "587");
|
|
|
+ props.setProperty("mail.smtp.socketFactory.port", "587");
|
|
|
+ }
|
|
|
+ if ("smtp.qq.com".equals(smtpServer)) {
|
|
|
+ props.setProperty("mail.smtp.port", "465");
|
|
|
+ props.setProperty("mail.smtp.socketFactory.port", "465");
|
|
|
+
|
|
|
+ }
|
|
|
+ props.put("mail.smtp.auth", "true");
|
|
|
+
|
|
|
+
|
|
|
+ // 创建Session对象
|
|
|
+ Session session = Session.getInstance(props, new Authenticator() {
|
|
|
+ protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
+ return new PasswordAuthentication(username, password);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ try {
|
|
|
+ InetAddress inetAddr = InetAddress.getLocalHost();
|
|
|
+
|
|
|
+ props.setProperty("mail.smtp.localhost", inetAddr.getHostName());
|
|
|
+ // 创建Message对象
|
|
|
+ Message message = new MimeMessage(session);
|
|
|
+ message.setFrom(new InternetAddress(username));
|
|
|
+ message.setSubject(subject);//主题
|
|
|
+ message.setText(content);//内容
|
|
|
+
|
|
|
+ // 设置收件人列表
|
|
|
+ Address[] recipients = new Address[]{
|
|
|
+ new InternetAddress(to),
|
|
|
+
|
|
|
+ };
|
|
|
+ message.setRecipients(Message.RecipientType.TO, recipients);
|
|
|
+
|
|
|
+ // 发送邮件
|
|
|
+ Transport.send(message);
|
|
|
+ System.out.println("Email sent successfully.");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 发送带附件的邮件信息
|
|
|
+// *
|
|
|
+// * @param to 接收方
|
|
|
+// * @param subject 邮件主题
|
|
|
+// * @param content 邮件内容(发送内容)
|
|
|
+// * @param files 文件数组 // 可发送多个附件
|
|
|
+// */
|
|
|
+// public void sendMessageCarryFiles(String to, String subject, String content, File[] files) {
|
|
|
+// MimeMessage mimeMessage = mailSender.createMimeMessage();
|
|
|
+// try {
|
|
|
+// MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
|
|
|
+// helper.setFrom(from); // 设置发送发
|
|
|
+// helper.setTo(to); // 设置接收方
|
|
|
+// helper.setSubject(subject); // 设置邮件主题
|
|
|
+// helper.setText(content); // 设置邮件内容
|
|
|
+// if (files != null && files.length > 0) { // 添加附件(多个)
|
|
|
+// for (File file : files) {
|
|
|
+// helper.addAttachment(file.getName(), file);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } catch (MessagingException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// // 发送邮件
|
|
|
+// mailSender.send(mimeMessage);
|
|
|
+// }
|
|
|
+// /**
|
|
|
+// * 发送带附件的邮件信息
|
|
|
+// *
|
|
|
+// * @param to 接收方
|
|
|
+// * @param subject 邮件主题
|
|
|
+// * @param content 邮件内容(发送内容)
|
|
|
+// * @param file 单个文件
|
|
|
+// */
|
|
|
+// public void sendMessageCarryFile(String to, String subject, String content, File file) {
|
|
|
+// MimeMessage mimeMessage = mailSender.createMimeMessage();
|
|
|
+// try {
|
|
|
+// MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
|
|
|
+// helper.setFrom(from); // 设置发送发
|
|
|
+// helper.setTo(to); // 设置接收方
|
|
|
+// helper.setSubject(subject); // 设置邮件主题
|
|
|
+// helper.setText(content); // 设置邮件内容
|
|
|
+// helper.addAttachment(file.getName(), file); // 单个附件
|
|
|
+// } catch (MessagingException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// // 发送邮件
|
|
|
+// mailSender.send(mimeMessage);
|
|
|
+// }
|
|
|
+//
|
|
|
+// public String getFrom() {
|
|
|
+// return from;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public void setFrom(String from) {
|
|
|
+// this.from = from;
|
|
|
+// }
|
|
|
+}
|
|
|
+
|
|
|
+
|