mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 08:58:38 +08:00
添加发送html邮件的方法
This commit is contained in:
parent
1b5f9eee04
commit
1cff9a1b73
14
pom.xml
14
pom.xml
@ -61,6 +61,8 @@
|
||||
<hutool.version>5.8.18</hutool.version>
|
||||
<xmlblend.version>2.3.0</xmlblend.version>
|
||||
<activation.version>1.1.1</activation.version>
|
||||
<mail.version>1.6.2</mail.version>
|
||||
<sunactivation.version>1.2.0</sunactivation.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@ -166,6 +168,18 @@
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>${mail.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sun.activation</groupId>
|
||||
<artifactId>javax.activation</artifactId>
|
||||
<version>${sunactivation.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
@ -20,29 +20,43 @@
|
||||
|
||||
<name>sms4j-Email-plugin</name>
|
||||
<description>sms4j-Email-plugin</description>
|
||||
|
||||
<properties>
|
||||
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-Email-api</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-Email-api</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-Email-comm</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-Email-comm</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-Email-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-Email-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>${mail.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sun.activation</groupId>
|
||||
<artifactId>javax.activation</artifactId>
|
||||
<version>${sunactivation.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
</project>
|
||||
|
||||
@ -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);
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package org.dromara.email.comm.config;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* MailSmtpConfig
|
||||
* <p> smtp协议配置文件
|
||||
* @author :Wind
|
||||
* 2023/6/7 21:19
|
||||
**/
|
||||
@Data
|
||||
public class MailSmtpConfig {
|
||||
/**
|
||||
* 端口号
|
||||
* */
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 服务器地址
|
||||
* */
|
||||
private String smtpServer;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
* */
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
* */
|
||||
private String password;
|
||||
}
|
||||
@ -12,14 +12,16 @@ import java.util.Objects;
|
||||
/**
|
||||
* HtmlUtil
|
||||
* <p> 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
|
||||
* <p>从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;
|
||||
|
||||
@ -22,6 +22,18 @@
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-Email-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sun.activation</groupId>
|
||||
<artifactId>javax.activation</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user