|
@@ -0,0 +1,109 @@
|
|
|
+package com.abi.qms.platform.infrastructure.util;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.mail.*;
|
|
|
+import javax.mail.internet.AddressException;
|
|
|
+import javax.mail.internet.InternetAddress;
|
|
|
+import javax.mail.internet.MimeMessage;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class SendmailUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置服务器
|
|
|
+ */
|
|
|
+ @Value(value = "${smtp.host}")
|
|
|
+ private String smtp_host;
|
|
|
+ @Value(value = "${smtp.port}")
|
|
|
+ private String smtp_port;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 服务器验证
|
|
|
+ */
|
|
|
+ @Value(value = "${smtp.auth}")
|
|
|
+ private boolean value_props;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发件人用户名、密码
|
|
|
+ */
|
|
|
+ @Value(value = "${mail.send.user}")
|
|
|
+ private String send_user;
|
|
|
+ @Value(value = "${mail.send.username}")
|
|
|
+ private String send_uname;
|
|
|
+ @Value(value = "${mail.send.authcode}")
|
|
|
+ private String authcode;
|
|
|
+
|
|
|
+ private Properties props;
|
|
|
+ private Session s;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化方法
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private void initProps() {
|
|
|
+ if(props!=null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ props.setProperty("mail.smtp.host", smtp_host);
|
|
|
+ props.put("mail.smtp.auth", "true");
|
|
|
+ props.put("mail.smtp.port", smtp_port);
|
|
|
+ props.put("mail.smtp.starttls.enable", true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送邮件
|
|
|
+ *
|
|
|
+ * @param headName 邮件头文件名
|
|
|
+ * @param sendHtml 邮件内容
|
|
|
+ * @param receiveUser 收件人地址
|
|
|
+ */
|
|
|
+ public void doSendHtmlEmail(String headName, String sendHtml,
|
|
|
+ String receiveUser) {
|
|
|
+ //懒加载props
|
|
|
+ initProps();
|
|
|
+ //获取session
|
|
|
+ Session s = Session.getDefaultInstance(props, new Authenticator() {
|
|
|
+ @Override
|
|
|
+ protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
+ return new PasswordAuthentication(send_uname, authcode);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ s.setDebug(true);
|
|
|
+ MimeMessage message = new MimeMessage(s);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 发件人
|
|
|
+ InternetAddress from = new InternetAddress(send_user);
|
|
|
+ message.setFrom(from);
|
|
|
+ // 收件人
|
|
|
+ InternetAddress to = new InternetAddress(receiveUser);
|
|
|
+ message.setRecipient(Message.RecipientType.TO, to);
|
|
|
+ // 邮件标题
|
|
|
+ message.setSubject(headName);
|
|
|
+ String content = sendHtml.toString();
|
|
|
+ // 邮件内容,也可以使纯文本"text/plain"
|
|
|
+ message.setContent(content, "text/html;charset=GBK");
|
|
|
+ message.saveChanges();
|
|
|
+ Transport transport = s.getTransport("smtp");
|
|
|
+ // smtp验证,就是你用来发邮件的邮箱用户名密码
|
|
|
+ transport.connect(smtp_host, send_uname, authcode);
|
|
|
+ // 发送
|
|
|
+ transport.sendMessage(message, message.getAllRecipients());
|
|
|
+ transport.close();
|
|
|
+ log.info("send success!");
|
|
|
+ } catch (AddressException e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (MessagingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|