SimpleMailMessage message = new SimpleMailMessage();
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
spring:
mail:
host: smtp.qq.com
username: 发件人邮箱
password: 授权密码
package com.sgz.springboot.service;
public interface SendMailService {
void sendMail();
}
package com.sgz.springboot.service.impl;
import com.sgz.springboot.service.SendMailService;
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 SendMailServiceImpl implements SendMailService {
@Autowired
private JavaMailSender javaMailSender;
// 发送人
private String from = "发件人邮箱";
// 接收人
private String to = "收件人邮箱";
// 标题
private String subject = "测试邮件标题";
// 正文
private String context = "测试邮件正文内容";
@Override
public void sendMail() {
// 创建一个简单邮件
SimpleMailMessage message = new SimpleMailMessage();
// 设置发件人
message.setFrom(from+"(发件人名称)"); // 可以设置发件人名称,要用英文的() message.setFrom(from+"(发件人名称)");
// 设置收件人
message.setTo(to);
// 设置发送的标题
message.setSubject(subject);
// 设置发送的内容
message.setText(context);
// 发送邮件
javaMailSender.send(message);
System.out.println("发送成功");
}
}
package com.sgz.springboot;
import com.sgz.springboot.service.SendMailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ApplicationTests {
@Autowired
private SendMailService sendMailService;
@Test
void contextLoads() {
sendMailService.sendMail();
}
}
package com.sgz.springboot.service.impl;
import com.sgz.springboot.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
* 日期:2022/9/6 - 15:35
* 需求:
*/
@Service
public class SendMailServiceImpl implements SendMailService {
@Autowired
private JavaMailSender javaMailSender;
// 发件人
private String from = "发件人邮箱";
// 收件人
private String to = "收件人邮箱";
// 标题
private String subject = "测试邮件标题";
// 正文
private String context = " 点开有惊喜 ";
@Override
public void sendMail() {
// 复杂邮件
try {
// 创建一个简单邮件
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
// 设置发件人
helper.setFrom(from+"(发件人名称)"); // 可以设置发件人名称,要用英文的() message.setFrom(from+"(发件人名称)");
// 设置收件人
helper.setTo(to);
// 设置发送的标题
helper.setSubject(subject);
// 设置发送的内容
helper.setText(context,true); // true:有超连接时,解析连接
// 发送邮件
javaMailSender.send(message);
System.out.println("发送成功");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
package com.sgz.springboot.service.impl;
import com.sgz.springboot.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
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 SendMailServiceImpl implements SendMailService {
@Autowired
private JavaMailSender javaMailSender;
// 发送人
private String from = "发件人邮箱";
// 接收人
private String to = "收件人邮箱";
// 标题
private String subject = "测试邮件标题";
// 正文
private String context = "测试邮件内容";
@Override
public void sendMail() {
// 附件邮件
try {
// 创建一个简单邮件
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true); // true:发送附件时需要添加
// 设置发件人
helper.setFrom(from+"(发件人名称)"); // 可以设置发件人名称,要用英文的() message.setFrom(from+"(发件人名称)");
// 设置收件人
helper.setTo(to);
// 设置发送的标题
helper.setSubject(subject);
// 设置发送的内容
helper.setText(context);
// 添加附件
File file1 = new File("D:\\Java\\SpringBoot\\daily_demo02\\day76_springboot_mail\\target\\day76_springboot_mail-0.0.1-SNAPSHOT.jar");
File file2 = new File("D:\\Java\\SpringBoot\\daily_demo02\\day76_springboot_mail\\src\\main\\resources\\8.png");
// 添加附件到邮件
helper.addAttachment(file1.getName(), file1);
helper.addAttachment("最好看的壁纸.png", file2);
// 发送邮件
javaMailSender.send(message);
System.out.println("发送成功");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}