|
|
@@ -0,0 +1,71 @@
|
|
|
+package top.husj.husj_wx.utils;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+import jakarta.mail.Message;
|
|
|
+import jakarta.mail.MessagingException;
|
|
|
+import jakarta.mail.Session;
|
|
|
+import jakarta.mail.Transport;
|
|
|
+import jakarta.mail.Message.RecipientType;
|
|
|
+import jakarta.mail.internet.InternetAddress;
|
|
|
+import jakarta.mail.internet.MimeMessage;
|
|
|
+
|
|
|
+public class EmailUtil {
|
|
|
+ private Session session;
|
|
|
+ private String fromEmail;
|
|
|
+
|
|
|
+ public EmailUtil(String host, String port, String username, String password) {
|
|
|
+ Properties props = new Properties();
|
|
|
+ props.put("mail.smtp.auth", "true");
|
|
|
+ props.put("mail.smtp.starttls.enable", "true");
|
|
|
+ props.put("mail.smtp.host", host);
|
|
|
+ props.put("mail.smtp.port", port);
|
|
|
+ props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
|
|
+ props.put("mail.smtp.socketFactory.port", port);
|
|
|
+ this.session = Session.getInstance(props, new jakarta.mail.Authenticator() {
|
|
|
+ protected jakarta.mail.PasswordAuthentication getPasswordAuthentication() {
|
|
|
+ return new jakarta.mail.PasswordAuthentication(username, password);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ this.fromEmail = username;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sendTextEmail(String to, String subject, String text) throws MessagingException {
|
|
|
+ Message message = new MimeMessage(this.session);
|
|
|
+ message.setFrom(new InternetAddress(this.fromEmail));
|
|
|
+ message.setRecipients(RecipientType.TO, InternetAddress.parse(to));
|
|
|
+ message.setSubject(subject);
|
|
|
+ message.setText(text);
|
|
|
+ Transport.send(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sendHtmlEmail(String to, String subject, String htmlContent) throws MessagingException, UnsupportedEncodingException {
|
|
|
+ Message message = new MimeMessage(this.session);
|
|
|
+ message.setFrom(new InternetAddress(this.fromEmail, "发件人名称"));
|
|
|
+ message.setRecipients(RecipientType.TO, InternetAddress.parse(to));
|
|
|
+ message.setSubject(subject);
|
|
|
+ message.setContent(htmlContent, "text/html;charset=UTF-8");
|
|
|
+ Transport.send(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String host = "smtp.030208.xyz";
|
|
|
+ String port = "465";
|
|
|
+ String username = "husj@030208.xyz";
|
|
|
+ String password = "15629747218hsjH";
|
|
|
+ EmailUtil emailUtil = new EmailUtil(host, port, username, password);
|
|
|
+
|
|
|
+ try {
|
|
|
+ emailUtil.sendTextEmail("807244836@qq.com", "测试文本邮件", "这是一封测试邮件。");
|
|
|
+ String html = "<h1>HTML邮件测试</h1><p style='color: red;'>红色文本</p><p><a href='https://example.com'>链接</a></p>";
|
|
|
+ emailUtil.sendHtmlEmail("807244836@qq.com", "测试HTML邮件", html);
|
|
|
+ System.out.println("邮件发送成功!");
|
|
|
+ } catch (Exception var7) {
|
|
|
+ var7.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|