ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] javax.mail 를 통한 Email 보내기
    IT개발이야기 2023. 1. 12. 15:48
    728x90
    반응형
    SMALL

    javax.mail 라이브러리를 통해 Email 보내기를 하였습니다.

     

    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
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    @Override
        public String sendMail(VO vo) throws Exception {
            final String bodyEncoding = "UTF-8"//콘텐츠 인코딩
            String fromEmail = "";         // 보내는사람이메일
            String fromUsername = "";     // 보내는사람명
            String toEmail = "";        // 받는사람이메일 (콤마 구분)
            String subject = "";        // 제목
            String _html = "";            // 내용
     
            String mailDesc = _html;
            StringBuffer sb = new StringBuffer();
            sb.append(mailDesc);
            String html = sb.toString();
     
            // 메일 옵션 설정
            Properties props = new Properties();
            props.put("mail.transport.protocol"""); // Protocol
            props.put("mail.smtp.host""");    // host
            props.put("mail.smtp.port""");    // port
            props.put("mail.smtp.auth""false");
            props.put("mail.smtp.quitwait""false");
            props.put("mail.smtp.socketFactory.class""javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback""true");
     
            try {
     
                // 메일 세션 생성
                Session session = javax.mail.Session.getInstance(props, null);
     
                // 메일 송/수신 옵션 설정
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(fromEmail, fromUsername));
                message.setRecipients(RecipientType.TO, InternetAddress.parse(toEmail, false));
                message.setSubject(subject);
                message.setSentDate(new Date());
     
                // 메일 콘텐츠 설정
                Multipart mParts = new MimeMultipart();
                MimeBodyPart mTextPart = new MimeBodyPart();
     
                // 메일 콘텐츠 - 내용
                mTextPart.setText(html, bodyEncoding, "html");
                mParts.addBodyPart(mTextPart);
     
                // 메일 콘텐츠 설정
                message.setContent(mParts);
     
                // MIME 타입 설정
                MailcapCommandMap MailcapCmdMap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
                MailcapCmdMap.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
                MailcapCmdMap.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
                MailcapCmdMap.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
                MailcapCmdMap.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
                MailcapCmdMap.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
                CommandMap.setDefaultCommandMap(MailcapCmdMap);
     
                // 메일 발송
                Transport.send( message );
            } catch ( Exception e ) {
                e.printStackTrace();
            }
     
            return "SUCCESS";
        }
    cs
    728x90
    반응형
    LIST

    댓글

Designed by Tistory.