diff --git a/pom.xml b/pom.xml index 80dd99bf..5ed9c24e 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,8 @@ 5.8.18 2.3.0 1.1.1 + 1.6.2 + 1.2.0 @@ -166,6 +168,18 @@ import + + com.sun.mail + javax.mail + ${mail.version} + + + + com.sun.activation + javax.activation + ${sunactivation.version} + + diff --git a/sms4j-Email-plugin/pom.xml b/sms4j-Email-plugin/pom.xml index 40d86ba6..00c9982e 100644 --- a/sms4j-Email-plugin/pom.xml +++ b/sms4j-Email-plugin/pom.xml @@ -20,29 +20,43 @@ sms4j-Email-plugin sms4j-Email-plugin + + - - - org.dromara.sms4j - sms4j-Email-api - ${revision} - + + + org.dromara.sms4j + sms4j-Email-api + ${revision} + - - org.dromara.sms4j - sms4j-Email-comm - ${revision} - + + org.dromara.sms4j + sms4j-Email-comm + ${revision} + - - org.dromara.sms4j - sms4j-Email-core - ${revision} - - + + org.dromara.sms4j + sms4j-Email-core + ${revision} + + + + com.sun.mail + javax.mail + ${mail.version} + + + + com.sun.activation + javax.activation + ${sunactivation.version} + + diff --git a/sms4j-Email-plugin/sms4j-Email-api/src/main/java/org/dromara/email/api/MailClient.java b/sms4j-Email-plugin/sms4j-Email-api/src/main/java/org/dromara/email/api/MailClient.java new file mode 100644 index 00000000..8ea14077 --- /dev/null +++ b/sms4j-Email-plugin/sms4j-Email-api/src/main/java/org/dromara/email/api/MailClient.java @@ -0,0 +1,8 @@ +package org.dromara.email.api; + +public interface MailClient { + + void sendMail(String mailAddress, String Title ,String body); + + void sendHtml(String mailAddress, String Title ,String body); +} diff --git a/sms4j-Email-plugin/sms4j-Email-comm/src/main/java/org/dromara/email/comm/config/MailSmtpConfig.java b/sms4j-Email-plugin/sms4j-Email-comm/src/main/java/org/dromara/email/comm/config/MailSmtpConfig.java new file mode 100644 index 00000000..49a399cf --- /dev/null +++ b/sms4j-Email-plugin/sms4j-Email-comm/src/main/java/org/dromara/email/comm/config/MailSmtpConfig.java @@ -0,0 +1,32 @@ +package org.dromara.email.comm.config; + +import lombok.Data; + +/** + * MailSmtpConfig + *

smtp协议配置文件 + * @author :Wind + * 2023/6/7 21:19 + **/ +@Data +public class MailSmtpConfig { + /** + * 端口号 + * */ + private String host; + + /** + * 服务器地址 + * */ + private String smtpServer; + + /** + * 账号 + * */ + private String username; + + /** + * 密码 + * */ + private String password; +} diff --git a/sms4j-Email-plugin/sms4j-Email-comm/src/main/java/org/dromara/email/comm/utils/HtmlUtil.java b/sms4j-Email-plugin/sms4j-Email-comm/src/main/java/org/dromara/email/comm/utils/HtmlUtil.java index 4bcfbe5b..600e2155 100644 --- a/sms4j-Email-plugin/sms4j-Email-comm/src/main/java/org/dromara/email/comm/utils/HtmlUtil.java +++ b/sms4j-Email-plugin/sms4j-Email-comm/src/main/java/org/dromara/email/comm/utils/HtmlUtil.java @@ -12,14 +12,16 @@ import java.util.Objects; /** * HtmlUtil *

Html相关工具 - * * @author :Wind * 2023/6/7 20:15 **/ -public class HtmlUtil { +public final class HtmlUtil { private static final HtmlUtil htmlUtil = new HtmlUtil(); + private HtmlUtil() { + } + /** * readHtml *

从resource读取模板文件 @@ -88,7 +90,10 @@ public class HtmlUtil { String piece = piece(s.getKey()); if (datum.contains(piece)){ list.add(datum.replace(piece, s.getValue())); + }else { + list.add(datum); } + } } return list; diff --git a/sms4j-Email-plugin/sms4j-Email-core/pom.xml b/sms4j-Email-plugin/sms4j-Email-core/pom.xml index f2e23806..715173f9 100644 --- a/sms4j-Email-plugin/sms4j-Email-core/pom.xml +++ b/sms4j-Email-plugin/sms4j-Email-core/pom.xml @@ -22,6 +22,18 @@ org.dromara.sms4j sms4j-Email-api + + + com.sun.mail + javax.mail + compile + + + + com.sun.activation + javax.activation + + diff --git a/sms4j-Email-plugin/sms4j-Email-core/src/main/java/org/dromara/email/core/Sms4jEmailCoreApplication.java b/sms4j-Email-plugin/sms4j-Email-core/src/main/java/org/dromara/email/core/Sms4jEmailCoreApplication.java deleted file mode 100644 index f663641c..00000000 --- a/sms4j-Email-plugin/sms4j-Email-core/src/main/java/org/dromara/email/core/Sms4jEmailCoreApplication.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.dromara.email.core; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class Sms4jEmailCoreApplication { - - public static void main(String[] args) { - SpringApplication.run(Sms4jEmailCoreApplication.class, args); - } - -} diff --git a/sms4j-Email-plugin/sms4j-Email-core/src/main/java/org/dromara/email/core/service/MailService.java b/sms4j-Email-plugin/sms4j-Email-core/src/main/java/org/dromara/email/core/service/MailService.java new file mode 100644 index 00000000..beafa422 --- /dev/null +++ b/sms4j-Email-plugin/sms4j-Email-core/src/main/java/org/dromara/email/core/service/MailService.java @@ -0,0 +1,99 @@ +package org.dromara.email.core.service; + +import org.dromara.email.api.MailClient; + +import javax.mail.*; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import java.util.Properties; + +public class MailService implements MailClient { + + private MailService() { + } + + public static MailClient NewMailService() { + return new MailService(); + } + + @Override + public void sendMail(String mailAddress, String Title, String body) { + String smtpServer = "smtp.qq.com"; + String username = "wzsf1810@qq.com"; + String password = "xophzbzswkzkiacb"; + String fromAddress = "wzsf1810@qq.com"; + String toAddress = "291203727@qq.com"; + int port = 465; // SMTP服务器的端口号 + + Properties props = new Properties(); + props.put("mail.smtp.host", smtpServer); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.port", port); + props.put("mail.smtp.ssl.enable", "true"); +// props.put("mail.smtp.ssl.socketFactory", new MailSSLSocketFactory()); + + Session session = Session.getInstance(props, new Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + + try { + Message message = new MimeMessage(session); + message.setFrom(new InternetAddress(fromAddress)); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress)); + message.setSubject(Title); + message.setText(body); + + Transport.send(message); + + System.out.println("Email sent successfully."); + } catch (MessagingException e) { + System.out.println("Failed to send email: " + e.getMessage()); + } + } + + @Override + public void sendHtml(String mailAddress, String Title, String body) { + String smtpServer = "smtp.qq.com"; + String username = "wzsf1810@qq.com"; + String password = "xophzbzswkzkiacb"; + String fromAddress = "wzsf1810@qq.com"; + String toAddress = "291203727@qq.com"; + int port = 465; // SMTP服务器的端口号 + + Properties props = new Properties(); + props.put("mail.smtp.host", smtpServer); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.port", port); + props.put("mail.smtp.ssl.enable", "true"); +// props.put("mail.smtp.ssl.socketFactory", new MailSSLSocketFactory()); + + Session session = Session.getInstance(props, new Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + + try { + Message message = new MimeMessage(session); + message.setFrom(new InternetAddress(fromAddress)); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress)); + message.setSubject(Title); + // 创建 MimeMultipart 对象 + MimeMultipart multipart = new MimeMultipart("related"); + // 创建 MimeBodyPart 对象,并将 HTML 内容添加到 MimeMultipart 中 + MimeBodyPart htmlPart = new MimeBodyPart(); + htmlPart.setContent(body, "text/html;charset=UTF-8"); + message.setContent(multipart); + multipart.addBodyPart(htmlPart); + Transport.send(message); + + System.out.println("Email sent successfully."); + } catch (MessagingException e) { + System.out.println("Failed to send email: " + e.getMessage()); + } + } +}