ÃֽŠ°Ô½Ã±Û(JAVA)
2018.07.30 / 10:21
½ºÇÁ¸µ ºÎÆ® SMTP À̸ÞÀÏ º¸³»±â (±âÁ¸ javax.mail Æ÷ÇÔ)
summerman
Ãßõ ¼ö 235
- public static void smtp
- (
- String host, String port, String user, String password,
- String senderMail, String readerMail,
- String subject, String content
- ) throws AddressException, MessagingException
- {
- Properties props = new Properties();
- // smtp¿¡ ÇÊ¿äÇÑ ÀÎÁõºÎ
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.ssl.trust", host);
- props.put("mail.smtp.auth", "true");
- // È£½ºÆ® / Æ÷Æ®
- props.put("mail.smtp.host", host);
- if (port != null)
- {
- props.put("mail.smtp.port", port);
- props.put("mail.smtp.socketFactory.port", port);
- props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- }
- // ÀÎÁõÀ» Æ÷ÇÔÇÑ ¸Þ½ÃÁö ¸¸µé±â
- Message msg = new MimeMessage(Session.getInstance(props, new Authenticator()
- {
- public PasswordAuthentication getPasswordAuthentication()
- {
- return new PasswordAuthentication(user, password);
- }
- }));
- msg.setFrom(new InternetAddress(senderMail));
- msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(readerMail));
- msg.setSubject(subject);
- msg.setContent(content, "text/html;charset=UTF-8");
- msg.setSentDate(new Date());
- Transport.send(msg);
- }
Spring boot ·Î email º¸³»±â
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mail
- ÀÌÀ¯´Â ¸ð¸£°ÚÁö¸¸ ½ºÇÁ¸µ ³»¿¡¼µµ mailSender / JavaMailSender·Î °¥¸³´Ï´Ù.
- JavaMailSender ÀÇ °æ¿ì´Â javax.mail ÀÌ ÀÖ¾î¾ßÇÕ´Ï´Ù.¼³Á¤ÆÄÀÏ : ÇÊÀÚ´Â ¸ðµç ¿¬°áÁ¤º¸¸¦ connection.properties ¿¡ µû·Î º¸°üÇÕ´Ï´Ù.
- <!-- java mail Ãß°¡ -->
- <dependency>
- <groupId>com.sun.mail</groupId>
- <artifactId>javax.mail</artifactId>
- </dependency>
- µû¶ó¼ connection.properties ¸¦ Á÷Á¢ ¸¸µé¾î »ç¿ëÇÏ°í ÀÖ´Ù´Â ±âÁØÀ¸·Î ¼³¸í.¼³Á¤ ÆÄÀÏÀ» ¸¸µé¾îº¾´Ï´Ù.
- # Mail SMTP
- saro.mail.smtp.host: smtp ÁÖ¼Ò
- saro.mail.smtp.port: Æ÷Æ®
- saro.mail.smtp.user: À¯Àú
- saro.mail.smtp.pass: ¾ÏÈ£
- saro.mail.smtp.mail: º¸³»´Â»ç¶÷ (ÀÎÁõ ÈÄ ¾Æ¹« À̸ÞÀÏÀ̳ª º¸³¾¼öÀÖ´Ù¸é À̺κÐÀ» »¬ ¼ö ÀÖ°ÚÁÒ)
¼ºñ½º¸¦ ¸¸µé¾î º¾½Ã´Ù.
- @Configuration
- // ÇÊÀÚ°¡ connection.properties ¿¡ ¼³Á¤ÇÑ °ÍÀ» ¿¹Á¦·Î...
- @PropertySource("classpath:connection.properties")
- public class MailConfig
- {
- @Value("${saro.mail.smtp.host}")
- String host;
- @Value("${saro.mail.smtp.port}")
- String port;
- @Value("${saro.mail.smtp.user}")
- String user;
- @Value("${saro.mail.smtp.pass}")
- String pass;
- @Bean
- public JavaMailSender getMailSender()
- {
- JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
- mailSender.setUsername(user);
- mailSender.setPassword(pass);
- mailSender.setJavaMailProperties(getMailProperties());
- return mailSender;
- }
- private Properties getMailProperties()
- {
- Properties properties = new Properties();
- properties.setProperty("mail.transport.protocol", "smtp");
- properties.setProperty("mail.smtp.starttls.enable", "true");
- properties.setProperty("mail.smtp.ssl.trust", host);
- properties.setProperty("mail.smtp.host", host);
- properties.setProperty("mail.smtp.auth", "true");
- properties.setProperty("mail.smtp.port", port);
- properties.setProperty("mail.smtp.socketFactory.port", port);
- properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- return properties;
- }
- }
ÄÁÆ®·Ñ·¯¿¡¼ º¸³»±â ¿¹Á¦ÀÔ´Ï´Ù.
- @Component
- @PropertySource("classpath:connection.properties")
- public class MailService
- {
- Logger logger = LoggerFactory.getLogger(MailService.class);
- @Autowired
- JavaMailSender javaMailSender;
- @Value("${saro.mail.smtp.mail}")
- String form;
- public boolean sendMail(String to, String subject, String content)
- {
- MimeMessagePreparator preparator = new MimeMessagePreparator()
- {
- public void prepare(MimeMessage mimeMessage) throws Exception
- {
- mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
- mimeMessage.setFrom(new InternetAddress(form));
- mimeMessage.setSubject(subject);
- mimeMessage.setText(content, "utf-8", "html");
- }
- };
- try
- {
- javaMailSender.send(preparator);
- return true;
- }
- catch (MailException me)
- {
- logger.error("MailException", me);
- return false;
- }
- }
- }
±×¸®°í ½ÇÇàÇϸé : Â¥ÀÜ!! ¸ÞÀÏÀÌ º¸³»Áý´Ï´Ù.
- @Autowired
- MailService mailService;
- @RequestMapping(path="/test-mail-send")
- @ResponseBody
- String testMailSend()
- {
- boolean isSend =
- mailService.sendMail("¹Þ´ÂÀ̸ÞÀÏÁÖ¼Ò", "Á¦¸ñ", "¸ÞÀÏ ³»¿ëÀÌ´Ù. !!");
- if (isSend)
- {
- return "¸ÞÀÏÀÌ ¹ß¼ÛµÇ¾ú´Ù!!";
- }
- else
- {
- return "¸ÞÀÏ º¸³»±â ½ÇÆÐ : ·Î±× È®ÀÎ ¹Ù¶÷.!!";
- }
- }