import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import java.util.Vector; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; 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 dori.jasper.engine.JRException; /* * Created on 31/10/2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class SendMail { private String m_strMailHost; private Properties m_props; private javax.mail.Session m_session; public SendMail(String strMailHost) { m_strMailHost = strMailHost; m_props = new Properties(); m_props = System.getProperties(); } void send( String[] aMailTo, String strMailFrom, String strSubject, String strBody, boolean bBodyIsHTML, File filAttachment ) throws MessagingException, IOException { DataSource[] aDS = {new FileDataSource(filAttachment)}; DataSource dsBody = new ByteArrayDataSource("Body", strBody, (bBodyIsHTML) ? "text/html" : "text" ); send(aMailTo, strMailFrom, strSubject, dsBody, aDS, null); } void send( String[] aMailTo, String strMailFrom, String strSubject, DataSource dsBody, DataSource dsAttachment) throws MessagingException, IOException { DataSource[] aDS = {dsAttachment}; send(aMailTo, strMailFrom, strSubject, dsBody, aDS, null); } void send( String[] aMailTo, String strMailFrom, String strSubject, DataSource dsBody, DataSource[] adsImage, DataSource[] adsAttachment) throws MessagingException, IOException { m_props.put("mail.smtp.host", m_strMailHost); m_session = javax.mail.Session.getDefaultInstance(m_props, null); m_session.setDebug(true); MimeMessage msg = new MimeMessage(m_session); msg.setFrom(new InternetAddress(strMailFrom)); InternetAddress[] address = new InternetAddress[aMailTo.length]; for (int i = 0; i < aMailTo.length; i++) { address[i] = new InternetAddress((String)aMailTo[i]); } msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(strSubject); msg.setSentDate(new java.util.Date()); msg.addHeader("MIME-Version", "1.0"); // create and fill the first message part Vector vBodyParts = new Vector(); MimeBodyPart bodyPart = new MimeBodyPart(); if (!dsBody.getContentType().equals("text/html")) { bodyPart.setDataHandler(new DataHandler(dsBody)); vBodyParts.add(bodyPart); } else { bodyPart.setHeader("X-Mailer", "controlweb"); bodyPart.setDataHandler(new DataHandler(dsBody)); vBodyParts.add(bodyPart); // Add and images used by the HTML. for (int i = 0; i < adsImage.length; i++) { bodyPart = new MimeBodyPart(); // attach the image to the message String strName = adsImage[i].getName(); bodyPart.setFileName(strName); bodyPart.setContentID(strName); //bodyPart.setHeader("Content-ID", "<" + strName + ">"); bodyPart.setDisposition(javax.mail.Part.INLINE); bodyPart.setDataHandler(new DataHandler(adsImage[i])); vBodyParts.add(bodyPart); } } // Add any file attachemnts for (int i = 0; i < adsAttachment.length; i++) { bodyPart = new MimeBodyPart(); // attach the file to the message bodyPart.setDataHandler(new DataHandler(adsAttachment[i])); bodyPart.setDisposition(javax.mail.Part.ATTACHMENT); bodyPart.setFileName(adsAttachment[i].getName()); vBodyParts.add(bodyPart); } // create the Multipart mime and attach the body parts Multipart mp = new MimeMultipart("related"); for (int i = 0; i < vBodyParts.size(); i++) { mp.addBodyPart((MimeBodyPart)vBodyParts.elementAt(i)); } // And finally add the Multipart to the message msg.setContent(mp); Transport.send(msg); } // This sample send an email message using sun's mail api // and Jasper. // Two jasper reports are used the first is a hyporthetical invoice // which is sent as a PDF attachment. The second report is used // as the body of the email and is sent as an inline HTML message. // If the report used for the body contains any images then they // are also added as inline attachments to the message. // static public void main(String[] aArgs) throws SQLException, JRException { Connection conn = null; try { String strServer = aArgs[0]; String strDatabaseName = aArgs[1]; String strUser = aArgs[2]; String strPassword = aArgs[3]; String strSMTPServer = aArgs[4]; String strFromAddr = aArgs[5]; String strToAddr = aArgs[6]; conn = DBServer.factory(DBServer.MSSQL , "jdbc:microsoft:sqlserver:" + "//" + strServer + ":1433;" + "DatabaseName=" + strDatabaseName + ";" + "user=" + strUser + ";" + "password=" + strPassword); SendMail send = new SendMail(strSMTPServer); String[] aMailTo = { strToAddr }; String strMailFrom = strFromAddr; String strSubject = "NUTC Tax Invoice"; // The location of the Jasper reports and associated images. String strReportDir = "C:\\dev\\HotelBookingEngine\\Reports"; // Generate the invoice JasperPDFReport invoice = new JasperPDFReport( new File(strReportDir, "Invoice.jasper").getCanonicalPath() , strReportDir , "Invoice" , null); // Set any report specific parameters invoice.setParameter("BookingRequestID", new Long(787)); // Write the PDF report out to a bytearray. ByteArrayOutputStream baosInvoice = new ByteArrayOutputStream(); invoice.write(conn, baosInvoice); // Now generate the message body as html JasperHTMLReport body = new JasperHTMLReport( new File(strReportDir, "InvoiceSMTPBody.jasper").getCanonicalPath() , strReportDir , "Body", null); // Set any report specific parameters body.setParameter("BookingRequestID", new Long(787)); // Write the HTML report out to a String StringWriter writerBody = new StringWriter(); body.write(conn, writerBody); // Get all of the images for the HTML report // so we can attach them to the email message. Vector vImages = body.getImageDataSource(); DataSource[] adsImages = new DataSource[1]; adsImages = (DataSource[])vImages.toArray(adsImages); // Set the PDF report up as an attachment. DataSource[] aAttachment = {new ByteArrayDataSource("Invoice.pdf", baosInvoice.toByteArray(), "application/pdf")}; // Now send the email message. send.send(aMailTo, strMailFrom, strSubject , new ByteArrayDataSource("Body", writerBody.toString(), "text/html") , adsImages , aAttachment); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (conn != null) conn.close(); } } }