티스토리 뷰

728x90
반응형

개발 환경

java 21.0.4 2024-07-16 LTSJava(TM) SE Runtime Environment (build 21.0.4+8-LTS-274)

Java HotSpot(TM) 64-Bit Server VM (build 21.0.4+8-LTS-274, mixed mode, sharing)

 

Tomcatv 10.1

EclipseVersion: 2024-06 (4.32.0)Build id: 20240606-1231

Spring Framework: 6.1.11


pom.xml

<!-- https://mvnrepository.com/artifact/com.sun.mail/jakarta.mail -->
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>jakarta.mail</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>6.1.11</version>
</dependency>

MailConfig.java

package com.email.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

@Configuration
public class MailConfig {

    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.gmail.com");
        mailSender.setPort(587);
        
        mailSender.setUsername("your email address");
        mailSender.setPassword("your password");
        
        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.debug", "true");
        
        return mailSender;
    }
    
}

MailService.java

package com.email.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;

@Service
public class MailService {
    
	@Autowired
	@Qualifier("javaMailSender")
    private JavaMailSender javaMailSender;


    public void sendEmail(String to, String subject, String text) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text, true); // HTML 내용 전송 가능

        javaMailSender.send(message);
    }
    
}

EmailManagerController.java

package com.email.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.email.service.MailService;

import jakarta.mail.MessagingException;



@RestController
public class EmailManagerController {
	@Autowired
	@Qualifier("mailService")
	private MailService mailService;

	@GetMapping("/emailSend")
	public ModelAndView emailSend() {
		ModelAndView modelAndView = new ModelAndView("emailSend");

		// 이메일 속성 설정
		try {
			mailService.sendEmail("jjh5400823@naver.com", "test mail send", "<p style='color:red;'>이메일 전송 테스트를 위한 메일입니다.</p>");
			modelAndView.addObject("message", "이메일이 성공적으로 전송되었습니다.");
		} catch (MessagingException e) {
			e.printStackTrace();
			modelAndView.addObject("message", "이메일 전송 중 오류가 발생했습니다: " + e.getMessage());
		}

		return modelAndView;
	}

}

추가 정보

구글 메일을 서버를 사용하는 경우 "AuthenticationFailedException" 이 발생할 수 있다.

이런 경우 구글에서 "구글 앱 비밀번호"를 검색한 후 생성한 12자리 비빌 번호를 그대로 사용하면 된다.

728x90
반응형