建立一個springboot工程,在其pom文件加入:html
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
spring.mail.host=smtp.163.com spring.mail.username=miles02@163.com spring.mail.password= spring.mail.port=25 spring.mail.protocol=smtp spring.mail.default-encoding=UTF-8
在password 中填寫本身的郵箱密碼。java
測試代碼清單以下:spring
package com.forezp; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.test.context.junit4.SpringRunner; import javax.mail.internet.MimeMessage; import java.io.File; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootJmsApplicationTests { @Test public void contextLoads() { } @Autowired private JavaMailSenderImpl mailSender; /** * 發送包含簡單文本的郵件 */ @Test public void sendTxtMail() { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); // 設置收件人,寄件人 simpleMailMessage.setTo(new String[] {"miles02@163.com"}); simpleMailMessage.setFrom("miles02@163.com"); simpleMailMessage.setSubject("Spring Boot Mail 郵件測試【文本】"); simpleMailMessage.setText("這裏是一段簡單文本。"); // 發送郵件 mailSender.send(simpleMailMessage); System.out.println("郵件已發送"); } /** * 發送包含HTML文本的郵件 * @throws Exception */ @Test public void sendHtmlMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage); mimeMessageHelper.setTo("miles02@163.com"); mimeMessageHelper.setFrom("miles02@163.com"); mimeMessageHelper.setSubject("Spring Boot Mail 郵件測試【HTML】"); StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body><h1>spring 郵件測試</h1><p>hello!this is spring mail test。</p></body>"); sb.append("</html>"); // 啓用html mimeMessageHelper.setText(sb.toString(), true); // 發送郵件 mailSender.send(mimeMessage); System.out.println("郵件已發送"); } /** * 發送包含內嵌圖片的郵件 * @throws Exception */ @Test public void sendAttachedImageMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); // multipart模式 MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setTo("miles02@163.com"); mimeMessageHelper.setFrom("miles02@163.com"); mimeMessageHelper.setSubject("Spring Boot Mail 郵件測試【圖片】"); StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body><h1>spring 郵件測試</h1><p>hello!this is spring mail test。</p>"); // cid爲固定寫法,imageId指定一個標識 sb.append("<img src=\"cid:imageId\"/></body>"); sb.append("</html>"); // 啓用html mimeMessageHelper.setText(sb.toString(), true); // 設置imageId FileSystemResource img = new FileSystemResource(new File("E:/1.jpg")); mimeMessageHelper.addInline("imageId", img); // 發送郵件 mailSender.send(mimeMessage); System.out.println("郵件已發送"); } /** * 發送包含附件的郵件 * @throws Exception */ @Test public void sendAttendedFileMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); // multipart模式 MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8"); mimeMessageHelper.setTo("miles02@163.com"); mimeMessageHelper.setFrom("miles02@163.com"); mimeMessageHelper.setSubject("Spring Boot Mail 郵件測試【附件】"); StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body><h1>spring 郵件測試</h1><p>hello!this is spring mail test。</p></body>"); sb.append("</html>"); // 啓用html mimeMessageHelper.setText(sb.toString(), true); // 設置附件 FileSystemResource img = new FileSystemResource(new File("E:/1.jpg")); mimeMessageHelper.addAttachment("image.jpg", img); // 發送郵件 mailSender.send(mimeMessage); System.out.println("郵件已發送"); } }
須要JAVASpring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼請加企鵝求求:一零三八七七四六二六 springboot