I have an excel sheet with one of the sheet as 'summary' which has summary of the execution, I am using javax mail to send this excel as attachment using outlook, however I would like to paste this summary in the email body so users get a summary before opening the excel. I have tried with opening the excel and capturing the screen shot but whole excel is captured I only need whats inside excel. I dont want to use VBA as the excel sheet may change.
Below is the code I am using for sending email:
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.testng.annotations.Test;
public class EmailExecutionReport {
static DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
static Date date = new Date();
static String attachFiles="C:\\Excel_Report.xls";
static String messageToBeSent="<body > <p><font face="+"calibri"+">Hi All,</font></p><p><font face="+"calibri"+">Please find the Environment validations report as in the attached file</font></p><p><font face="+"calibri"+">This set of execution was completed on: "+dateFormat.format(date)+"</font></p><br><font face="+"calibri"+">Regards<br><font face="+"calibri"+"><br><br>---This is a auto generated email---</body>";
@Test
public static void JavaSentEamil() throws IOException {
// String host="host";
final String user="SSS@abcd.com";
String to="icpm-qa@abcd.com";
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host","abcd.abcd.com");
props.put("mail.smtp.auth", "false");
Session session=Session.getDefaultInstance(props, null);
session.setDebug(true);
//Compose the message
try {
MimeMessage message = new MimeMessage(session);
message.saveChanges();
message.setFrom(new InternetAddress(user));
//message.addRecipient(Message.RecipientType.TO,new InternetAddress (to));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("Automation Environment validations Report as on: "+dateFormat.format(date));
// message.setText("This is test mail sent from Java Program");
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(messageToBeSent, "text/html ; charset=ISO-8859-1");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if (attachFiles != null) {
MimeBodyPart attachPart = new MimeBodyPart();
attachPart.attachFile(attachFiles);
multipart.addBodyPart(attachPart);
}
// sets the multi-part as e-mail's content
message.setContent(multipart);
//send the message
Transport.send(message);
System.out.println("Message sent successfully...");
}
catch (MessagingException e) {e.printStackTrace();}
}
}