这篇文章介绍springboot的邮件发送。
由于很简单就没有分出server和imp之类,只是在controller简单写个方法进行测试。
首先pom文件加入spring-boot-starter-mail,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dalaoyang</groupId> <artifactId>springboot_mail</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_mail</name> <description>springboot_mail</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
接下来是application.properties,里面包含发送邮箱的地址,账号密码,接信地址,由于只是测试,所以都是固定写死的,如果项目中需要,可以根据自行情况进行修改。
##端口号 server.port=8888 ##这里根据自己的情况填写 ##邮箱服务器地址 ##QQ smtp.qq.com ##sina smtp.sina.cn ##aliyun smtp.aliyun.com ##163 smtp.163.com spring.mail.host=smtp.aliyun.com ##邮箱用户名 spring.mail.username=dalaoyang@aliyun.com ##邮箱密码(注意:qq邮箱应该使用独立密码,去qq邮箱设置里面获取) spring.mail.password=****** ##编码格式 spring.mail.default-encoding=UTF-8 ##发送邮件地址 mail.fromMail.sender=dalaoyang@aliyun.com ##接收邮件地址 mail.fromMail.receiver=yangyang@dalaoyang.cn
然后先展示一下controller的完整代码:
package com.dalaoyang.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.controller * @email yangyang@dalaoyang.cn * @date 2018/4/10 */ @RestController public class MailController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Value("${mail.fromMail.sender}") private String sender; @Value("${mail.fromMail.receiver}") private String receiver; @Autowired private JavaMailSender javaMailSender; /* * * @Description http://localhost:8888/sendMail * @author dalaoyang * @email 397600342@qq.com * @method 发送文本邮件 * @date * @param * @return */ @RequestMapping("/sendMail") public String sendMail() { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(sender); message.setTo(receiver); message.setSubject("大老杨"); message.setText("你好你好你好!"); try { javaMailSender.send(message); logger.info("简单邮件已经发送。"); } catch (Exception e) { logger.error("发送简单邮件时发生异常!", e); } return "success"; } /* * * @Description http://localhost:8888/sendHtmlMail * @author dalaoyang * @email 397600342@qq.com * @method 发送html邮件 * @date * @param * @return */ @RequestMapping("/sendHtmlMail") public String testHtmlMail() { String content="<html>\n" + "<body>\n" + " <h3>hello world ! 这是一封Html邮件!</h3>\n" + "</body>\n" + "</html>"; MimeMessage message = javaMailSender.createMimeMessage(); try { //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("html mail"); helper.setText(content, true); javaMailSender.send(message); logger.info("html邮件发送成功"); } catch (MessagingException e) { logger.error("发送html邮件时发生异常!", e); } return "success"; } /* * * @Description http://localhost:8888/sendFilesMail * @author dalaoyang * @email 397600342@qq.com * @method 发送附件邮件 * @date * @param * @return */ @RequestMapping("/sendFilesMail") public String sendFilesMail() { String filePath="/Users/dalaoyang/Downloads/article_tag.sql"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("附件邮件"); helper.setText("这是一封带附件的邮件", true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); javaMailSender.send(message); logger.info("带附件的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送带附件的邮件时发生异常!", e); } return "success"; } /* * * @Description http://localhost:8888/sendInlineResourceMail * @author dalaoyang * @email 397600342@qq.com * @method 发送图片邮件 * @date * @param * @return */ @RequestMapping("/sendInlineResourceMail") public String sendInlineResourceMail() { String Id = "dalaoyang12138"; String content="<html><body>这是有图片的邮件:<img src=\'cid:" + Id + "\' ></body></html>"; String imgPath = "/Users/dalaoyang/Downloads/dalaoyang.jpeg"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("这是有图片的邮件"); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(imgPath)); helper.addInline(Id, res); javaMailSender.send(message); logger.info("嵌入静态资源的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送嵌入静态资源的邮件时发生异常!", e); } return "success"; } }
然后一点一点来,本文分4种情况:
1.发送简单的文本邮件
/* * * @Description http://localhost:8888/sendMail * @author dalaoyang * @email yangyang@dalaoyang.cn * @method 发送文本邮件 * @date * @param * @return */ @RequestMapping("/sendMail") public String sendMail() { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(sender); message.setTo(receiver); message.setSubject("大老杨"); message.setText("你好你好你好!"); try { javaMailSender.send(message); logger.info("简单邮件已经发送。"); } catch (Exception e) { logger.error("发送简单邮件时发生异常!", e); } return "success"; }
访问http://localhost:8888/sendMail,可以看到页面上显示success,在去看邮箱可以看到如下图,证明发送成功
2.发送html邮件
/* * * @Description http://localhost:8888/sendHtmlMail * @author dalaoyang * @email yangyang@dalaoyang.cn * @method 发送html邮件 * @date * @param * @return */ @RequestMapping("/sendHtmlMail") public String testHtmlMail() { String content="<html>\n" + "<body>\n" + " <h3>hello world ! 这是一封Html邮件!</h3>\n" + "</body>\n" + "</html>"; MimeMessage message = javaMailSender.createMimeMessage(); try { //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("html mail"); helper.setText(content, true); javaMailSender.send(message); logger.info("html邮件发送成功"); } catch (MessagingException e) { logger.error("发送html邮件时发生异常!", e); } return "success"; }
访问http://localhost:8888/sendHtmlMail,可以看到页面上显示success,在去看邮箱可以看到如下图,证明发送成功
3.发送附件邮件
/* * * @Description http://localhost:8888/sendFilesMail * @author dalaoyang * @email yangyang@dalaoyang.cn * @method 发送附件邮件 * @date * @param * @return */ @RequestMapping("/sendFilesMail") public String sendFilesMail() { String filePath="/Users/dalaoyang/Downloads/article_tag.sql"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("附件邮件"); helper.setText("这是一封带附件的邮件", true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); javaMailSender.send(message); logger.info("带附件的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送带附件的邮件时发生异常!", e); } return "success"; }
访问http://localhost:8888/sendFilesMail,可以看到页面上显示success,在去看邮箱可以看到如下图,证明发送成功
4.发送图片邮件
/* * * @Description http://localhost:8888/sendInlineResourceMail * @author dalaoyang * @email yangyang@dalaoyang.cn * @method 发送图片邮件 * @date * @param * @return */ @RequestMapping("/sendInlineResourceMail") public String sendInlineResourceMail() { String Id = "dalaoyang12138"; String content="<html><body>这是有图片的邮件:<img src=\'cid:" + Id + "\' ></body></html>"; String imgPath = "/Users/dalaoyang/Downloads/dalaoyang.jpeg"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("这是有图片的邮件"); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(imgPath)); helper.addInline(Id, res); javaMailSender.send(message); logger.info("嵌入静态资源的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送嵌入静态资源的邮件时发生异常!", e); } return "success"; }
访问http://localhost:8888/sendInlineResourceMail,可以看到页面上显示success,在去看邮箱可以看到如下图,证明发送成功
热门评论
springboot支出发送邮件带PDF附件吗
nice