How to send mail(s) with a single attachment from a dashboard (Automatic/manual)
This is second approach of how to send mail(s) from a dashboard.
You can refer the first approach for the same from here: How to send alert mail(s) from a dashboard (Automatic/manual)
How to Send a mail with a single attachment from a dashboard
In this document,I will show code for sending mail with attachment(any file type) which is already exist.
In next part,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.jsp and sendmail.jsp into 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) Give path of your file in tomcat server.
http://localhost:8080/converToXml.jsp
3) Change MIME type to application/x-www-form-urlencoded
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 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 file in the root folder.
sendmail.jsp
/*basic java code for sending mail*/
//attachment
messageBodyPart= new MimeBodyPart();
String filename = “path of file”;
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
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
Great article,,,very informative !!!
Hi Zalak,
As what I understand from your article above, I need to embedded the email address of recipient(s) into excel sheet which mean that every time I click on the 'Send' button, it will send to those recipients which embedded in excel sheet.
Is there anyway that allows me to key in the recipient(s) email only after I click on 'Send' button?
Thanks,
Kim Yun
Hi Kim Yun,
Yes you understood right.
Put textbox or any component for taking recipient names and keep component's destination as b4 to b5(whatever range you want) in above example.
so when you click on send button it will take recipient names from embedded excel sheet but it will seem like it has taken from text box.
Regards,
Zalak
Hi Zalak,
Thanks for your reply!
May I know the attachment mentioned is the whole dashboard? If it is, which format will it be?
Regards,
Kim Yun
Hi Kin Yun,
I didn't get your question.Are you asking about attachment in mail?If yes,it can be in any format.String filename = "path of file"; here,path of file means path/abc.xls(/doc/ppt).
Regards,
Zalak