This is third part of how to send mail from dashboard
How to send mail from a dashboard (Automatic/manual) - Part 1
How to send mail from a dashboard (Automatic/manual) - Part 2
Send a mail with multiple attachments from a dashboard (Automatic/manual)
In this,I will show how to convert data of embedded excel of dashboard to word format and sending as attachment in mail.
-Install Tomcat server.
- Download mail.jar file.
- Put files in specific folder.
1) Place mail.jar file into D:\apache-tomcat-6.0.39\lib
2) Place two files converToXml.jspand sendmail.jspinto D:\apache-tomcat-6.0.39\webapps\ROOT
ConverToXml.jsp - To convert data of embedded excel sheet of dashboard to XML file
Sendmail.jsp - To send mail using some java API
- Make XML DATA connection in Dashboard to convert EXCEL data to XML file.
1) Provide name of connection (eg.EXCEL to XML)
2 2) Give path of your file in tomcat server.
http://localhost:8080/converToXml.jsp
3 3) Change MIME type to application/x-www-form-urlencoded
4 4) Give information for creating xml file.
- Check Enable Send checkbox
- Give name of file to be created (eg.mailinfo)
- Select range of data (for eg. Sheet1!$B$2:$B$5)
Make one more XML DATA connection to convert spreadsheet data to XML by performing above steps
Excel to XML : convert mail information to XML file
Data To Word : convert actual data to xml file,which will be converted to doc file further.
Make XML DATA connection in Dashboard to send mail
1) Provide name of connection (eg.Send Mail)
2) Give path of your file in tomcat server.
http://localhost:8080/sendmail.jsp
3) Change MIME type to application/x-www-form-urlencoded
Put one refresh button on canvas and set properties as below
ConvertToXml.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page import="java.io.*" %> <% // Set the path to save scenarios
String scenarioPath = request.getRealPath("/") ;
// Build a string from the input
InputStream in = request.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuffer buf = new StringBuffer();
String line;
while ((line = r.readLine())!=null)
{
buf.append(line);
}
String xmlString = buf.toString();
// Write this to an xml file
int startPos = xmlString.indexOf("variable name=") + 15;
int endPos = xmlString.indexOf("\">");
String fileName = xmlString.substring(startPos, endPos);
PrintWriter fileWriter = new PrintWriter (new BufferedWriter(new FileWriter(scenarioPath + fileName + ".xml")));
fileWriter.write(xmlString);
fileWriter.flush();
fileWriter.close();
%>
This code generate mailinfo.xml and data.xml file in the root folder.
sendmail.jsp
DocumentBuilderFactory dbf1 = DocumentBuilderFactory.newInstance();
DocumentBuilder db1 = dbf1.newDocumentBuilder();
Document doc1 = db1.parse("D:/apache-tomcat-6.0.39/webapps/ROOT/data.xml");
NodeList colNo1 = doc1.getElementsByTagName("column");
// Creating word file
File file=new File("D:/apache-tomcat-6.0.39/webapps/ROOT/data.doc");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
String bufstr1 = "<table border='2' align='center'><thead><th>Metric Name.</th><th>Status</th></thead>";
int q;
for(q=0;q<=colNo1.getLength()-1;q++)
{
if(colNo1.item(q).getFirstChild() == null)
{
bufstr1 += "";
}
else
{
bufstr1 +="<tr>";
bufstr1 +="<td>";
bufstr1 += colNo1.item(q).getFirstChild().getNodeValue();
bufstr1 +="</td>";
//bufstr1 +="\t";
q++;
if(colNo1.item(q).getFirstChild() == null)
{
bufstr1 += "";
}
else
{
bufstr1 +="<td>";
bufstr1 += colNo1.item(q).getFirstChild().getNodeValue();
bufstr1 +="</td>";
}
bufstr1 +="</tr>";
}
}
bufstr1+="</table>";
- bw.write(bufstr1);
- bw.close();
String sub = colNo.item(0).getFirstChild().getNodeValue();
String body="";
int n;
//add word file as attachment
messageBodyPart1 = new MimeBodyPart();
String filename1 = "D:/apache-tomcat-6.0.39/webapps/ROOT/data.doc";
DataSource source1 = new FileDataSource(filename1);
messageBodyPart1.setDataHandler(new DataHandler(source1));
messageBodyPart1.setFileName(filename1);
multipart.addBodyPart(messageBodyPart1);
How to use SEND MAIL functionality in Dashboard
- Start tomcat server.Go to the apache tomcat bin directory and click on start.bat
- Run dashboard and click on send button. This is manual sending of mail. If you want to send alert mails automatic, you can set timing in XML connections of dashboard.
Regards,
Zalak Dalal