JAVA MAIL 단체 메 일
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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.