ํญ๋ชฉ | Gmail API | Gmail SMTP |
---|---|---|
์ธ์ฆ ๋ฐฉ์ | OAuth 2.0 (access token) | ์ฑ ๋น๋ฐ๋ฒํธ |
์ฌ์ฉ ๋ฐฉ๋ฒ | Google Cloud Console ์ค์ + OAuth ์ธ์ฆ ํ๋ฆ ํ์ | ์ฑ ๋น๋ฐ๋ฒํธ ๋ฐ๊ธ๋ง์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅ |
์ฃผ์ ์ฌ์ฉ ๋ชฉ์ | Gmail ์ฝํ ์ธ ์ ๊ทผ, ๋ฉ์ผ ๊ด๋ฆฌ | ๋จ์ ๋ฉ์ผ ์ ์ก(SMTP ์ ์ก ์ ์ฉ) |
์ด ๊ธ์์๋ Gmail API(OAuth) ๋ฐฉ์์ด ์๋, SMTP ๋ฐฉ์์ผ๋ก Gmail์ ์ฐ๋ํ์ฌ ๋ฉ์ผ์ ์ ์กํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํฉ๋๋ค.
๊ตฌ๊ธ ๊ณ์ > ๋ณด์ > ์ฑ ๋น๋ฐ๋ฒํธ
"Mail API ํ
์คํธ"
// ๋ฉ์ผ ์ ์ก ๊ธฐ๋ฅ์ ์ํ ์คํํฐ
implementation 'org.springframework.boot:spring-boot-starter-mail'
## Google SMTP
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=boogiewoong2@gmail.com
spring.mail.password=**** **** **** **** # ์ฑ ๋น๋ฐ๋ฒํธ ์
๋ ฅ
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.mime.charset=UTF-8
spring.mail.properties.mail.transport.protocol=smtp
๐ ๋ณด์ ํ
application.yml
๋๋ .env
ํ์ผ๋ก ์ธ๋ถ ๋ถ๋ฆฌ.gitignore
์ค์ ํ์!package com.example.demo.C06Google;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@Slf4j
@RequestMapping("/google/mail")
public class C01GoogleMailAPIController {
@Autowired
JavaMailSender javaMailSender;
@GetMapping("/req")
@ResponseBody
public String req(@RequestParam("email") String email, @RequestParam("text") String text){
log.info("GET /google/mail/req ์์ฒญ ์์ ...");
try {
// ๋จ์ ํ
์คํธ ๋ฉ์ผ ๊ตฌ์ฑ
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(email); // ์์ ์ ์ฃผ์
message.setSubject("[WEB๋ฐ์ ] ๋ฉ์ผ ํ
์คํธ์
๋๋ค"); // ๋ฉ์ผ ์ ๋ชฉ
message.setText(text); // ๋ณธ๋ฌธ ๋ด์ฉ
javaMailSender.send(message); // ์ ์ก
return "โ
๋ฉ์ผ ์ ์ก ์ฑ๊ณต!";
} catch (Exception e) {
log.error("๋ฉ์ผ ์ ์ก ์คํจ", e);
return "โ ๋ฉ์ผ ์ ์ก ์คํจ: " + e.getMessage();
}
}
}
spring-boot-starter-mail
๊ณผ JavaMailSender
๋ฅผ ์ฌ์ฉํด Gmail์ ํตํ SMTP ๋ฉ์ผ ์ ์ก ๊ตฌํ ๊ฐ๋ฅ๐ข ๋ค์ ํธ์์๋ HTML ๋ฉ์ผ ์ ์ก / ์ฒจ๋ถํ์ผ ์ถ๊ฐ / MimeMessage ์ฌ์ฉ ์์ ๋ฅผ ๋ค๋ฃฐ ์์ ์ด์ผ!
ํ์ํ๋ฉด ์ง๊ธ ๋ฐ๋ก ์ค๋นํด์ค๊ฒ ๐
์ด์ ์ด ๋ด์ฉ์ ๋ฒจ๋ก๊ทธ์ ๋ฐ๋ก ์ฌ๋ฆด ์ ์์ด! ๐
์ธ๋ค์ผ ์ด๋ฏธ์ง๋ ๋ง๋ค์ด์ค๊น?