I have a small program that is part of my ETL process that converts an HTML report to an image (png) than will embed that image into an email and send it to the correct recipients. The program works fine for reports that only have one page but for reports that have 2+ pages I receive the following errors in my log results:

2014/01/15 13:00:30 - sendMailUtil - Loading transformation from XML file [file:///Users/tolbert/Documents/Elance/ETL-Mail-Invision-tek 2/Non_Authentication/sendimageemailutilty-20-dec/sendMailUtil.ktr]
2014/01/15 13:00:30 - sendMailUtil - Dispatching started for transformation [sendMailUtil]
2014/01/15 13:00:30 - getResult_prev.0 - Finished processing (I=0, O=0, R=1, W=1, U=0, E=0)
2014/01/15 13:00:30 - from_kettle_properties.0 - Finished processing (I=0, O=0, R=1, W=1, U=0, E=0)
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : Unexpected error
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : org.pentaho.di.core.exception.KettleException: 
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : An unexpected error occurred
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : String index out of range: -1
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : 
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : 	at Processor.processRow(Processor.java:138)
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : 	at org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClass.processRow(UserDefinedJavaClass.java:1181)
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : 	at org.pentaho.di.trans.step.RunThread.run(RunThread.java:50)
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : 	at java.lang.Thread.run(Thread.java:695)
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : 	at java.lang.String.substring(String.java:1931)
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : 	at java.lang.String.substring(String.java:1904)
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : 	at Processor.processRow(Processor.java:115)
2014/01/15 13:00:31 - UDJC.0 - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : 	... 3 more
2014/01/15 13:00:31 - UDJC.0 - Finished processing (I=0, O=0, R=1, W=0, U=0, E=1)
2014/01/15 13:00:31 - sendMailUtil - sendMailUtil
2014/01/15 13:00:31 - sendMailUtil - sendMailUtil
2014/01/15 13:00:31 - sendMailUtil - ERROR (version 4.4.2-GA, build 18750 from 2013-07-19 11.23.04 by buildguy) : Errors detected!

Results of the recipients that are to receive the report, and the reports to be delivered are received by ETL transformations prior to the sendMailUtil transform. My script takes those results and the retrieves the recipients email addresses, report name, and file path and converts the report to an image which is then embedded into an email and emailed to the users. As I said above, the script runs fine for reports that only have one page. I need help changing the script to also deliver reports that have more than one page.

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;
}