package kr.co.udapsoft.common.sender;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.logger.Logger;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
import kr.co.hsnc.common.sql.WAFSQLException;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;
public class MessageSend extends Biz {
public MessageSend(HandlerStorage storage) {
super(storage);
}
public void doBiz() throws Exception {
}
public void sendEmail(String toEmail, String sendUser, String mailSubject, String subSubject, StringBuffer mailContent) throws Exception{
sendEmail( toEmail, sendUser, mailSubject, subSubject, mailContent, null);
}
/**
* 이메일 전송 메소드(Javamail 사용)
* @param user
* @param from_email
* @param to_name
* @param to_email
*/
public void sendEmail(String toEmail, String sendUser, String mailSubject, String subSubject, StringBuffer mailContent, String gubun) throws Exception{
Date today=new Date();
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
String date = formater.format(today);
// 메일서버 정보와 실제 서버의 주소를 가져온다.
RowSet smtpInfo = getSmtpInfo();
String smtpIp = smtpInfo.getRow(0).get("CHG_CODE01");
String smtpUser = smtpInfo.getRow(0).get("CHG_CODE02");
String smtpPw = smtpInfo.getRow(0).get("CHG_CODE03");
String link = smtpInfo.getRow(0).get("CHG_CODE04");
String linkPortal = smtpInfo.getRow(0).get("CHG_CODE05");
sendUser = smtpInfo.getRow(0).get("CHG_CODE06");
/*
RowSet userInfo = getUserInfo(sendUser);
String fromEmail = userInfo.getRow(0).get("E_MAIL");
String fromName = userInfo.getRow(0).get("USER_NM");
String fromDuty = userInfo.getRow(0).get("A_DUTY");
String fromDept = userInfo.getRow(0).get("DEPART_DEPT_NM");
String fromTel = userInfo.getRow(0).get("TEL_NO");
*/
Properties properties = new Properties(); // Properties 객체 설정
properties.put("mail.transport.protocol", "smtp"); // SMTP 프로토콜 사용
properties.put("mail.smtp.host", smtpIp); // 메일서버 주소 설정
properties.put("mail.smtp.port", "25"); // 메일서버 포트 설정
properties.put("mail.smtp.auth", true); // 인증 사용 여부
SmtpAuth auth = new SmtpAuth(smtpUser, smtpPw); // SMTP 인증을 위해서 반드시 필요하다
StringBuffer contentBase = new StringBuffer();
String[] email_domain = toEmail.split("@");
if(gubun == null){
if("HOTMAIL.COM".equals(email_domain[1].toUpperCase())||"GMAIL.COM".equals(email_domain[1].toUpperCase())){
contentBase.append("
\n");
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append("
\n");
contentBase.append(" \n");
}else{
/*contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append(" :::협력업체포탈_mailing::: \n");
contentBase.append(" \n");
contentBase.append(" \n");*/
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append("
\n");
contentBase.append(" \n");
//contentBase.append(" \n");
}
// 2019.11.29 링크버튼 없는 양식 추가
}else if(gubun == "NO_LINK"){
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append("
\n");
contentBase.append(" \n");
}else{
/*contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append(" :::전자조달_mailing::: \n");
contentBase.append(" \n");
contentBase.append(" \n");*/
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append(" \n");
contentBase.append("
\n");
contentBase.append(" \n");
//contentBase.append(" \n");
}
WAFLogger.debug(contentBase);
try {
Session session = Session.getInstance(properties, auth); // Properties 객체와 auth(인증)객체로 메일전송 세션 생성
MimeMessage msg = new MimeMessage(session); // 메일 객체를 세션을 통해 생성
InternetAddress[] to_address = {new InternetAddress(toEmail)}; // 받는 사람 메일 주소 객체 생성
// msg.setFrom(new InternetAddress(sendUser)); // 보내는 사람 메일주소 설정
msg.setFrom(new InternetAddress(new String(sendUser.getBytes("euc-kr"), "8859_1")));
msg.setRecipients(Message.RecipientType.TO, to_address); // 받는 사람 메일 주소를 메일 객체에 매핑
msg.setContent(contentBase.toString(), "text/html; charset=euc-kr"); // 메일 내용을 설정하며 메일 형식을 HTML로 지정
msg.setSubject(mailSubject,"euc-kr"); // 메일 제목 설정
msg.setSentDate(new Date()); // 보내는 날짜 설정
Transport.send(msg); // 메일 전송 부분
} catch(MessagingException me) {
WAFLogger.error(this.getClass().getName() + " --> sendEmail() : \n" + me.getMessage());
WAFLogger.error(me);
//throw me;
} catch(Exception e) {
WAFLogger.error(this.getClass().getName() + " --> sendEmail() : \n" + e.getMessage());
WAFLogger.error(e);
//throw e;
}
}
/**
* 이메일 전송 메소드(Javamail 사용)
* @param user
* @param from_email
* @param to_name
* @param to_email
*/
public void sendSMS(String sendAddress, String recvAddress, String sendUser, String mailContent) throws Exception{
try{
conn.begin();
//2015.3.6 변경
/*RowSet userInfo = getUserInfo(sendUser);
if (userInfo.size() > 0 && !userInfo.getRow(0).get("TEL_NO").isEmpty()){
recvAddress = userInfo.getRow(0).get("TEL_NO").replaceAll("-", "");
} */
recvAddress = recvAddress.replaceAll("-","");
if(recvAddress.trim().equals("")){
recvAddress = "024899000";
}
if(!sendAddress.equals("")){
PersistentImpl persistent = new PersistentImpl(conn.getConnection());
StringBuffer smsInsert = new StringBuffer();
smsInsert.append(" INSERT INTO T_SEND(NREGID, \n");
smsInsert.append(" NSVCTYPE, \n");
smsInsert.append(" NADDRTYPE, \n");
smsInsert.append(" SADDRS, \n");
smsInsert.append(" NCONTSTYPE, \n");
smsInsert.append(" SCONTS, \n");
smsInsert.append(" SFROM \n");
smsInsert.append(" )VALUES( \n");
smsInsert.append(" UMSBIZ_SENDSEQ.NEXTVAL, \n");
smsInsert.append(" 3, \n");
smsInsert.append(" 0, \n");
smsInsert.append(" ?, \n");
smsInsert.append(" 0, \n");
smsInsert.append(" '"+mailContent+"', \n");
smsInsert.append(" ? \n");
smsInsert.append(" ) \n");
persistent.setStatement(smsInsert.toString());
persistent.addParameter(sendAddress);
persistent.addParameter(recvAddress);
persistent.execute();
persistent.clearParameters();
}
}catch(Exception e){
WAFLogger.error(e);
e.printStackTrace();
conn.setRollback();
}finally{
conn.end();
}
}
private RowSet getSmtpInfo() throws Exception{
Search search = new SearchImpl();
RowSet rowSet = new RowSetImpl();
StringBuffer sqlstr = new StringBuffer();
try {
sqlstr.append(" SELECT CHG_CODE01 \n");
sqlstr.append(" ,CHG_CODE02 \n");
sqlstr.append(" ,CHG_CODE03 \n");
sqlstr.append(" ,CHG_CODE04 \n");
sqlstr.append(" ,CHG_CODE05 \n");
sqlstr.append(" ,CHG_CODE06 \n");
sqlstr.append(" FROM EBID_COM910T A \n");
sqlstr.append(" ,EBID_COM911T B \n");
sqlstr.append(" WHERE A.BASECD = B.BASECD \n");
sqlstr.append(" AND A.BASECD = 'EMAIL' \n");
sqlstr.append(" AND B.DETAILCD = '001' \n");
search.setStatement(sqlstr.toString());
rowSet = search.execute();
}
catch(WAFSQLException se) {
Logger.err.println("SQL : \n" + se.getStatement());
Logger.err.println("PARAM : \n" + se.getParameter());
throw se;
}
finally {
if( rowSet == null ) rowSet = new RowSetImpl();
return rowSet;
}
}
public RowSet getUserInfo(String acctId) throws Exception{
Search search = new SearchImpl();
RowSet rowSet = new RowSetImpl();
StringBuffer sqlstr = new StringBuffer();
try {
sqlstr.append(" SELECT BUSEO \n");
sqlstr.append(" ,GRADE_NAME \n");
sqlstr.append(" ,USER_NAME \n");
sqlstr.append(" ,TEL_NO \n");
sqlstr.append(" ,HAND_TEL_NO \n");
sqlstr.append(" ,E_MAIL \n");
sqlstr.append(" FROM PUB0120T \n");
sqlstr.append(" WHERE ACCT_ID = "+acctId+" \n");
search.setStatement(sqlstr.toString());
rowSet = search.execute();
}
catch(WAFSQLException se) {
Logger.err.println("SQL : \n" + se.getStatement());
Logger.err.println("PARAM : \n" + se.getParameter());
throw se;
}
finally {
if( rowSet == null ) rowSet = new RowSetImpl();
return rowSet;
}
}
//메일 메세지 가져오기
public RowSet getMailMessage(String detailCd) throws Exception {
Search search = new SearchImpl();
RowSet rowSet = new RowSetImpl();
StringBuffer sqlstr = new StringBuffer();
try {
sqlstr.append(" SELECT CHG_CODE01 AS TITLE \n");
sqlstr.append(" ,CHG_CODE02 AS SUBTITLE \n");
sqlstr.append(" ,CHG_CODE03 \n");
sqlstr.append(" ||CHG_CODE04 \n");
sqlstr.append(" ||CHG_CODE05 AS CONTENTS1 \n");
sqlstr.append(" ,CHG_CODE06 AS CONTENTS2 \n");
sqlstr.append(" FROM EBID_COM911T B \n");
sqlstr.append(" ,EBID_COM910T A \n");
sqlstr.append(" WHERE A.BASECD = B.BASECD \n");
sqlstr.append(" AND A.BASECD = 'EMAIL_CONT' \n");
sqlstr.append(" AND B.DETAILCD = '"+detailCd+"' \n");
search.setStatement(sqlstr.toString());
rowSet = search.execute();
}
catch(WAFSQLException se) {
Logger.err.println("SQL : \n" + se.getStatement());
Logger.err.println("PARAM : \n" + se.getParameter());
throw se;
}
finally {
if( rowSet == null ) rowSet = new RowSetImpl();
}
return rowSet;
}
}