@Component
@EnableScheduling
public class ScheduledTest {
@Autowired
private JavaMailSender sender;
private String Subject = "主题";
private String Content = "正文";
private String To = "2039701916@qq.com";
private String from = "16638011271@163.com";
// @Scheduled(cron = "0/5 * * * * *")
public void test(){
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject(Subject);
simpleMailMessage.setText(Content);
simpleMailMessage.setTo(To);
simpleMailMessage.setFrom(from);
sender.send(simpleMailMessage);
System.out.println("发送邮件");
}
@Scheduled(cron = "0/5 * * * * *")
public void test1() throws Exception{
// 创建封装复杂邮件的对象
MimeMessage mail = sender.createMimeMessage();
// 创建复杂邮件辅助工具类,并与 创建封装复杂邮件的对象 建立联系,并开启multipart模式也可以说开启附件模式
MimeMessageHelper Helper = new MimeMessageHelper(mail,true);
Helper.setSubject(Subject);
// 在正文中我发送一张链接图片
Helper.setText(Content + "<img src='https://cdn.pixabay.com/photo/2024/04/08/14/09/nature-8683570_1280.jpg' />",true);
Helper.setTo(To);
Helper.setFrom(from);
File file = new File("D:\\Desktop\\上海磬德50辆牵引车明细表(1).xlsx");
// 进行邮件附件的附加
Helper.addAttachment("车辆信息",file);
sender.send(mail);
System.out.println("发送邮件完毕");
}
}
有疑问点击该链接 写文章-CSDN创作中心