fairnet/src/main/java/kcc/com/cmm/EgovMultiPartEmail.java
2024-07-08 15:37:51 +09:00

176 lines
4.6 KiB
Java

package kcc.com.cmm;
import java.io.Serializable;
import java.net.MalformedURLException;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.mail.MailException;
/**
* 발송메일에 첨부파일용으로 사용되는 VO 클래스
* @author 공통서비스 개발팀 이기하
* @since 2011.12.06
* @version 1.0
* @see
*
* <pre>
* << 개정이력(Modification Information) >>
*
* 수정일 수정자 수정내용
* ---------- -------- ---------------------------
* 2011.12.06 이기하 최초 생성
* 2013.05.23 이기하 thread-safe 하게 변경
*
* </pre>
*/
public class EgovMultiPartEmail implements Serializable {
private static final long serialVersionUID = -4322006921324597283L;
private String id;
private String password;
private int port;
private String host;
private String emailAddress;
private String senderName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getSenderName() {
return senderName;
}
public void setSenderName(String senderName) {
this.senderName = senderName;
}
@Deprecated
public String send() throws EmailException {
MultiPartEmail email = new MultiPartEmail();
email.setCharset("UTF-8");
email.setHostName(this.host);
email.setSmtpPort(this.port);
email.setStartTLSEnabled(true);
email.setAuthenticator(new DefaultAuthenticator(this.id, this.password));
email.setSocketConnectionTimeout(60000);
email.setSocketTimeout(60000);
email.setFrom(this.emailAddress, this.senderName);
return email.send();
}
// Simple 메일
public String send(String addTo, String subject, String msg) throws Exception {
SimpleEmail email = new SimpleEmail();
setEmailInfo(addTo, subject, msg, email);
return email.send();
}
// HTML 메일
public String send(String addTo, String subject, String textMsg, String htmlMsg) throws MailException, MalformedURLException {
String result = "";
try {
HtmlEmail email = new HtmlEmail();
// 3번째 파라미터 'html' 의미 없음. 단 문자 없으면 error
setEmailInfo(addTo, subject, "html", email);
email.setHtmlMsg(htmlMsg);
// HTML 이메일을 지원하지 않는 클라이언트라면 다음 메세지를 뿌려웁니다
email.setTextMsg("Your email client does not support HTML messages");
// email.setTextMsg(textMsg);
// 이미지 Sample
// 삽입할 이미지와 그 Content Id를 설정합니다. URL 이미지 사용 시 메일전송 지연될 수 있음
// URL url = new URL("https://www.google.co.kr/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png");
// String cid = email.embed(url, "Apache logo");
// // HTML 메세지를 설정합니다
// email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
result = email.send();
} catch (EmailException e) {
System.out.println("EmailException Occured!!!");
}
return result;
}
// 파일첨부 메일
public String send(String addTo, String subject, String msg, EmailAttachment attachment) throws Exception {
MultiPartEmail email = new MultiPartEmail();
setEmailInfo(addTo, subject, msg, email);
if (attachment != null) {
email.attach(attachment);
}
return email.send();
}
private void setEmailInfo(String addTo, String subject, String msg, Email email) throws EmailException {
// 테스트 네이버s
email.setAuthenticator(new DefaultAuthenticator(this.id, this.password));
email.setStartTLSEnabled(true);
email.setSSLOnConnect(true);
// 테스트 네이버e
email.setCharset("UTF-8");
email.setHostName(this.host);
email.setSmtpPort(this.port);
email.setSocketConnectionTimeout(60000);
email.setSocketTimeout(60000);
email.setFrom(this.emailAddress, this.senderName);
email.addTo(addTo);
email.setSubject(subject);
email.setMsg(msg);
}
}