티스토리 뷰


자바 smtp 메일 전송 소스

public static HashMap sendMailJG(HashMap param) {

HashMap resultMap = new HashMap();

String smtpHost = CommonUtil.getConfig("mail.url.ip");
int smtpPort = Integer.parseInt(CommonUtil.getConfig("mail.url.port"));
Properties prop = new Properties();
prop.put("mail.smtp.host", smtpHost);
prop.put("mail.smtp.port", smtpPort);
prop.put("mail.smtp.auth", "false");

final String sender = "no-reply";
String mailAddr = CommonUtil.getConfig("mail.addr");
Session session = Session.getDefaultInstance(prop);
resultMap.put("result", "FAIL");

try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender+mailAddr, "관리자", "UTF-8"));
String[] toList = StringUtil.safe(param.get("TO")).split(">");
InternetAddress[] addArray = new InternetAddress[toList.length];

for(int i=0; i String JGUserID = toList[i]+mailAddr;
addArray[i] = new InternetAddress(JGUserID);
}

message.addRecipients(Message.RecipientType.TO, addArray);

message.setSubject(StringUtil.safe(param.get("SJ")));

String htmlBody = StringUtil.safe(param.get("HTMLBODY"));

htmlBody = htmlBody.replaceAll("'", "'");
htmlBody = htmlBody.replaceAll("<", "<");
htmlBody = htmlBody.replaceAll(">", ">");
htmlBody = htmlBody.replaceAll("/", "/");
htmlBody = htmlBody.replaceAll(""", "\"");
message.setText(htmlBody,"utf-8","html");

if(CommonUtil.getConfig("mail.use").equals("Y")){
Transport.send(message);
}else{
resultMap.put("result", "SUCESS");
return resultMap;
}
resultMap.put("result", "SUCESS");
} catch (Exception e) {
logger.debug("[sendMail]===================================== :: [catch]"+e.toString());
resultMap.put("result", "FAIL");
}

return resultMap;
}


위에가 메일 보내는 메소드 이다.
만약 메일 본문이 게시판 에디터를 통해서 보내는 케이스라면 에디터가 정상적인 html 엘리먼트를 ' 이런식으로 변경한다.
htmlBody = htmlBody.replaceAll("'", "'");
htmlBody = htmlBody.replaceAll("<", "<");
htmlBody = htmlBody.replaceAll(">", ">");
htmlBody = htmlBody.replaceAll("/", "/");
htmlBody = htmlBody.replaceAll(""", "\"");

이렇게 그냥 메일 보내면 html 태그가 메일에 그대로 찍혀서 이상하게 나온다.
그래서 정상적인 html 로 바꾸기 위해 위와 같은 처리를 해주었다.

HashMap mailSendMap = new HashMap();

mailSendMap.put("SJ", "메일제목");

String htmlBody = "메일본문";
mailSendMap.put("HTMLBODY", htmlBody);

mailSendMap.put("TO", JG_USER_ID);

resultMap = XmlUtil.sendMailJG(mailSendMap);
mailSendMap.put("result", resultMap.get("result"));
mailSendMap.put("resultMessage", resultMap.get("resultMessage"));

if("FAIL".equals(resultMap.get("result"))) {
mailSendMap.put("SUCCES_AT", "N");
mailSendMap.put("FAILR_RESN", resultMap.get("resultMessage"));
} else {
mailSendMap.put("SUCCES_AT", "Y");
mailSendMap.put("FAILR_RESN", resultMap.get("resultMessage"));
}

'자바(Java)' 카테고리의 다른 글

jsp 파일 다운로드  (0) 2021.05.10
jsp 파일 다운로드  (0) 2021.05.06
[java] 자바 int L  (0) 2020.09.11
자바 Math.ceil 사용 방법  (0) 2020.09.01
[java] 자바 엑셀 쓰기 Apache POI 사용 xlsx  (0) 2020.08.26
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함