标签:chm 失败 发送 artifact user tostring 信息 注册账号 个人
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
#邮箱环境配置
#发送邮件服务器
spring.mail.host=smtp.163.com
#发送邮件的邮箱地址
spring.mail.username=XXX@163.com
#邮箱密码
spring.mail.password=yourPassword
spring.mail.default-encoding=utf-8
spring.mail.port=465
spring.mail.protocol=smtps
spring.mail.from=${spring.mail.username}
这些配置除了spring.mail.from需要手动注入项目,其他项将自动注入,无需人为干预,贴心!如果是QQ邮箱,需要设置yourPassword为QQ官方服务授权码,自己问问度娘吧!下面是发送邮件的接口:
public interface MailService {
Boolean sendSimpleMail(String to, String subject, String content);
String sendHtmlEmail(String to, String subject, String content);
String sendAttachmentsMail(String to, String subject, String content, String filePath);
}
下面是实现类,负责创建和发送新的电子邮件消息。
@Service
@Slf4j
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.from}")
private String from;
/**
* 发送普通电子邮件
* @param to 邮件接收地址
* @param subject 邮件主题
* @param content 邮件内容
* @return 电子邮件是否发送成功
*/
@Override
public Boolean sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(from);
msg.setTo(to);
msg.setSubject(subject);
msg.setText(content);
try {
javaMailSender.send(msg);
log.info("简单邮件已经发送。" + content);
} catch (MailException ex) {
log.error("发送失败:" + ex.getMessage());
return false;
}
return true;
}
/**
* 发送HTML邮件
* @param to
* @param subject
* @param content
* @return
*/
@Override
public String sendHtmlEmail(String to, String subject, String content) {
MimeMessage message = null;
try {
message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
// 撰写HTML格式的内容
StringBuffer sb = new StringBuffer("<p style=‘color:#6db33f‘>使用Spring Boot发送HTML格式电子邮件。</p>");
helper.setText(sb.toString(), true);
javaMailSender.send(message);
return "发送成功";
} catch (Exception e) {
return e.getMessage();
}
}
/**
* 发送带附件的邮件
*
* @param to
* @param subject
* @param content
* @param filePath
*/
@Override
public String sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
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);
javaMailSender.send(message);
log.info("带附件的邮件已经发送。");
} catch (Exception e) {
log.error("发送带附件的邮件时发生异常!", e);
return "失败";
}
return "成功";
}
}
简单邮件是没有样式的,很多时候,我们希望发送的邮件内容带有样式,此时可发送HTML邮件,使用第二个函数即可满足需求。如果遇到需要为邮件插入附件的场景,请调用第三个函数。
@RestController
@RequestMapping("/sendEmail")
public class SendEmailsController {
@Autowired
private MailService mailService;
private static String to = "yyy@163.com";
@RequestMapping("/sendSimpleEmail")
@ResponseBody
public boolean sendEmail() {
return mailService.sendSimpleMail(to, "一封文本格式的邮件", "哈哈哈");
}
@RequestMapping("/sendHtmlEmail")
@ResponseBody
public String sendHtmlEmail() {
return mailService.sendHtmlEmail(to, "一封HTML格式的邮件", "哈哈哈");
}
@GetMapping(value = "/sendAttachmentsMail")
public String sendAttachmentsMail() {
return mailService.sendAttachmentsMail(to, "测试发送附件Title",
"测试发送附件 title", "C:\\Users\\20190265\\Pictures\\15718018.jpg");
}
}
关于在Spring Boot中发送电子邮件的不同姿势,大家有什么看法?欢迎留言讨论,祝各位生活愉快,工作顺利!
标签:chm 失败 发送 artifact user tostring 信息 注册账号 个人
原文地址:https://www.cnblogs.com/east7/p/13357216.html