连续发送了四封邮件:普通文本邮件,带附件的邮件,内容包含图片的邮件,发送html邮件。
普通文本邮件截图
带附件的邮件截图
内容包含图片的邮件截图(图片太大,就截取一部分)
发送html邮件截图
- 引入pom文件依赖
<!-- 邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 在application.properties 中添加邮箱配置
spring.mail.host=smtp.qq.com
spring.mail.port=587
spring.mail.username=jackdking@foxmail.com
spring.mail.password=邮箱授权码,非邮箱登入密码
-
from,即为邮件发送者,一般设置在配置文件中
-
to,邮件接收者,此参数可以为数组,同时发送多人
-
subject,邮件主题
-
content,邮件的主体
-
邮件发送者 from 一般采用固定的形式写到配置文件中。
-
在qq邮箱中开启收发邮件步骤
-
进入邮件开启页面
-
点击开启,并发送短信
-
确认发送,邮件收发开启
-
- MailServiceImpl类注入邮件API类
/**
* @author jackdking
* @date 2018/5/3 22:07
*/
@Component
public class MailServiceImpl implements IMailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String mailFrom;
......
- 4种邮件类型方法
/**
* 发送简单邮件
*
* @param to
* @param subject
* @param content
*/
@Override
public void sendSimpleEmail(String to,String subject,String content) {
SimpleMailMessage message = new SimpleMailMessage();
subject="简单文本邮件";
content="你好,我是空白";
to = "jackdking@foxmail.com";//我自己的邮箱
message.setFrom(mailFrom);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
/**
* 发送html邮件
*
* @param to
* @param subject
* @param content
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
subject="发送html邮件";
content="<html>\n" +
"<body>\n" +
" <h3>hello world !你好,我是空白 ,发送html邮件!</h3>\n" +
"</body>\n" +
"</html>";
to = "jackdking@foxmail.com";//我自己的邮箱
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(mailFrom);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送带附件的邮件
*
* @param to
* @param subject
* @param content
* @param filepath
*/
@Override
public void sendFileMail(String to, String subject, String content, String filepath) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
subject="发送带附件的邮件";
content="你好,我是空白";
to = "jackdking@foxmail.com";//我自己的邮箱
filepath="D:\\微信图片_20200524230149.jpg";
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(mailFrom);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
FileSystemResource file = new FileSystemResource(new File(filepath));
String fileName = filepath.substring(filepath.lastIndexOf(File.separator));
helper.addAttachment(fileName,file);
mailSender.send(mimeMessage);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void sendPictureMail(String to, String subject, String content, String picturepath) {
// TODO Auto-generated method stub
String Id = "jackdking1314";
content="<html><body>图片邮件:<img src=\‘cid:" + Id + "\‘ ></body></html>";
String imgPath = "D:\\微信图片_20200524230149.jpg";
to = "jackdking@foxmail.com";//我自己的邮箱
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailFrom);
helper.setTo(to);
helper.setSubject("这是有图片的邮件");
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(imgPath));
helper.addInline(Id, res);
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
- SpringbootMailApplication应用启动类实现了ApplicationRunner接口,应用启动成功就执行run方法,发送4种邮件。
@SpringBootApplication
public class SpringbootMailApplication implements ApplicationRunner
{
@Autowired
IMailService mailService;
public static void main( String[] args )
{
SpringApplication.run(SpringbootMailApplication.class, args);
}
//启动应用后直接发送邮件
@Override
public void run(ApplicationArguments args) throws Exception {
// TODO Auto-generated method stub
mailService.sendSimpleEmail(null, null, null);
mailService.sendHtmlMail(null, null, null);
mailService.sendFileMail(null, null, null,null);
mailService.sendPictureMail(null, null, null,null);
}
}
- 应用启动成功,并成功发送了4封邮件
完整的demo项目,请关注公众号“前沿科技bot“并发送"邮件系统"获取。