ÃֽŠ°Ô½Ã±Û(JAVA)
2018.07.30 / 10:21

½ºÇÁ¸µ ºÎÆ® SMTP À̸ÞÀÏ º¸³»±â (±âÁ¸ javax.mail Æ÷ÇÔ)

summerman
Ãßõ ¼ö 235


  1. public static void smtp
  2. (
  3. String host, String port, String user, String password,
  4. String senderMail, String readerMail,
  5. String subject, String content
  6. ) throws AddressException, MessagingException
  7. {
  8. Properties props = new Properties();
  9. // smtp¿¡ ÇÊ¿äÇÑ ÀÎÁõºÎ
  10. props.put("mail.smtp.starttls.enable", "true");
  11. props.put("mail.smtp.ssl.trust", host);
  12. props.put("mail.smtp.auth", "true");
  13. // È£½ºÆ® / Æ÷Æ®
  14. props.put("mail.smtp.host", host);
  15. if (port != null)
  16. {
  17. props.put("mail.smtp.port", port);
  18. props.put("mail.smtp.socketFactory.port", port);
  19. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  20. }
  21. // ÀÎÁõÀ» Æ÷ÇÔÇÑ ¸Þ½ÃÁö ¸¸µé±â
  22. Message msg = new MimeMessage(Session.getInstance(props, new Authenticator()
  23. {
  24. public PasswordAuthentication getPasswordAuthentication()
  25. {
  26. return new PasswordAuthentication(user, password);
  27. }
  28. }));
  29. msg.setFrom(new InternetAddress(senderMail));
  30. msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(readerMail));
  31. msg.setSubject(subject);
  32. msg.setContent(content, "text/html;charset=UTF-8");
  33. msg.setSentDate(new Date());
  34. Transport.send(msg);
  35. }


Spring boot ·Î email º¸³»±â
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mail
- ÀÌÀ¯´Â ¸ð¸£°ÚÁö¸¸ ½ºÇÁ¸µ ³»¿¡¼­µµ mailSender / JavaMailSender·Î °¥¸³´Ï´Ù.
- JavaMailSender ÀÇ °æ¿ì´Â javax.mail ÀÌ ÀÖ¾î¾ßÇÕ´Ï´Ù.
  1. <!-- java mail Ãß°¡ -->
  2. <dependency>
  3. <groupId>com.sun.mail</groupId>
  4. <artifactId>javax.mail</artifactId>
  5. </dependency>
¼³Á¤ÆÄÀÏ : ÇÊÀÚ´Â ¸ðµç ¿¬°áÁ¤º¸¸¦ connection.properties ¿¡ µû·Î º¸°üÇÕ´Ï´Ù.
- µû¶ó¼­ connection.properties ¸¦ Á÷Á¢ ¸¸µé¾î »ç¿ëÇÏ°í ÀÖ´Ù´Â ±âÁØÀ¸·Î ¼³¸í.
  1. # Mail SMTP
  2. saro.mail.smtp.host: smtp ÁÖ¼Ò
  3. saro.mail.smtp.port: Æ÷Æ®
  4. saro.mail.smtp.user: À¯Àú
  5. saro.mail.smtp.pass: ¾ÏÈ£
  6. saro.mail.smtp.mail: º¸³»´Â»ç¶÷ (ÀÎÁõ ÈÄ ¾Æ¹« À̸ÞÀÏÀ̳ª º¸³¾¼öÀÖ´Ù¸é À̺κÐÀ» »¬ ¼ö ÀÖ°ÚÁÒ)
¼³Á¤ ÆÄÀÏÀ» ¸¸µé¾îº¾´Ï´Ù.
  1. @Configuration
  2. // ÇÊÀÚ°¡ connection.properties ¿¡ ¼³Á¤ÇÑ °ÍÀ» ¿¹Á¦·Î...
  3. @PropertySource("classpath:connection.properties")
  4. public class MailConfig
  5. {
  6. @Value("${saro.mail.smtp.host}")
  7. String host;
  8.  
  9. @Value("${saro.mail.smtp.port}")
  10. String port;
  11.  
  12. @Value("${saro.mail.smtp.user}")
  13. String user;
  14.  
  15. @Value("${saro.mail.smtp.pass}")
  16. String pass;
  17.  
  18. @Bean
  19. public JavaMailSender getMailSender()
  20. {
  21. JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
  22. mailSender.setUsername(user);
  23. mailSender.setPassword(pass);
  24. mailSender.setJavaMailProperties(getMailProperties());
  25. return mailSender;
  26. }
  27.  
  28. private Properties getMailProperties()
  29. {
  30. Properties properties = new Properties();
  31. properties.setProperty("mail.transport.protocol", "smtp");
  32. properties.setProperty("mail.smtp.starttls.enable", "true");
  33. properties.setProperty("mail.smtp.ssl.trust", host);
  34. properties.setProperty("mail.smtp.host", host);
  35. properties.setProperty("mail.smtp.auth", "true");
  36. properties.setProperty("mail.smtp.port", port);
  37. properties.setProperty("mail.smtp.socketFactory.port", port);
  38. properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  39. return properties;
  40. }
  41. }
¼­ºñ½º¸¦ ¸¸µé¾î º¾½Ã´Ù.
  1. @Component
  2. @PropertySource("classpath:connection.properties")
  3. public class MailService
  4. {
  5. Logger logger = LoggerFactory.getLogger(MailService.class);
  6. @Autowired
  7. JavaMailSender javaMailSender;
  8. @Value("${saro.mail.smtp.mail}")
  9. String form;
  10. public boolean sendMail(String to, String subject, String content)
  11. {
  12. MimeMessagePreparator preparator = new MimeMessagePreparator()
  13. {
  14. public void prepare(MimeMessage mimeMessage) throws Exception
  15. {
  16. mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
  17. mimeMessage.setFrom(new InternetAddress(form));
  18. mimeMessage.setSubject(subject);
  19. mimeMessage.setText(content, "utf-8", "html");
  20. }
  21. };
  22.  
  23. try
  24. {
  25. javaMailSender.send(preparator);
  26. return true;
  27. }
  28. catch (MailException me)
  29. {
  30. logger.error("MailException", me);
  31. return false;
  32. }
  33. }
  34. }
ÄÁÆ®·Ñ·¯¿¡¼­ º¸³»±â ¿¹Á¦ÀÔ´Ï´Ù.
  1. @Autowired
  2. MailService mailService;
  3.  
  4. @RequestMapping(path="/test-mail-send")
  5. @ResponseBody
  6. String testMailSend()
  7. {
  8. boolean isSend =
  9. mailService.sendMail("¹Þ´ÂÀ̸ÞÀÏÁÖ¼Ò", "Á¦¸ñ", "¸ÞÀÏ ³»¿ëÀÌ´Ù. !!");
  10. if (isSend)
  11. {
  12. return "¸ÞÀÏÀÌ ¹ß¼ÛµÇ¾ú´Ù!!";
  13. }
  14. else
  15. {
  16. return "¸ÞÀÏ º¸³»±â ½ÇÆÐ : ·Î±× È®ÀÎ ¹Ù¶÷.!!";
  17. }
  18. }
±×¸®°í ½ÇÇàÇϸé : Â¥ÀÜ!! ¸ÞÀÏÀÌ º¸³»Áý´Ï´Ù.