<%@ page import = "java.io.*, java.util.*, javax.mail.*, javax.mail.internet.*, javax.activation.*, java.util.*" %> <%! // CONSTANTS // SPECIFY A MAIL GATEWAY // Note: This IP address specifies the localhost, and this will // work on a lot of machines (it works with my ISP). However, // you might have to change it, to point at a different machine // with an email gateway (i.e. SMTP). private final String mailHost = "127.0.0.1"; // SPECIFY A "FROM" ADDRESS // This is the address that it appears that the email message was sent from. private final String fromEmailAddress = "fromAddress@myCompany.com"; // SPECIFY A "TO" ADDRESS // This is the address that the email message is sent to. private final String toEmailAddress = "toAddress@destinationCompany.com"; // ---------------------------------------------------------------- // ***** This class is a helper class, to wrap a data source. // ***** class ByteArrayDataSource implements DataSource { private byte[] data; // data for mail message private String type; // content type/mime type /** * Create a DataSource from a String * @param data is the contents of the mail message * @param type is the mime-type such as text/html */ ByteArrayDataSource(String data, String type) { try { // Assumption that the string contains only ascii // characters ! Else just pass in a charset into this // constructor and use it in getBytes() this.data = data.getBytes("iso-8859-1"); } catch (UnsupportedEncodingException uex) { } this.type = type; } //DataSource interface methods public InputStream getInputStream() throws IOException { if (data == null) throw new IOException("no data exception in ByteArrayDataSource"); return new ByteArrayInputStream(data); } public OutputStream getOutputStream() throws IOException { throw new IOException("illegal operation in ByteArrayDataSource"); } public String getContentType() { return type; } public String getName() { return "dummy"; } } // ***** This function does the work of actually sending email, // ***** using javax.mail . String sendMail(String from, String to, String subject, String body, String message) throws JspException { // We need to set a few properties first... Properties props = System.getProperties(); props.put("mail.smtp.host", mailHost); Session emailsession = Session.getDefaultInstance(props, null); try { // Now, let's make a multipart email message Message email = new MimeMessage(emailsession); email.setFrom(new InternetAddress(from)); // set FROM InternetAddress[] address = { new InternetAddress(to) }; email.setRecipients(Message.RecipientType.TO, address); // set TO email.setSubject(subject); // set SUBJECT email.setSentDate(new Date()); // set DATE email.setHeader("X-Mailer", "MailFormJava"); // If we had no attachments, we would just do this: //email.setText(body); // set BODY // But, we DO have attachments, so we do this: // ***** the BODY MimeBodyPart bodyPart1 = new MimeBodyPart(); bodyPart1.setText(body); // set BODY // ***** ATTACHMENT #1: a text attachment MimeBodyPart attachment1 = new MimeBodyPart(); attachment1.setText("Hello there! I'm a text attachment."); // set attachment attachment1.setFileName("hello.txt"); // ***** ATTACHMENT #2: here's how to send an HTML attachment MimeBodyPart attachment2 = new MimeBodyPart(); StringBuffer sb = new StringBuffer(); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("Hello there! I am an HTML attachment.\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("

Welcome to my HTML attachment.

" + "\n"); sb.append("Here is some text for you...
And some more text...
\n"); sb.append("\n"); sb.append("\n"); attachment2.setFileName("hello.htm"); attachment2.setDataHandler(new DataHandler( new ByteArrayDataSource(sb.toString(), "text/html"))); // ***** ATTACHMENT #3: here's how to attach a file that was already passed to us // base64-encoded as a POST parameter (e.g. audio file from via ) MimeBodyPart attachment3 = new MimeBodyPart(); attachment3.setDataHandler(new DataHandler( new ByteArrayDataSource(message, "audio/x-wav"))); attachment3.setFileName("audiofile.wav"); attachment3.addHeader("Content-Transfer-Encoding","base64"); // ***** put body + attachments together Multipart multipart = new MimeMultipart(); multipart.addBodyPart(bodyPart1); multipart.addBodyPart(attachment1); multipart.addBodyPart(attachment2); multipart.addBodyPart(attachment3); email.setContent(multipart); // ***** and send it Transport.send(email); } catch (MessagingException e) { throw new JspException (e.getMessage()); } return("OK"); } %> <% // ***** Here's where the email is initiated. // ***** Actually send an email, with the following parameters, and // ***** while we're at it, attach an audio file that was given to us via POST, // ***** and a couple of other attachments, just to illustrate how it's done. response.setContentType("application/voicexml+xml"); sendMail( fromEmailAddress, // FROM address toEmailAddress, // TO address "Here is an audio email message for you!", // SUBJECT "The enclosed file, audiofile.wav, was\nsent to you by the BeVocal Cafe.\nCheck us out at http://cafe.bevocal.com .", // BODY request.getParameter("message") ); // BASE64 AUDIO FILE ATTACHMENT %>
Thank you! Your email message with audio attachment has been sent. Would you like to send another voice email message? Thank you for calling! Goodbye.