이메일 전송
1. 개발환경
- Spring 4.0.3
- Maven
- JavaConfig
2. pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 | < dependencies > < dependency > < groupid >org.springframework</ groupid > < artifactid >spring-support</ artifactid > < version >2.0.8</ version > </ dependency > < dependency > < groupid >javax.mail</ groupid > < artifactid >mail</ artifactid > < version >1.4.7</ version > </ dependency > </ dependencies > |
3. MailSender 설정(javaConfig)
config.xml
1 2 3 4 5 6 7 8 9 10 | <!--?xml version="1.0" encoding="UTF-8"?--> < properties > < entry key = "mail.host" >smtp.gmail.com</ entry > < entry key = "mail.port" >25</ entry > < entry key = "mail.encoding" >UTF-8</ entry > < entry key = "mail.username" >이메일 주소</ entry > < entry key = "mail.password" >이메일 비밀번호</ entry > < entry key = "mail.smtp.auth" >true</ entry > </ properties > |
javaConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import com.woniper.service.MailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Service; @Value ( "${mail.host}" ) private String mailHost; @Value ( "${mail.port}" ) private int mailPort; @Value ( "${mail.username}" ) private String mailUserName; @Value ( "${mail.password}" ) private String mailPassword; @Value ( "${mail.encoding}" ) private String mailEncoding; @Value ( "${mail.smtp.auth}" ) private boolean smtpAuth; @Bean public MailSender javaMailSender() { JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setHost(mailHost); javaMailSender.setPort(mailPort); javaMailSender.setUsername(mailUserName); javaMailSender.setPassword(mailPassword); javaMailSender.setDefaultEncoding(mailEncoding); Properties properties = new Properties(); properties.put( "mail.smtp.auth" , smtpAuth); properties.put( "mail.smtp.starttls.enable" , smtpAuth); javaMailSender.setJavaMailProperties(properties); return javaMailSender; } |
4. Service 구현
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import com.woniper.service.MailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Service; /** * Created by woniper on 2014. 6. 20.. */ @Service public class MailServiceImpl implements MailService { @Autowired private MailSender mailSender; @Override public void sendMail(String to, String subject, String msg){ SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(msg); mailSender.send(message); } } |
5. Service 사용(Controller)
1 2 3 4 5 6 7 8 9 10 11 | import com.woniper.service.MailService; @Controller public class MailController { @Autowired private MailService mailService; @RequestMapping (value= "/email" ) public @ResponseBody void emailTest() throws Exception { mailService.sendMail( "lkw1989@naver.com" , "mail test title" , "mail test Contents" ); } } |
출처 : https://blog.woniper.net/215?category=699184
'Spring Framework > Spring Core' 카테고리의 다른 글
EHCache (ehcache-spring-annotations, ehcache.xml, @Cacheable) (0) | 2021.03.22 |
---|---|
스프링프레임워크로 카카오 챗봇 만들기 (0) | 2021.03.22 |
HandlerMethodArgumentResolverComposite Class Composite 패턴 (0) | 2020.09.03 |
HATEOAS 사용하기 (0) | 2020.09.01 |
XML 스키마 기반의 POJO 클래스를 이용한 AOP 구현 (0) | 2020.07.06 |
[Spring] AOP 개요 (0) | 2020.07.06 |
[Spring] Locale 처리 (0) | 2020.07.06 |
[Spring] ViewResolver 설정 (0) | 2020.07.06 |