--- title: 'Java mail' disqus: hackmd --- ###### tags: `SpringBoot` `email` Java mail === [TOC] ## 筆記目的 讓人可以透過spring boot去寄送email 當然常用的大概會有以下幾種,但本文就以最簡單的幾種作為範例  * [參考自spring mail筆記](https://sites.google.com/im.fju.edu.tw/web/spring-framework-web/spring-mail#h.p_0sRi8gR978a1) ## 程式碼部分 ### 專案架構  請忽略TestMail這個java檔案 ### pom.xml ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` ``` <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.3</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <name>helloworld</name> <description>Demo project for Spring Boot</description> <properties> <java.version>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-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project> ``` ### application.properties ```java= spring.mail.host = smtp.gmail.com spring.mail.username = xxxx@gmail.com spring.mail.password = xxxx send.from.email= xxxx@gmail.com spring.mail.properties.mail.smtp.auth = true; spring.mail.properties.mail.smtp.starttls.enable = true spring.mail.properties.mail.smtp.ssl.enable = true spring.mail.properties.mail.socketFactory.port=587 spring.mail.properties.mail.socketFactory.class=javax.net.ssl.SSLSocketFactory spring.mail.properties.mail.socketFactory.fallback=false spring.mail.smtp.port= 587 ``` ### Controller ```java= package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.example.demo.service.MailService; @Controller public class MailController { @Autowired private MailService mailService; @GetMapping("/") @ResponseBody public String hello(){ mailService.prepareAndSend("emailyouwanttosend","Sample email from XXX"); return "Mail sent"; } } ``` > 把emailyouwanttosend替換成你收信人的信箱 把Sample email from XXX替換成你要寄信的訊息 ### Service ```java= package com.example.demo.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessagePreparator; import org.springframework.stereotype.Service; @Service public class MailService { private JavaMailSender mailSender; @Autowired public MailService(JavaMailSender mailSender) { this.mailSender = mailSender; } public void prepareAndSend(String recipient, String message) { MimeMessagePreparator messagePreparator = mimeMessage -> { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage); messageHelper.setFrom("XXXXXXXXXX@gmail.com"); messageHelper.setTo(recipient); messageHelper.setSubject(message); messageHelper.setText(message); }; try { mailSender.send(messagePreparator); //System.out.println("sent"); } catch (MailException e) { //System.out.println(e); // runtime exception; compiler will not force you to handle it } } } ``` ### JavaMailApplication(對這個class按下執行,因為他有main函數) ``` package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class JavaMailApplication { public static void main(String[] args) { SpringApplication.run(JavaMailApplication.class, args); } } ``` > 之後在瀏覽器瀏覽 > http://localhost:8080/ > > 會出現以下文字 Mail sent  會出現**Mail sent**也是因為在Controller當中我們有以下的程式碼片段 ``` @GetMapping("/") @ResponseBody public String hello(){ mailService.prepareAndSend("emailyouwanttosend","Sample email from XXX"); return "Mail sent"; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up