import com.utility.mail.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.NewsAddress;
import org.pentaho.di.core.encryption.Encr;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException,IOException
{
// First, get a row from the default input hop
//
Object[] r = getRow();
// If the row object is null, we are done processing.
//
if (r == null) {
setOutputDone();
return false;
}
String PREFERRED_EMAIL = get(Fields.In,"PREFERRED_EMAIL").getString(r);
String host_prop = get(Fields.In, "host").getString(r);
String port_prop = get(Fields.In, "port").getString(r);
String mail_from_prop = get(Fields.In, "mail_from").getString(r);
String password_prop =get(Fields.In, "password").getString(r);
String out = Encr.decryptPasswordOptionallyEncrypted(password_prop);
String host = host_prop;
String port =port_prop;
String mailFrom =mail_from_prop;
String password =out;
String security_prop = get(Fields.In, "security").getString(r);
String security=security_prop;
// variable declaration for css ans img path (getting from html file by applying regex operation on href and img src)
String cssADD;
String completeCssADD;
String imgADD="";
String completeImgADD;
// current date
DateFormat dateFormat = new SimpleDateFormat("yyyy/MMM/dd");
Date date = new Date();
String curDate=dateFormat.format(date);
// message info
String mailTo=PREFERRED_EMAIL;
String[] mailToAdd = mailTo.split(",");
String REPORT_NAME = get(Fields.In, "REPORT_NAME").getString(r);
String subject = REPORT_NAME +" - "+curDate;
BufferedReader br;
BufferedReader brCSS;
FileReader fr;
FileReader frCSS;
String content="";
String CssContent="";
String FILEPATH = get(Fields.In, "FILEPATH").getString(r);
try{
fr=new FileReader(FILEPATH);
br= new BufferedReader(fr);
String s;
String c;
while(( s=br.readLine())!=null)
{
content=content+s;
}
String forImg_CSS=new String(content);
//Fetching Css File address from file path(getting from DB)
cssADD = forImg_CSS.substring(forImg_CSS.indexOf("href=\""));
cssADD = cssADD.substring("href=\"".length());
cssADD = cssADD.substring(0, cssADD.indexOf("\""));
imgADD = forImg_CSS.substring(forImg_CSS.indexOf("src=\""));
imgADD = imgADD.substring("src=\"".length());
imgADD = imgADD.substring(0, imgADD.indexOf("\""));
//Merging File path with css path
completeCssADD=FILEPATH.substring(0, FILEPATH.lastIndexOf("/"))+"/"+cssADD;
frCSS=new FileReader(completeCssADD);
brCSS=new BufferedReader(frCSS);
while ((c=brCSS.readLine())!=null)
{
CssContent=CssContent+c;
}
}
catch(Exception ex)
{
ex.printStackTrace();
throw new KettleException("An unexpected error occurred", ex);
}
String str=new String(content);
String CssStr=new String(CssContent);
str=str.replaceAll("<link type=\"text/css\" rel=\"stylesheet","");
str=str.replaceAll("\" href=\"[^\"]*\"", "<style type=\"text/css\">"+CssStr+"</style");
str=str.replaceAll("src=\"[^\"]*\"","src=\"cid:image1\" width=\"100%\" height=\"100%\"" );
StringBuffer body = new StringBuffer(str);
body.append("The first image is a Daily Snapshot delivered by ALCF:<br>");
body.append("Argonne National Laboratory - ALCF BI-TEAM");
// inline images
//Image Class path
completeImgADD=FILEPATH.substring(0, FILEPATH.lastIndexOf("/"))+"/"+imgADD;
Map inlineImages = new HashMap();
inlineImages.put("image1",completeImgADD);
try {
EmbeddedPentaoImageEmailUtil.send(host, port, mailFrom, mailToAdd,subject, body.toString(), inlineImages,security);
System.out.println("Email sent.");
} catch (Exception ex) {
System.out.println("Could not send email.");
ex.printStackTrace();
logError("An error has been produced while sending mails", ex);
throw new KettleException("An unexpected error occurred", ex);
}
return true;
}