springboot如何发送邮件呢?
发送邮件是我们日常开发中,经常使用的功能,那么如何才能发送邮件呢?下文笔者将一一道来,如下所示
1.引入相应的maven依赖 2.编写相应的配置文件 3.调用相应的工具类发送邮件例:SpringBoot发送邮件(带附件)的示例分享
引入maven <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> yml配置 spring: mail: host: smtp.163.com #邮件服务器地址,邮箱不一样,服务器地址不一样 username: #邮箱用户名 password: #一般使用授权码 properties: 'mail.smtp.ssl.enable': 'true' #设置安全连接 邮件发送附件实体类 MailAttachmentDto.java import lombok.Data; import lombok.experimental.Accessors; @Data @Accessors(chain = true) public class MailAttachmentDto { /** * 附件文件名 */ private String fileName; /** * 附件路径(绝对路径) */ private String filePath; } 邮件发送实体 MailDto.java import lombok.Data; import lombok.experimental.Accessors; import java.util.list; @Data @Accessors(chain = true) public class MailDto { /** * 主题 */ private String subject; /** * 邮件内容 */ private String text; /** * 收件人邮箱地址(发送给谁) */ private String to; /** * 发送人(谁发的) */ private String from; /** * 附件列表 */ private List<MailAttachmentDto> attachmentDtoList; } 发送邮件工具类 MailUtis.java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import javax.annotation.PostConstruct; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.List; @Component public class MailUtil { @Autowired private JavaMailSenderImpl javaMailSender; public static MailUtil mailUtil; @PostConstruct public void init() { mailUtil = this; mailUtil.javaMailSender = this.javaMailSender; } /** * 发送邮件(含有附件) */ public static void sendMail(MailDto mailDto) throws MessagingException { MimeMessage mimeMessage = mailUtil.javaMailSender.createMimeMessage(); MimeMessageHelper helper = null; helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject(mailDto.getSubject()); helper.setText(mailDto.getText()); //发送含有html代码的内容 //helper.setText(mailDto.getText(), true); helper.setTo(mailDto.getTo()); helper.setFrom(mailDto.getFrom()); if (!CollectionUtils.isEmpty(mailDto.getAttachmentDtoList())) { List<MailAttachmentDto> attachmentDtoList = mailDto.getAttachmentDtoList(); for (MailAttachmentDto attachmentDto : attachmentDtoList) { helper.addAttachment(attachmentDto.getFileName(), new File(attachmentDto.getFilePath())); } } mailUtil.javaMailSender.send(mimeMessage); } } //发送邮件测试 /** * 发送邮件 测试 * @return */ @PostMapping(value = "/sendMail") public String sendMail(){ MailDto dto=new MailDto(); dto.setSubject("测试邮件主题") .setText("我是测试邮件具体内容") .setTo("admin@java265.com") .setFrom("我是发送人"); List<MailAttachmentDto> attachmentDtoList=new LinkedList<>(); MailAttachmentDto attachmentDto=new MailAttachmentDto(); attachmentDto.setFileName("test.jpg") .setFilePath("D:\\test.jpg"); dto.setAttachmentDtoList(attachmentDtoList); try { MailUtil.sendMail(dto); return "success"; } catch (MessagingException e) { e.printStackTrace(); return "error"; } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。