|
@@ -0,0 +1,56 @@
|
|
|
|
+package com.jpsoft.order.modules.common.controller;
|
|
|
|
+
|
|
|
|
+import com.jpsoft.order.modules.common.dto.MessageResult;
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
+
|
|
|
|
+import com.sun.mail.util.MailSSLSocketFactory;
|
|
|
|
+import java.util.*;
|
|
|
|
+import javax.mail.*;
|
|
|
|
+import javax.mail.internet.*;
|
|
|
|
+
|
|
|
|
+@RestController
|
|
|
|
+public class sendEmailController {
|
|
|
|
+ public static MessageResult sendEmail(String eMail, String eMailTitle, String eMailContent) throws Exception {
|
|
|
|
+ MessageResult messageResult = new MessageResult<>();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Properties prop = new Properties();
|
|
|
|
+ prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com"); // 设置QQ邮件服务器
|
|
|
|
+ prop.setProperty("mail.smtp.port", "465"); // 设置端口号
|
|
|
|
+ prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议
|
|
|
|
+ prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码
|
|
|
|
+
|
|
|
|
+ // 关于QQ邮箱,还要设置SSL加密,加上以下代码即可
|
|
|
|
+ MailSSLSocketFactory sf = new MailSSLSocketFactory();
|
|
|
|
+ sf.setTrustAllHosts(true);
|
|
|
|
+ prop.put("mail.smtp.ssl.enable", "true");
|
|
|
|
+ prop.put("mail.smtp.ssl.socketFactory", sf);
|
|
|
|
+
|
|
|
|
+ //1、创建定义整个应用程序所需的环境信息的 Session 对象
|
|
|
|
+ Session session = Session.getDefaultInstance(prop, new Authenticator() {
|
|
|
|
+ public PasswordAuthentication getPasswordAuthentication() {
|
|
|
|
+ return new PasswordAuthentication("it@feilihua.com", "Flh@2022");//发件人邮件用户名、授权码
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ session.setDebug(true);//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
|
|
|
|
+
|
|
|
|
+ //4、创建邮件
|
|
|
|
+ MimeMessage message = new MimeMessage(session);//创建邮件对象
|
|
|
|
+ message.setFrom(new InternetAddress("it@feilihua.com","湖北菲利华石英玻璃股份有限公司"));//指明邮件的发件人
|
|
|
|
+ message.setRecipient(Message.RecipientType.TO, new InternetAddress(eMail));//指明邮件的收件人
|
|
|
|
+ message.setSubject(eMailTitle);//邮件的标题
|
|
|
|
+ message.setContent(eMailContent, "text/html;charset=UTF-8");//邮件的文本内容
|
|
|
|
+
|
|
|
|
+ //5、发送邮件
|
|
|
|
+ Transport.send(message);
|
|
|
|
+
|
|
|
|
+ messageResult.setResult(true);
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e) {
|
|
|
|
+ messageResult.setResult(false);
|
|
|
|
+ messageResult.setMessage(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return messageResult;
|
|
|
|
+ }
|
|
|
|
+}
|