在 pom.xml
文件中添加 Spring Boot Starter Mail 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
在 application.properties
或 application.yml
中配置 SMTP 服务器信息:
spring.mail.host=your_smtp_host
spring.mail.port=your_smtp_port
spring.mail.username=your_username
spring.mail.password=your_password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
@Autowired
private JavaMailSender emailSender;
public void sendSimpleMessage(String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(text);
emailSender.send(message);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Service
public class EmailService {
@Autowired
private JavaMailSender emailSender;
public void sendHtmlMessage(String to, String subject, String htmlBody) throws MessagingException {
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(htmlBody, true);
emailSender.send(message);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class EmailService {
@Autowired
private JavaMailSender emailSender;
public void sendMessageWithAttachment(String to, String subject, String text, String pathToAttachment) throws MessagingException {
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(text);
FileSystemResource file = new FileSystemResource(new File(pathToAttachment));
helper.addAttachment("Attachment", file);
emailSender.send(message);
}
}
使用 Spring 的定时任务功能,在需要定时发送的方法上加上 @Scheduled
注解:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledEmail {
private final EmailService emailService;
public ScheduledEmail(EmailService emailService) {
this.emailService = emailService;
}
@Scheduled(cron = "0 0 8 * * ?") // 每天早上8点发送邮件
public void sendScheduledEmail() {
// 发送邮件逻辑
emailService.sendSimpleMessage("recipient@example.com", "Subject", "Content");
}
}
以上代码提供了基本的邮件发送逻辑和定时任务配置。在实际应用中,请替换相关信息,例如邮件主题、内容、收件人地址以及附件路径等。