From 043f821ea777b983c25ea6ec0b8d17a4abc25e9c Mon Sep 17 00:00:00 2001
From: liyunfengfengfeng <2305743208@qq.com>
Date: Sat, 10 Mar 2018 20:20:18 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=82=AE=E7=AE=B1=E5=8F=91?=
=?UTF-8?q?=E9=80=81=E9=AA=8C=E8=AF=81=E7=A0=81=E7=9A=84=E5=B7=A5=E5=85=B7?=
=?UTF-8?q?=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
beauty_ssm.iml | 3 +
pom.xml | 12 ++
.../java/com/yingjun/ssm/util/MailUtil.java | 151 ++++++++++++++++++
3 files changed, 166 insertions(+)
create mode 100644 src/main/java/com/yingjun/ssm/util/MailUtil.java
diff --git a/beauty_ssm.iml b/beauty_ssm.iml
index 4e033b3..b014c3b 100644
--- a/beauty_ssm.iml
+++ b/beauty_ssm.iml
@@ -95,5 +95,8 @@
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index f4961c4..9e70ee2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -241,6 +241,18 @@
1.9.12
+
+
+ javax.mail
+ mail
+ 1.4.5
+
+
+ com.sun.mail
+ javax.mail
+ 1.5.4
+
+
diff --git a/src/main/java/com/yingjun/ssm/util/MailUtil.java b/src/main/java/com/yingjun/ssm/util/MailUtil.java
new file mode 100644
index 0000000..b2a0a2c
--- /dev/null
+++ b/src/main/java/com/yingjun/ssm/util/MailUtil.java
@@ -0,0 +1,151 @@
+package com.yingjun.ssm.util;
+import javax.mail.*;
+import javax.mail.Message.RecipientType;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import java.util.Date;
+import java.util.Properties;
+
+/**
+ * 邮件工具类
+ * @author liyunfeng
+ */
+public class MailUtil {
+
+
+ /**
+ * 发件人的 邮箱 和 密码(替换为自己的邮箱和密码)
+ * PS: 某些邮箱服务器为了增加邮箱本身密码的安全性,给 SMTP 客户端设置了独立密码(有的邮箱称为“授权码”),
+ * 对于开启了独立密码的邮箱, 这里的邮箱密码必需使用这个独立密码(授权码)。
+ */
+ public static String account = "m18746046951@163.com";
+ public static String password = "lyf19960227";
+
+
+ /**
+ * 发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为: smtp.xxx.com
+ * 网易163邮箱的 SMTP 服务器地址为: smtp.163.com
+ */
+ public static String myEmailSMTPHost = "smtp.163.com";
+
+ /**
+ * 发送邮件的方法
+ * @param to 邮件的接收方
+ * @param code 邮件的激活码
+ */
+ public static void sendMail(String to, String code) {
+ // 1.创建连接对象,链接到邮箱服务器
+ Properties props = new Properties();
+ // 使用的协议(JavaMail规范要求)
+ props.setProperty("mail.transport.protocol", "smtp");
+ // 发件人的邮箱的 SMTP 服务器地址
+ props.setProperty("mail.smtp.host", myEmailSMTPHost);
+ // 需要请求认证
+ props.setProperty("mail.smtp.auth", "true");
+ // 2.根据配置创建会话对象, 用于和邮件服务器交互
+ Session session = Session.getInstance(props, new Authenticator() {
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication() {
+ return new PasswordAuthentication(account,password);
+ }
+ });
+
+ try {
+ // 3.创建邮件对象
+ Message message = new MimeMessage(session);
+ // 3.1设置发件人
+ message.setFrom(new InternetAddress(account));
+ // 3.2设置收件人
+ message.setRecipient(RecipientType.TO, new InternetAddress(to));
+ // 3.3设置邮件的主题
+ message.setSubject("来自集群注册平台的激活邮件");
+ // 3.4设置邮件的正文
+ message.setContent("
来自集群注册平台的激活邮件,您的验证码是:"+ code, "text/html;charset=UTF-8");
+ // 4.发送邮件
+ Transport.send(message);
+ } catch (MessagingException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * 发送找回密码邮件的方法
+ * @param to 邮件的接收方
+ * @param code 邮件的验证码
+ */
+ public static void findPasswordMail(String to, String code) {
+ // 1.创建连接对象,链接到邮箱服务器 参数配置
+ Properties props = new Properties();
+ // 使用的协议(JavaMail规范要求)
+ props.setProperty("mail.transport.protocol", "smtp");
+ // 发件人的邮箱的 SMTP 服务器地址
+ props.setProperty("mail.smtp.host", myEmailSMTPHost);
+ // 需要请求认证
+ props.setProperty("mail.smtp.auth", "true");
+ // 2.根据配置创建会话对象, 用于和邮件服务器交互
+ Session session = Session.getInstance(props, new Authenticator() {
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication() {
+ return new PasswordAuthentication(account,password);
+ }
+ });
+
+ try {
+ // 3.创建邮件对象
+ Message message = new MimeMessage(session);
+ // 3.1设置发件人
+ message.setFrom(new InternetAddress(account));
+ // 3.2设置收件人
+ message.setRecipient(RecipientType.TO, new InternetAddress(to));
+ // 3.3设置邮件的主题
+ message.setSubject("来自平台的验证邮件");
+ // 3.4设置邮件的正文
+ message.setContent("来自集群注册平台的验证邮件,请点击以下链接进行重置密码:
", "text/html;charset=UTF-8");
+ // 4.发送邮件
+ Transport.send(message);
+ } catch (MessagingException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ /**
+ * 创建一封只包含文本的简单邮件
+ *
+ * @param session 和服务器交互的会话
+ * @param sendMail 发件人邮箱
+ * @param receiveMail 收件人邮箱
+ * @return
+ * @throws Exception
+ */
+ public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception {
+ // 1. 创建一封邮件
+ MimeMessage message = new MimeMessage(session);
+
+ // 2. From: 发件人
+ message.setFrom(new InternetAddress(sendMail, "某宝网", "UTF-8"));
+
+ // 3. To: 收件人(可以增加多个收件人、抄送、密送)
+ message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "XX用户", "UTF-8"));
+
+ // 4. Subject: 邮件主题
+ message.setSubject("打折钜惠", "UTF-8");
+
+ // 5. Content: 邮件正文(可以使用html标签)
+ message.setContent("XX用户你好, 今天全场5折, 快来抢购, 错过今天再等一年。。。", "text/html;charset=UTF-8");
+
+ // 6. 设置发件时间
+ message.setSentDate(new Date());
+
+ // 7. 保存设置
+ message.saveChanges();
+
+ return message;
+ }
+
+ public static void main(String[]args){
+ //接收方 接受码
+ MailUtil.sendMail("2305743208@qq.com", "测试一下,干啥呢老哥,接到邮件回复一下啊啊啊啊啊");
+
+ }
+}
\ No newline at end of file
--
GitLab