使用spring-boot-starter-mail發送郵件html
<?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.wangxh.email</groupId> <artifactId>email-study</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>email-study</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.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-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties:java
#郵件發送人配置 spring.mail.host=********* spring.mail.username=******** spring.mail.password=******* spring.mail.port=***** spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
username和passowrd是發送郵件的帳戶;web
host:是發送郵件的服務器,不是收件服務器spring
這裏能夠經過foxmail查看:apache
好比發送的是163帳號那麼配置以下:服務器
spring.mail.host=smtp.163.comapp
spring.mail.port=25maven
package com.wangxh.email.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.mail.MailProperties; 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.stereotype.Component; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @Component public class EmailService { @Autowired private JavaMailSender javaMailSender; @Autowired MailProperties mailProperties; /** * 發送純文本郵件 * * @param text 郵件文本 * @param to 接收人的郵箱 * @param subject 郵件主題 */ public void sendTextEmail(String text, String to, String subject) { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setSubject(subject); simpleMailMessage.setText(text); simpleMailMessage.setFrom(mailProperties.getUsername()); simpleMailMessage.setTo(to); javaMailSender.send(simpleMailMessage); } /** * 發送網頁郵件 * * @param html 網頁 * @param to 接收人的郵箱 * @param subject 郵件主題 */ public void sendHtmlEmail(String html, String to, String subject) { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(mailProperties.getUsername()); helper.setTo(to); helper.setSubject(subject); // "<html><body><img src='http://baidu.com/2/1.jpg' ></body></html>" helper.setText(html, true); } catch (MessagingException e) { e.printStackTrace(); } javaMailSender.send(mimeMessage); } /** * 發送附件的郵件 * * @param to 接收人的郵箱 * @param subject 郵件主題 * @param text 郵件文本 * @param files 附件的文件地址 c:xxx.jpg */ public void sendAttachmentsMail(String to, String subject,String text, String... files) { try { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(mailProperties.getUsername()); helper.setTo(to); helper.setSubject(subject); helper.setText(text); if (files!=null){ for (String f:files){ FileSystemResource file = new FileSystemResource(new File(f)); helper.addAttachment("附件-1.jpg", file); helper.addAttachment("附件-2.jpg", file); } } javaMailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } finally { } } }