728x90
1. jsp
2. java script
3. ajax 통신
4. MailDto 작성
@Builder
@Data
@RequiredArgsConstructor
@AllArgsConstructor
public class MailDto {
private String email;
private String subject;
private String contents;
}
5. MailConfig 작성(설정)
package com.example.crud.config;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
public class MailConfig {
private final JavaMailSender sender;
private final MimeMessage mimeMessage;
private final MimeMessageHelper mimeMessageHelper;
public MailConfig(JavaMailSender javaMailSender) throws MessagingException {
this.sender = javaMailSender;
this.mimeMessage = javaMailSender.createMimeMessage();
this.mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
}
public void setFrom(String fromAddress) throws MessagingException {
mimeMessageHelper.setFrom(fromAddress);
}
public void setTo(String email) throws MessagingException {
mimeMessageHelper.setTo(email);
}
public void setSubject(String subject) throws MessagingException {
mimeMessageHelper.setSubject(subject);
}
public void setText(String text, boolean useHtml) throws MessagingException {
mimeMessageHelper.setText(text, useHtml);
}
public void setAttach(String fileName, String path) throws MessagingException, IOException {
File file = new ClassPathResource(path).getFile();
FileSystemResource fsr = new FileSystemResource(file);
mimeMessageHelper.addAttachment(fileName, fsr);
}
public void setInline(String fileName, String path) throws MessagingException, IOException {
File file = new ClassPathResource(path).getFile();
FileSystemResource fileSystemResource = new FileSystemResource(file);
mimeMessageHelper.addInline(fileName, fileSystemResource);
}
public void send() {
sender.send(mimeMessage);
}
}
5. build.gradle
implementation 'org.springframework.boot:spring-boot-starter-mail:2.6.3'
6. MailService 작성
package com.example.crud.service.api;
import com.example.crud.config.MailConfig;
import com.example.crud.domain.MailDto;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RequiredArgsConstructor
@Service
public class MailService {
private final JavaMailSender javaMailSender;
/**
* 기본적으로 1명에게 보냄
* @param mailDto
* @return
*/
public void sendMail(MailDto mailDto) throws MessagingException, IOException {
MailConfig MailConfig = new MailConfig(javaMailSender);
MailConfig.setTo(mailDto.getEmail());
MailConfig.setSubject(mailDto.getSubject());
// String htmlContent = "<p>" + "메세지" + "<p> <img src='cid:google-logo'>";
String htmlContent = mailDto.getContents();
MailConfig.setText(htmlContent, true);
MailConfig.send();
}
/**
* 여러명 보내기
*/
public void sendMailToPeople(){
List<String> toUserList = new ArrayList<>();
toUserList.add("m_veloper@naver.com");
toUserList.add("수신대상@naver.com");
toUserList.add("수신대상@naver.com");
toUserList.add("수신대상@naver.com");
toUserList.add("수신대상@naver.com");
int toUserSize = toUserList.size();
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo((String[]) toUserList.toArray(new String[toUserSize]));
simpleMailMessage.setSubject("제목 샘플");
simpleMailMessage.setText("내용");
javaMailSender.send(simpleMailMessage);
}
// public void sendMailWithFiles(String email) throws MessagingException, IOException {
// MailConfig MailConfig = new MailConfig(javaMailSender);
//
// MailConfig.setTo(email);
// MailConfig.setSubject("제목");
//
// String htmlContent = "<p>" + "메세지" + "<p> <img src='cid:google-logo'>";
// MailConfig.setText(htmlContent, true);
//
// // 파일이 있다면
// MailConfig.setAttach("test.txt", "static/test.txt");
// MailConfig.setInline("google-logo", "static/google-logo.png");
// MailConfig.send();
// }
}
7. ajax통신 받는 @RestController
ex) 이메일로 랜덤 값을 받고 싶다면
1) bulid.gradle
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
2) 숫자,문자 10글자 랜덤
String generatedString = RandomStringUtils.randomAlphanumeric(10);
8. application.properties 설정
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username = gmail 아이디
spring.mail.password = gmail 비밀번호
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
(( dbelle만의 정리 부분 ))-------------------------------------------------------------------------------------------------------
//ModelAndView 페이지 경로
//path 지정경로 = location.href
@RequestMapping(path = {""})
public ModelAndView index() {
return new ModelAndView("index");
}
728x90
'BACK' 카테고리의 다른 글
[SpringBoot JSP] - xml 설정(Intellij) (0) | 2022.03.29 |
---|---|
[SpringBoot JSP] - DB 설정(Intellij) (0) | 2022.03.29 |
[SpringBoot jsp] - Intellij jsp사용할 수 있도록 SpringBoot 설정(Intellij) (0) | 2022.03.29 |
[SpringBoot JSP] OPEN API (KaKao Login) (0) | 2022.03.28 |
[SpringBoot JSP] - gmail 보내기 & 설정 (Intellij) (0) | 2022.03.28 |