-
[JAVA] javax.mail 를 통한 Email 보내기IT개발이야기 2023. 1. 12. 15:48728x90반응형SMALL
javax.mail 라이브러리를 통해 Email 보내기를 하였습니다.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364@Overridepublic 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", ""); // Protocolprops.put("mail.smtp.host", ""); // hostprops.put("mail.smtp.port", ""); // portprops.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'IT개발이야기' 카테고리의 다른 글
[SPRINGBOOT] Gradle 설정 (0) 2023.01.13 [SPRINGBOOT] Thymeleaf 에 대해서 (개념, 활용팁) (0) 2023.01.13 [JAVA] Scheduler 와 shedlock 이야기 (0) 2023.01.11 [VUE] .vue 많이 쓰는 component 모음 (0) 2023.01.10 [VUE] SCRIPT 구성 (0) 2023.01.10