package gpro.common.New;
import gpro.common.base.GProInterface;
import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.mail.smtp.SMTPMessage;
public class MailUtil extends GProInterface implements Runnable {
private Properties properties;
private String subject;
private StringBuilder body;
private String from;
private String to;
private String cc;
private String pathName;
private StringBuilder fileText;
private static final String fileSuffix = ".csv";
private boolean logDontMail;
private static final AtomicInteger ai = new AtomicInteger();
private final Logger logger = LoggerFactory.getLogger(getClass());
public MailUtil(String subject, StringBuilder body, StringBuilder fileText) {
init(subject, body);
this.fileText = fileText;
}
public MailUtil(String subject, StringBuilder body) {
init(subject, body);
}
private void init(String arg0, StringBuilder arg1) {
properties = getConnectionProps() == null ? new Properties() : getConnectionProps();
subject = arg0 == null ? "NO_SUBJECT" : arg0;
body = arg1 == null ? new StringBuilder("NO_BODY") : arg1;
to = properties.getProperty("Email.toList", "mbrigant@g-trade.com");
cc = properties.getProperty("Email.ccList", "mbrigant@g-trade.com");
from = properties.getProperty("Email.from", "CGMAutoEmailer@g-trade.com");
properties.put("mail.smtp.host", getConnectionProps().getProperty("Email.server", "160.254.138.72"));
if ("true".equalsIgnoreCase(getConnectionProps().getProperty("Email.writeToLog"))) {
logDontMail = true;
}
pathName = properties.getProperty("Email.filePath", ".\\out\\");
}
public void run() {
try {
sendMail();
} catch (Exception ex) {
logger.warn(EXCEPTION_MESSAGE, ex);
}
}
public void sendMail() throws Exception {
Session session = Session.getInstance(properties);
SMTPMessage msg = new SMTPMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(SMTPMessage.RecipientType.TO, to);
if (cc != null)
msg.setRecipients(SMTPMessage.RecipientType.CC, cc);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body.toString());
File fp = null;
if (fileText != null && pathName != null) {
fp = new File(pathName + ai.getAndIncrement() + fileSuffix);
FileWriter fw = new FileWriter(fp, true);
fw.write(fileText.toString());
fw.close();
MimeMultipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(body.toString());
mp.addBodyPart(mbp, 0);
mbp = new MimeBodyPart();
mbp.attachFile(fp);
mp.addBodyPart(mbp, 1);
msg.setContent(mp);
}
if (logDontMail) {
write2Log(msg);
} else {
Transport.send(msg);
}
if (fp != null) {
fp.delete();
}
}
private void write2Log(SMTPMessage smtpMessage) {
try {
String rn = "\r\n";
StringBuilder sb = new StringBuilder();
Address adressTo[] = smtpMessage.getRecipients(Message.RecipientType.TO);
List to = Arrays.asList(adressTo);
sb.append(rn + "To :" + to);
if (cc != null) {
Address adressCC[] = smtpMessage.getRecipients(Message.RecipientType.CC);
List cc = Arrays.asList(adressCC);
sb.append(rn + "CC :" + cc);
}
Date date = smtpMessage.getSentDate();
sb.append(rn + "Date : " + date);
String sub = smtpMessage.getSubject();
sb.append(rn + "Subject : " + sub);
Object bd = smtpMessage.getContent();
smtpMessage.getDataHandler().getInputStream();
sb.append(rn + "Content : " + smtpMessage.getContent());
logger.info(sb.toString());
} catch (Exception e) {
logger.warn(EXCEPTION_MESSAGE, e);
}
}
}
import gpro.common.base.GProInterface;
import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.mail.smtp.SMTPMessage;
public class MailUtil extends GProInterface implements Runnable {
private Properties properties;
private String subject;
private StringBuilder body;
private String from;
private String to;
private String cc;
private String pathName;
private StringBuilder fileText;
private static final String fileSuffix = ".csv";
private boolean logDontMail;
private static final AtomicInteger ai = new AtomicInteger();
private final Logger logger = LoggerFactory.getLogger(getClass());
public MailUtil(String subject, StringBuilder body, StringBuilder fileText) {
init(subject, body);
this.fileText = fileText;
}
public MailUtil(String subject, StringBuilder body) {
init(subject, body);
}
private void init(String arg0, StringBuilder arg1) {
properties = getConnectionProps() == null ? new Properties() : getConnectionProps();
subject = arg0 == null ? "NO_SUBJECT" : arg0;
body = arg1 == null ? new StringBuilder("NO_BODY") : arg1;
to = properties.getProperty("Email.toList", "mbrigant@g-trade.com");
cc = properties.getProperty("Email.ccList", "mbrigant@g-trade.com");
from = properties.getProperty("Email.from", "CGMAutoEmailer@g-trade.com");
properties.put("mail.smtp.host", getConnectionProps().getProperty("Email.server", "160.254.138.72"));
if ("true".equalsIgnoreCase(getConnectionProps().getProperty("Email.writeToLog"))) {
logDontMail = true;
}
pathName = properties.getProperty("Email.filePath", ".\\out\\");
}
public void run() {
try {
sendMail();
} catch (Exception ex) {
logger.warn(EXCEPTION_MESSAGE, ex);
}
}
public void sendMail() throws Exception {
Session session = Session.getInstance(properties);
SMTPMessage msg = new SMTPMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(SMTPMessage.RecipientType.TO, to);
if (cc != null)
msg.setRecipients(SMTPMessage.RecipientType.CC, cc);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body.toString());
File fp = null;
if (fileText != null && pathName != null) {
fp = new File(pathName + ai.getAndIncrement() + fileSuffix);
FileWriter fw = new FileWriter(fp, true);
fw.write(fileText.toString());
fw.close();
MimeMultipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(body.toString());
mp.addBodyPart(mbp, 0);
mbp = new MimeBodyPart();
mbp.attachFile(fp);
mp.addBodyPart(mbp, 1);
msg.setContent(mp);
}
if (logDontMail) {
write2Log(msg);
} else {
Transport.send(msg);
}
if (fp != null) {
fp.delete();
}
}
private void write2Log(SMTPMessage smtpMessage) {
try {
String rn = "\r\n";
StringBuilder sb = new StringBuilder();
Address adressTo[] = smtpMessage.getRecipients(Message.RecipientType.TO);
List to = Arrays.asList(adressTo);
sb.append(rn + "To :" + to);
if (cc != null) {
Address adressCC[] = smtpMessage.getRecipients(Message.RecipientType.CC);
List cc = Arrays.asList(adressCC);
sb.append(rn + "CC :" + cc);
}
Date date = smtpMessage.getSentDate();
sb.append(rn + "Date : " + date);
String sub = smtpMessage.getSubject();
sb.append(rn + "Subject : " + sub);
Object bd = smtpMessage.getContent();
smtpMessage.getDataHandler().getInputStream();
sb.append(rn + "Content : " + smtpMessage.getContent());
logger.info(sb.toString());
} catch (Exception e) {
logger.warn(EXCEPTION_MESSAGE, e);
}
}
}
No comments:
Post a Comment