✨Feat: 增加发送邮件功能
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package cn.hezhaohui.mail;
|
||||
|
||||
import cn.hezhaohui.mail.service.MailService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SendMailApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SendMailApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.hezhaohui.mail.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
@Configuration
|
||||
public class MailConfig {
|
||||
|
||||
@Bean
|
||||
public JavaMailSender getJavaMailSender() {
|
||||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
||||
mailSender.setHost("smtp.163.com");
|
||||
mailSender.setPort(465);
|
||||
|
||||
mailSender.setUsername("hezhaohui0807@163.com");
|
||||
mailSender.setPassword("AMaauzQZQMnkSfZi");
|
||||
|
||||
Properties properties = mailSender.getJavaMailProperties();
|
||||
properties.put("mail.transport.protocol", "smtp");
|
||||
properties.put("mail.smtp.auth", "true");
|
||||
properties.put("mail.smtp.ssl.enable", "true");
|
||||
properties.put("mail.smtp.starttls.enable", "true");
|
||||
properties.put("mail.debug", "true");
|
||||
|
||||
return mailSender;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.hezhaohui.mail.service;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MailService {
|
||||
|
||||
@Resource
|
||||
private JavaMailSender emailSender;
|
||||
|
||||
public void sendMessage(String to, String subject, String text) {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setFrom("hezhaohui0807@163.com");
|
||||
message.setTo(to);
|
||||
message.setSubject(subject);
|
||||
message.setText(text);
|
||||
emailSender.send(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user