parent
40fda3defd
commit
59720dcab8
@ -0,0 +1,83 @@
|
||||
package com.ruoyi.jjh.declaration.component;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 发送邮件业务处理层
|
||||
* @author du
|
||||
* @since 2024/12/12 9:55
|
||||
*/
|
||||
@Component("EmailEnterImpl")
|
||||
public class EmailEnterImpl implements EmailEnterService{
|
||||
|
||||
/**
|
||||
* 是否是正式环境
|
||||
*/
|
||||
@Value("${isTiming}")
|
||||
private Boolean isTiming;
|
||||
|
||||
/**
|
||||
* SMTP 服务器
|
||||
*/
|
||||
@Value("${smtp}")
|
||||
private String smtp;
|
||||
|
||||
/**
|
||||
* SMTP IP
|
||||
*/
|
||||
@Value("${smtpIp}")
|
||||
private String smtpIp;
|
||||
|
||||
/**
|
||||
* SMTP 端口
|
||||
*/
|
||||
@Value("${smtpDk}")
|
||||
private String smtpDk;
|
||||
|
||||
/**
|
||||
* 根据邮箱地址发送邮件
|
||||
*/
|
||||
@Override
|
||||
public Boolean toEnter(String subject, String body,String toEmail) {
|
||||
// 发件人邮箱的登录凭证
|
||||
String username = "jjdn@sipac.gov.cn"; // 发件人邮箱用户名(一般为邮箱地址)
|
||||
String password = "N!4lsU.T_#2T2Z"; // 发件人邮箱的授权码
|
||||
// 设置邮件服务器属性
|
||||
Properties properties = new Properties();
|
||||
properties.put("mail.smtp.host", smtpIp);
|
||||
properties.put("mail.smtp.port", smtpDk);
|
||||
properties.put("mail.smtp.auth", "true"); // 启用身份验证
|
||||
properties.put("mail.smtp.starttls.enable", "true"); // 启用 TLS
|
||||
// 获取邮件会话
|
||||
Session session = Session.getInstance(properties, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
// 获取邮件会话
|
||||
try {
|
||||
// 创建邮件消息
|
||||
MimeMessage message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress(username));
|
||||
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
|
||||
message.setSubject(subject);
|
||||
message.setText(body);
|
||||
// 发送邮件
|
||||
Transport.send(message);
|
||||
System.out.println("邮件发送成功");
|
||||
} catch (AddressException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MessagingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.ruoyi.jjh.declaration.component;
|
||||
|
||||
/**
|
||||
* 发送邮件业务层
|
||||
* @author du
|
||||
* @since 2024/12/12 9:50
|
||||
*/
|
||||
public interface EmailEnterService {
|
||||
/**
|
||||
* 根据邮箱地址发送邮件
|
||||
*/
|
||||
Boolean toEnter(String subject, String body,String toEmail);
|
||||
}
|
Loading…
Reference in new issue