EJB(xml/mail/jms/jdbc)
2015.06.16 / 23:57

JavaMail API - SMTP ¸ÞÀÏ - SSL / TLS

¸Þ¸®¾ß½º
Ãßõ ¼ö 209

JavaMail API¸¦ ÀÌ¿ëÇÏ¿© SMTP¸¦ ±¸ÇöÇØ º¸µµ·Ï ÇÑ´Ù.


JavaMailÀº ÀÌ¹Ì ³Î¸® »ç¿ëµÇ°í ÀÖ´Â À̸ÞÀÏÀÇ °³³äÀ» ¸ðµÎ Ŭ·¡½ºÈ­ÇÏ¿© Á¦°øÇÑ´Ù.

SMTP¸¦ ±¸ÇöÇϱâ À§Çؼ­´Â

1. Session

2. Transport

3. Message

4. ÇÁ·ÎÆÛƼ ¼³Á¤

5. SSL/TLS, Plain, Starttls

À§ ´Ù¼¸ °¡Áö Á¤µµ¸¦ ¾Ë¾Æ¾ßÇÑ´Ù.


¿ì¼± Plain Áï Æò¹®¿¡ ´ëÇؼ­ ¾î¶² ¾Ïȣȭµµ ÇÏÁö ¾ÊÀº »óÅ·Π¸ÞÀÏÀ» º¸³¾¶§¿¡ ´ëÇؼ­´Â °í·ÁÇÏÁö ¾Ê°Ú´Ù. º¸¾È ü³ÎÀ» ÀÌ¿ëÇÑ ¸ÞÀÏ Àü¼ÛÀº SSL/TLS¸¦ ÀÌ¿ëÇÑ ¾Ï½ÃÀû º¸¾È°ú Starttls¸¦ ÀÌ¿ëÇÑ ¸í½ÃÀû º¸¾ÈÀÌ ÀÖ´Ù.(SSL/TLS ÂüÁ¶)

Starttls¸¦ ÀÌ¿ëÇÑ ¸ÞÀÏ Àü¼ÛÀº ¾Æ·¡¿Í °°´Ù.

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
public final class SmtpTlsstartImplementation {
 
    public void send(String host, String port, String username, String password) {
        props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.ssl.trust", host);
         
        session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        session.setDebug(true); //for debug
         
        Message mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress("from@cloudstick.co.kr"));
        mimeMessage.setRecipient(Message.RecipientType.TO,
                new InternetAddress("to@cloudstick.co.kr"));
        mimeMessage.setSubject("Mail subject");
        mimeMessage.setText("Mail content by text");
        Transport.send(mimeMessage);
    }
}

À§ÀÇ ³»¿ë Áß¿¡¼­ mail.smtp.starttls.enableÀÌ Starttls¸¦ ÀÌ¿ëÇÑ ¸ÞÀÏ º¸³»±âÀÇ ÇÙ½ÉÀÌ´Ù. Starttls´Â JavaMail¿¡ TLS¸ðµå¸¦ ½ÃÀÛÇ϶ó°í ¸í½ÃÀûÀ¸·Î ¿äûÇÑ´Ù. Áï StarttlsÀ» È®ÀÎÇϱâ Àü±îÁö JavaMail API´Â ÇØ´ç ¸ÞÀÏÀ» ¾ÏȣȭµÇÁö ¾ÊÀº Æò¹®À¸·Î º¸³½´Ù°í °¡Á¤ÇÑ´Ù. Starttls ¼³Á¤À» È®ÀÎÇÏ¸é º¸¾È °ü·Ã ä³ÎÀ» »ý¼ºÇÏ¿© ÀÎÁõ¼­ È®ÀÎ µîÀÇ ÀÛ¾÷À» °ÅÄ£´Ù. À§ÀÇ props ¼³Á¤ Áß props.put("mail.smtp.ssl.trust", host); ¸¦ Á¦°ÅÇϸé ÀÎÁõ¼­ °ü·Ã ¿À·ù°¡ ¹ß»ýÇÑ´Ù. À̸¦ ÇØ°áÇϱâ À§Çؼ­´Â ÀÎÁõ¼­ Á¤º¸¸¦ ·ÎÄÿ¡ ÀúÀåÇØ¾ß Çϴµ¥ À̸ÞÀÏ¿¡¼­ ÀÌ·¯ÇÑ ÀÛ¾÷±îÁö´Â ÇÊ¿ä¾øÀ» µí ÇÏ´Ù.


À̾ SSL/TLSÀ» ÀÌ¿ëÇÑ SMTP ±¸ÇöÀº ¾Æ·¡¿Í °°´Ù.
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
public final class SmtpSslImplementation {
 
    public void send(String host, String port, String username, String password) {
        props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.trust", host);
         
        session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        session.setDebug(true); //for debug
         
        Message mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress("from@cloudstick.co.kr"));
        mimeMessage.setRecipient(Message.RecipientType.TO,
                new InternetAddress("to@cloudstick.co.kr"));
        mimeMessage.setSubject("Mail subject");
        mimeMessage.setText("Mail content by text");
        Transport.send(mimeMessage);
    }
}

°ÅÀÇ µ¿ÀÏÇÏÁö¸¸ props.put("mail.smtp.ssl.enable", "true"); ÀÌ ºÎºÐÀÌ ´Ù¸£´Ù. SSLÀº Secure Sockets LayerÀÇ ¾àÀÚÀÌ°í ÀÚµ¿À¸·Î º¸¾È ä³ÎÀ» »ý¼ºÇÏ¿© ¸ÞÀÏÀ» Àü¼ÛÇÑ´Ù.