| | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 邮件服务类 |
| | | * |
| | | * @author www |
| | | * @date 2024-03-21 |
| | | */ |
| | | @Service |
| | | public class EmailService { |
| | | @Value("${email.userName}") |
| | |
| | | @Value("${email.enable}") |
| | | private Boolean enable; |
| | | |
| | | @Value("${email.debug}") |
| | | private Boolean debug; |
| | | |
| | | @Resource |
| | | private QxshMapper qxshMapper; |
| | | |
| | |
| | | protected final Logger logger = LoggerFactory.getLogger(this.getClass()); |
| | | |
| | | public Session createSession() { |
| | | // 创建一个配置文件,并保存 |
| | | Properties props = new Properties(); |
| | | |
| | | // SMTP服务器连接信息:126—smtp.126.com,163—smtp.163.com,qq-smtp.qq.com" |
| | | props.put("mail.smtp.host", smtpHost); // SMTP主机名 |
| | | props.put("mail.smtp.port", smtpPort); // 主机端口号:126—25,163—645,qq-587 |
| | | props.put("mail.smtp.auth", smtpAuth); // 是否需要用户认证 |
| | | props.put("mail.smtp.starttls.enale", smtpTls); // 启用TlS加密 |
| | | // 126—smtp.126.com,163—smtp.163.com,qq-smtp.qq.com" |
| | | props.put("mail.smtp.host", smtpHost); |
| | | props.put("mail.smtp.port", smtpPort); |
| | | props.put("mail.smtp.auth", smtpAuth); |
| | | props.put("mail.smtp.starttls.enale", smtpTls); |
| | | |
| | | Session session = Session.getInstance(props, new Authenticator() { |
| | | @Override |
| | |
| | | } |
| | | }); |
| | | |
| | | // 控制台打印调试信息 |
| | | session.setDebug(true); |
| | | session.setDebug(debug); |
| | | |
| | | return session; |
| | | } |
| | |
| | | return null; |
| | | } |
| | | |
| | | // 创建Session会话 |
| | | Session session = createSession(); |
| | | |
| | | // 创建邮件对象 |
| | | MimeMessage message = new MimeMessage(session); |
| | | message.setSubject(title); |
| | | message.setText(text); |
| | |
| | | message.setRecipients(Message.RecipientType.CC, ias); |
| | | } |
| | | |
| | | // 发送 |
| | | Transport.send(message); |
| | | |
| | | return true; |