|
@@ -1,5 +1,6 @@
|
|
|
package com.fdkankan.sms;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.sun.mail.util.MailSSLSocketFactory;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
@@ -28,6 +29,7 @@ public class SendMailAcceUtils {
|
|
|
|
|
|
public final static String CN_CODE_SUBJECT = "四维看看验证码";
|
|
|
|
|
|
+
|
|
|
public final static String EN_CODE_SUBJECT = "4Dkankan verification code";
|
|
|
|
|
|
public final static String EN_CODE_SUBJECT_USA = "FALCON VISUAL";
|
|
@@ -63,6 +65,103 @@ public class SendMailAcceUtils {
|
|
|
"<div> Note: This is an auto-sent Email, please do not reply directly. May you have any questions, please contact our customer service.</div><br>";
|
|
|
|
|
|
/**
|
|
|
+ *
|
|
|
+ * @param userName 发送人账号
|
|
|
+ * @param password 发送人密码
|
|
|
+ * @param stmpHost smtp服务器地址
|
|
|
+ * @param receive 接收人邮箱地址
|
|
|
+ * @param subject 主题
|
|
|
+ * @param msg 消息主题
|
|
|
+ * @param filename 附件地址
|
|
|
+ * @return
|
|
|
+ * @throws GeneralSecurityException
|
|
|
+ */
|
|
|
+ public static boolean sendMail(String userName, String password, String stmpHost,
|
|
|
+ String receive, String subject, String msg, String filename)
|
|
|
+ throws GeneralSecurityException {
|
|
|
+ if (StrUtil.isEmpty(userName)
|
|
|
+ || StrUtil.isEmpty(password)
|
|
|
+ || StrUtil.isEmpty(stmpHost)
|
|
|
+ || StrUtil.isEmpty(receive)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // smtp服务器的链接信息
|
|
|
+ Properties properties = new Properties();
|
|
|
+
|
|
|
+ // 设置邮件服务器
|
|
|
+ properties.setProperty("mail.smtp.host", stmpHost);
|
|
|
+
|
|
|
+ properties.put("mail.smtp.auth", "true");
|
|
|
+ MailSSLSocketFactory sf = new MailSSLSocketFactory();
|
|
|
+ sf.setTrustAllHosts(true);
|
|
|
+ properties.put("mail.smtp.ssl.enable", "true");
|
|
|
+ properties.put("mail.smtp.ssl.socketFactory", sf);
|
|
|
+ // 获取默认session对象
|
|
|
+ Session session = Session.getDefaultInstance(properties, new Authenticator() {
|
|
|
+ public PasswordAuthentication getPasswordAuthentication() { // qq邮箱服务器账户、第三方登录授权码
|
|
|
+ return new PasswordAuthentication(userName, password); // 发件人邮件用户名、密码
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 创建默认的 MimeMessage 对象
|
|
|
+ MimeMessage message = new MimeMessage(session);
|
|
|
+
|
|
|
+ // Set From: 头部头字段
|
|
|
+ message.setFrom(new InternetAddress(userName));
|
|
|
+
|
|
|
+ // Set To: 头部头字段
|
|
|
+ message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));
|
|
|
+
|
|
|
+ // Set Subject: 主题文字
|
|
|
+ message.setSubject(subject);
|
|
|
+
|
|
|
+ // 创建消息部分
|
|
|
+ BodyPart messageBodyPart = new MimeBodyPart();
|
|
|
+
|
|
|
+ // 消息
|
|
|
+ messageBodyPart.setText(msg);
|
|
|
+
|
|
|
+ // 创建多重消息
|
|
|
+ MimeMultipart multipart = new MimeMultipart("mixed");
|
|
|
+
|
|
|
+ if(StringUtils.isNotEmpty(filename)){
|
|
|
+ // 附件部分
|
|
|
+ messageBodyPart = new MimeBodyPart();
|
|
|
+ // 设置要发送附件的文件路径
|
|
|
+ DataSource source = new FileDataSource(filename);
|
|
|
+ messageBodyPart.setDataHandler(new DataHandler(source));
|
|
|
+
|
|
|
+ // messageBodyPart.setFileName(filename);
|
|
|
+ // 处理附件名称中文(附带文件路径)乱码问题
|
|
|
+ messageBodyPart.setFileName(MimeUtility.encodeText(filename));
|
|
|
+ multipart.addBodyPart(messageBodyPart);
|
|
|
+ }
|
|
|
+
|
|
|
+ //html代码部分
|
|
|
+ MimeBodyPart htmlPart = new MimeBodyPart();
|
|
|
+ multipart.addBodyPart(htmlPart);
|
|
|
+ //html代码
|
|
|
+ htmlPart.setContent(msg, "text/html;charset=utf-8");
|
|
|
+
|
|
|
+ // 发送完整消息
|
|
|
+ message.setContent(multipart);
|
|
|
+
|
|
|
+ // 发送消息
|
|
|
+ Transport.send(message);
|
|
|
+ // System.out.println("Sent message successfully....");
|
|
|
+ return true;
|
|
|
+ } catch (MessagingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 发送带附件的邮件
|
|
|
*
|
|
|
* @param receive
|