Email Util

2453 단어
package com.ebao.lisig.integration.agpi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

import com.ebao.foundation.common.config.Env;
import com.ebao.lisig.integration.agpi.data.EmailInfoDTO;
import com.ebao.lisig.integration.agpi.data.MailAttach;

public class EmailUtil {				
	private static Log log = LogFactory.getLog(EmailUtil.class);			

	public static Map SendMail(EmailInfoDTO emailDTO) {			
		Map result = new HashMap();		
		MultiPartEmail email = new MultiPartEmail();		
		try {		
			email.setCharset("UTF-8");	
			email.setHostName(Env.getValue("MAIL.HOST"));	
			if ("true".equals(Env.getValue("MAIL.SMTP.AUTH")))	
				email.setAuthentication(Env.getValue("SERVER.USERNAME"), Env.getValue("SERVER.PASSWORD"));
			email.setFrom(Env.getValue("ARGO_EMAIL_SENDER"), "");	

			email.setSubject(emailDTO.getSubject());	
			if (null != emailDTO.getContent() && !"".equals(emailDTO.getContent()))	
				email.setMsg(emailDTO.getContent());	
			if (null != emailDTO.getTo() && !"".equals(emailDTO.getTo())) {		
				String[] toArray = emailDTO.getTo().split(";");	
				for (String to : toArray) {	
					email.addTo(to);
				}	
			}		
			if (null != emailDTO.getCc() && !"".equals(emailDTO.getCc())) {		
				String[] ccArray = emailDTO.getCc().split(";");	
				for (String cc : ccArray) {	
					email.addCc(cc);
				}	
			}		

			List<MailAttach> attachs = emailDTO.getAttachmentData();		
			if (null != attachs) {		
				for (MailAttach attach : attachs) {	
					EmailAttachment attachment = new EmailAttachment();
					attachment.setName(attach.getName());
					attachment.setPath(attach.getPath());
					attachment.setDisposition(EmailAttachment.ATTACHMENT);
					email.attach(attachment);
				}	
			}		
			email.send();		
		} catch (EmailException e) {			
			log.error("Email sent failed" + e.getLocalizedMessage());		
			result.put("status", Boolean.FALSE);		
			result.put("exception", e.getLocalizedMessage());		
			return result;		
		}			
		result.put("status", Boolean.TRUE);			
		return result;
	}	
}

좋은 웹페이지 즐겨찾기