문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 | ||
wiki:java:메일_보내기 [2020/04/03 09:32] dhan 만듦 |
wiki:java:메일_보내기 [2023/01/13 18:44] (현재) |
||
---|---|---|---|
줄 1: | 줄 1: | ||
- | ====== | + | ====== |
<WRAP left notice 80%> | <WRAP left notice 80%> | ||
- | * description : java mail과 관련된 내용 기술 | + | * description : |
* author | * author | ||
* email : hylee@repia.com | * email : hylee@repia.com | ||
- | * lastupdate | + | * lastupdate |
</ | </ | ||
- | < | + | < |
+ | |||
+ | ===== 시작 전 확인사항 ===== | ||
+ | > 메일발송 시작 전 보낼메일 계정이 smtp 로 설정이 되어 있는지 확인해야 함. | ||
+ | > 네이버, 구글, outLook 계정 설정이 각각 다르기 때문에 찾아보고 해야함. | ||
+ | > 검색하면 무수히 많은 정보가 있으니 참고 하면 됨. | ||
+ | |||
+ | ===== globals.properties (설정) ===== | ||
+ | <code java> | ||
+ | # | ||
+ | # 보내는 메일 | ||
+ | mail.senderId = ${보내는 사람 메일 주소} | ||
+ | # 메일 서버 | ||
+ | mail.host = ${메일 서버} | ||
+ | # 메일 서버 port | ||
+ | mail.port = ${메일 서버 포트, 일반적으로 25} | ||
+ | # 받는 메일 | ||
+ | mail.addressee = ${받는 사람 메일 주소} | ||
+ | </ | ||
+ | |||
+ | ===== 메일 계정 등록하는 방법 ===== | ||
+ | |||
+ | <code java> | ||
+ | |||
+ | //' | ||
+ | public static void mailSender(String consultTitle , String htmlDesc) throws AddressException, | ||
+ | String senderId = EgovProperties.getProperty(" | ||
+ | String host = EgovProperties.getProperty(" | ||
+ | String port = EgovProperties.getProperty(" | ||
+ | String addressee = EgovProperties.getProperty(" | ||
+ | |||
+ | try { | ||
+ | Properties props = new Properties(); | ||
+ | |||
+ | //SMTP 서버정보설정 | ||
+ | props.put(" | ||
+ | props.put(" | ||
+ | props.put(" | ||
+ | |||
+ | //SMTP 서버정보를 session에 담는다. | ||
+ | Session session = Session.getInstance(props); | ||
+ | |||
+ | // | ||
+ | InternetAddress fromAddress = new InternetAddress(senderId); | ||
+ | |||
+ | // | ||
+ | MimeMessage message = new MimeMessage(session); | ||
+ | |||
+ | // | ||
+ | message.setFrom(fromAddress); | ||
+ | |||
+ | |||
+ | // 내용 인코딩이 UTF-8일 떄 아래처럼 제목 인코딩도 ' | ||
+ | // ex) message.setSubject(MimeUtility.encodeText(consultTitle, | ||
+ | // 위처럼 ' | ||
+ | // ex) catch (MessagingException | UnsupportedEncodingException eME) | ||
+ | |||
+ | // 제목 | ||
+ | message.setSubject(consultTitle); | ||
+ | |||
+ | //내용 ," 내용 인코딩 " | ||
+ | message.setContent(htmlDesc, | ||
+ | |||
+ | // | ||
+ | // | ||
+ | // | ||
+ | message.addHeader(" | ||
+ | |||
+ | //받는 메일 | ||
+ | InternetAddress toAddress = new InternetAddress(addressee); | ||
+ | message.addRecipient(Message.RecipientType.TO, | ||
+ | |||
+ | //위에 정보를 담은 message를 Transport.send()로 메일보낸다. | ||
+ | Transport.send(message); | ||
+ | } | ||
+ | catch (MessagingException eME) | ||
+ | { | ||
+ | Logger.error(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | === 중요 내용 === | ||
+ | > 내용 인코딩이 UTF-8일 떄 제목 인코딩도 아래처럼 선언해야한다. | ||
+ | > ex) message.setSubject(MimeUtility.encodeText(consultTitle, | ||
+ | > ' | ||
+ | > ex) catch (MessagingException | UnsupportedEncodingException eME) | ||
+ | |||
+ | === 소스 출처 === | ||
+ | > 위 소스는 내부 프로젝트 3곳에서 검증이 되었습니다. | ||
+ | > K * * (기관), 코 * * * * (기업), H * * * * (대학) 에 모두 적용된 것입니다. | ||
===== Tip ===== | ===== Tip ===== | ||
===== Troubleshooting ===== | ===== Troubleshooting ===== | ||
+ | |||
+ | ===== Ref ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
- | ===== Ref ===== | ||
- | [[https:// | ||
- | {{tag> | + | {{tag>주레피 |