JAVA MAIL 단체 메 일

7302 단어
더 읽 기
package com.hp.poc.common;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EmailsSend  {
    //BCC  100   
    final int BCC_SIZE = 100;
    //  10     
    final int PEROID = 100000;
    final Logger Log = LoggerFactory.getLogger(EmailsSend.class);
    ArrayList partsList=new ArrayList();
    String subject="",body="";
    
	public EmailsSend() {
		super();
	}
	
	public EmailsSend(ArrayList partsList, String subject, String body) {
		super();
		this.partsList = partsList;
		this.subject = subject;
		this.body = body;
	}
	
	public  ArrayList setps(String subject,String body,ArrayList to) throws Exception{
		ArrayList smallArray=null;
		ArrayList errArray=new ArrayList();
		double c=(double)to.size()/(double)BCC_SIZE;
		int k=((Double)Math.ceil(c)).intValue();
		int i=0;
		for(int j=1;j<=k;j++){
			smallArray=new ArrayList();
			for(;i to) throws Exception{
		HashMap map=sendEmailByCustomerMailServer(subject,body,to,null);
		if(map.get("state").toString().equals("success")){
			return true;
		}
		return false;
	}
	private  HashMap sendEmailByCustomerMailServer(String subject,
			String body, ArrayList to, ArrayList cc) {
		HashMap map = new HashMap();
		String message = "      !";
		map.put("state", "success");
		map.put("message", message);
		try {
			Properties props = new Properties();
			props.put("mail.smtp.host",
					ChannelConfig.getValue("mail.smtp.host"));
			String port = ChannelConfig.getValue("mail.smtp.port");
			String auth = ChannelConfig.getValue("mail.smtp.auth");
			String from=ChannelConfig.getValue("mail.smtp.username");
			if (auth != null && !"".equals(auth.trim()) && "true".equals(auth)) {
				props.put("mail.smtp.auth", auth);
				if (port != null && !"".equals(port.trim())) {
					props.put("mail.smtp.port", port);
				}
			} else {
				props.put("mail.smtp.auth", "false");
				props.put("mail.smtp.timeout", "0");
			}

			// Getting the Mail Session Object
			Session mailConnection = Session.getDefaultInstance(props);
			Transport transport = mailConnection.getTransport("smtp");
			if (auth != null && !"".equals(auth.trim()) && "true".equals(auth)) {
				transport.connect(ChannelConfig.getValue("mail.smtp.host"),
						ChannelConfig.getValue("mail.smtp.username"),
						ChannelConfig.getValue("mail.smtp.password"));
			} else {
				transport.connect();
			}

			Message msg = new MimeMessage(mailConnection);
			msg = buildMessage(msg, subject, body, from, to, cc);
			transport.sendMessage(msg, msg.getAllRecipients());
			org.jfree.util.Log.info(message+" "+to.toString());
			return map;
		} catch (Exception e) {
			message="      !    :
"+e.getMessage(); map.put("state", "failed"); map.put("message", message); e.printStackTrace(); org.jfree.util.Log.error(message+" "+to.toString()); } return map; } private Message buildMessage(Message msg, String subject, String body, String from, ArrayList to, ArrayList cc) throws UnsupportedEncodingException, MessagingException{ Address[] toAdd = buildEmaiAddress(to); Address fromAdd = new InternetAddress(from); msg.setFrom(fromAdd); msg.setRecipients(Message.RecipientType.BCC, toAdd); if (cc != null && cc.size() > 0) { Address[] ccAdd = buildEmaiAddress(cc); msg.setRecipients(Message.RecipientType.CC, ccAdd); } msg.setSubject(MimeUtility.encodeText(subject, MimeUtility.mimeCharset("UTF-8"), null)); msg.setContent(body, "text/html;charset=utf-8"); return msg; } private Address[] buildEmaiAddress(ArrayList addressArray) throws AddressException{ if(addressArray == null || addressArray.size() == 0) { return null; } Address[] addressList = new Address[addressArray.size()]; for (int i = 0; i < addressArray.size(); i++){ addressList[i] = new InternetAddress(addressArray.get(i)); } return addressList; } }

 
 
@GET
	@Path("setp")
	@Produces(MediaType.TEXT_PLAIN)
    public String sendEmailsToAllPartner() throws Exception{
		String sql="SELECT DISTINCT authorities.`UserName` FROM authorities,users WHERE " +
				"authorities.`UserName`=users.`UserName` AND authorities.`authority`='ROLE_PARTNER' " +
				"AND users.`CompanyID` IS NOT NULL AND LENGTH(authorities.`UserName`)>3 ORDER BY authorities.`UserName`;";
		ArrayList partsList=(ArrayList) dao.findByNativeSql(sql.toString());
		
		File currPathFile=new File(Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath());
		File attFile=new File(currPathFile.getParentFile().getParentFile().getPath() + File.separator + "template"+File.separator+"html_tw.html");
		String body=EmailHelper.loadPage(attFile);
		String subject="            ";
		partsList=new ArrayList();
//		for(int i=0;i<1133;i++){
//			partsList.add("[email protected]");
//		}
		EmailsSend es=new EmailsSend();
		String errAddr=es.setps(subject, body, partsList).toString();
		Log.info(errAddr);
		return errAddr;
	}

좋은 웹페이지 즐겨찾기