| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
|
| 3.375;3.375 |
| 1 | /* | |
| 2 | * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.util.Email.html,v $ | |
| 3 | * $Revision: 1.1 $ | |
| 4 | * | |
| 5 | * Copyright (C) 2006 Tim Pizey | |
| 6 | * | |
| 7 | * Part of Melati (http://melati.org), a framework for the rapid | |
| 8 | * development of clean, maintainable web applications. | |
| 9 | * | |
| 10 | * Melati is free software; Permission is granted to copy, distribute | |
| 11 | * and/or modify this software under the terms either: | |
| 12 | * | |
| 13 | * a) the GNU General Public License as published by the Free Software | |
| 14 | * Foundation; either version 2 of the License, or (at your option) | |
| 15 | * any later version, | |
| 16 | * | |
| 17 | * or | |
| 18 | * | |
| 19 | * b) any version of the Melati Software License, as published | |
| 20 | * at http://melati.org | |
| 21 | * | |
| 22 | * You should have received a copy of the GNU General Public License and | |
| 23 | * the Melati Software License along with this program; | |
| 24 | * if not, write to the Free Software Foundation, Inc., | |
| 25 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the | |
| 26 | * GNU General Public License and visit http://melati.org to obtain the | |
| 27 | * Melati Software License. | |
| 28 | * | |
| 29 | * Feel free to contact the Developers of Melati (http://melati.org), | |
| 30 | * if you would like to work out a different arrangement than the options | |
| 31 | * outlined here. It is our intention to allow Melati to be used by as | |
| 32 | * wide an audience as possible. | |
| 33 | * | |
| 34 | * This program is distributed in the hope that it will be useful, | |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 37 | * GNU General Public License for more details. | |
| 38 | * | |
| 39 | * Contact details for copyright holder: | |
| 40 | * | |
| 41 | * Tim Pizey <timp At paneris.org> | |
| 42 | * http://paneris.org/~timp | |
| 43 | */ | |
| 44 | package org.melati.util; | |
| 45 | ||
| 46 | import java.io.File; | |
| 47 | import java.io.IOException; | |
| 48 | import javax.mail.Address; | |
| 49 | import javax.mail.Message; | |
| 50 | import javax.mail.MessagingException; | |
| 51 | import javax.mail.Multipart; | |
| 52 | import javax.mail.Session; | |
| 53 | import javax.mail.Transport; | |
| 54 | import javax.mail.internet.InternetAddress; | |
| 55 | import javax.mail.internet.MimeBodyPart; | |
| 56 | import javax.mail.internet.MimeMessage; | |
| 57 | import javax.mail.internet.MimeMultipart; | |
| 58 | //import org.melati.poem.Database; | |
| 59 | ||
| 60 | import javax.activation.DataHandler; | |
| 61 | import javax.activation.FileDataSource; | |
| 62 | ||
| 63 | /** | |
| 64 | * Send an email to one or more recipients with or without attachments. | |
| 65 | */ | |
| 66 | public final class Email { | |
| 67 | ||
| 68 | /** Property key for the SMTP server.*/ | |
| 69 | 2 | public static String SMTPSERVER = "SMTPServer"; |
| 70 | ||
| 71 | 0 | private Email() { |
| 72 | 0 | } |
| 73 | ||
| 74 | /** | |
| 75 | * deprecated try to disentangle poem and utils | |
| 76 | */ | |
| 77 | /* | |
| 78 | public static void send(Database database, String from, String to, | |
| 79 | String replyto, String subject, String text) | |
| 80 | throws EmailException, IOException { | |
| 81 | File[] empty = {}; | |
| 82 | String smtpServer = database.getSettingTable().get(SMTPSERVER); | |
| 83 | sendWithAttachments(smtpServer, from, to, replyto, subject, text, empty); | |
| 84 | } | |
| 85 | */ | |
| 86 | /** | |
| 87 | * Send the email. | |
| 88 | * @param smtpServer name of SMTP server to use | |
| 89 | * @param from email address and optionally name of sender | |
| 90 | * @param to email address and optionally name of recipient | |
| 91 | * @param replyto email address and optionally name to reply to | |
| 92 | * @param subject subject of message | |
| 93 | * @param text text body of email | |
| 94 | */ | |
| 95 | public static void send(String smtpServer, String from, String to, | |
| 96 | String replyto, String subject, String text) | |
| 97 | throws EmailException, IOException { | |
| 98 | 4 | File[] empty = {}; |
| 99 | 4 | sendWithAttachments(smtpServer, from, to, replyto, subject, text, empty); |
| 100 | 2 | } |
| 101 | ||
| 102 | /* | |
| 103 | public static void sendToList(Database database, String from, | |
| 104 | String[] toList, String apparentlyTo, String replyto, String subject, | |
| 105 | String message) throws EmailException, IOException { | |
| 106 | File[] empty = {}; | |
| 107 | ||
| 108 | String smtpServer = database.getSettingTable().get(SMTPSERVER); | |
| 109 | for (int i = 0; i < toList.length; i++) | |
| 110 | sendWithAttachments(smtpServer, from, toList[i], replyto, subject, message, empty); | |
| 111 | } | |
| 112 | */ | |
| 113 | /** | |
| 114 | * Send the email to a list of recipients. | |
| 115 | * | |
| 116 | * @param smtpServer name of SMTP server to use | |
| 117 | * @param from email address and optionally name of sender | |
| 118 | * @param toList list of email addresses and optionally names of recipients | |
| 119 | * @param replyto email address and optionally name to reply to | |
| 120 | * @param subject subject of message | |
| 121 | * @param message text body of email | |
| 122 | */ | |
| 123 | public static void sendToList(String smtpServer, String from, | |
| 124 | String[] toList, String replyto, String subject, | |
| 125 | String message) throws EmailException, IOException { | |
| 126 | 0 | File[] empty = {}; |
| 127 | ||
| 128 | 0 | for (int i = 0; i < toList.length; i++) |
| 129 | 0 | sendWithAttachments(smtpServer, from, toList[i], replyto, subject, message, empty); |
| 130 | 0 | } |
| 131 | ||
| 132 | /** | |
| 133 | * Send message with attachments. | |
| 134 | * | |
| 135 | * @param smtpServer name of SMTP server to use | |
| 136 | * @param from email address and optionally name of sender | |
| 137 | * @param to email address and optionally name of recipient | |
| 138 | * @param replyto email address and optionally name to reply to | |
| 139 | * @param subject subject of message | |
| 140 | * @param text text body of email | |
| 141 | * @param attachments Array of files to attach | |
| 142 | */ | |
| 143 | public static void sendWithAttachments(String smtpServer, String from, | |
| 144 | String to, String replyto, String subject, String text, File[] attachments) | |
| 145 | throws EmailException, IOException { | |
| 146 | ||
| 147 | // Construct the message | |
| 148 | 6 | Message message = initialiseMessage(smtpServer, from, to, replyto, subject); |
| 149 | try { | |
| 150 | // create and fill the first, text message part | |
| 151 | 6 | MimeBodyPart mbp1 = new MimeBodyPart(); |
| 152 | 6 | mbp1.setText(text); |
| 153 | 6 | Multipart mp = new MimeMultipart(); |
| 154 | 6 | mp.addBodyPart(mbp1); |
| 155 | 12 | for (int i = 0; i < attachments.length; i++) { |
| 156 | 6 | File f = attachments[i]; |
| 157 | 6 | if (f != null) { |
| 158 | // create the second message part | |
| 159 | 2 | MimeBodyPart mbp2 = new MimeBodyPart(); |
| 160 | // attach the file to the message | |
| 161 | 2 | FileDataSource fds = new FileDataSource(f); |
| 162 | 2 | mbp2.setDataHandler(new DataHandler(fds)); |
| 163 | 2 | mbp2.setFileName(fds.getName()); |
| 164 | 2 | mp.addBodyPart(mbp2); |
| 165 | } | |
| 166 | } | |
| 167 | // add the Multipart to the message | |
| 168 | 6 | message.setContent(mp); |
| 169 | 0 | } catch (Exception e) { |
| 170 | 0 | e.printStackTrace(); |
| 171 | 0 | throw new EmailException("Problem creating message: " + e.toString()); |
| 172 | 6 | } |
| 173 | // send the message | |
| 174 | 6 | post(message); |
| 175 | 4 | } |
| 176 | ||
| 177 | /** | |
| 178 | * Send HTML message with attachments. | |
| 179 | * | |
| 180 | * @param smtpServer name of SMTP server to use | |
| 181 | * @param from email address and optionally name of sender | |
| 182 | * @param to email address and optionally name of recipient | |
| 183 | * @param replyto email address and optionally name to reply to | |
| 184 | * @param subject subject of message | |
| 185 | * @param plainText text body of email | |
| 186 | * @param htmlText HTML body of email | |
| 187 | * @param referenced Array of Files referenced withing the HTML body | |
| 188 | * @param attachments Array of files to attach | |
| 189 | */ | |
| 190 | public static void sendAsHtmlWithAttachments(String smtpServer, String from, | |
| 191 | String to, String replyto, String subject, String plainText, | |
| 192 | String htmlText, File[] referenced, File[] attachments) | |
| 193 | throws EmailException, IOException { | |
| 194 | ||
| 195 | // Construct the message | |
| 196 | 2 | Message message = initialiseMessage(smtpServer, from, to, replyto, subject); |
| 197 | try { | |
| 198 | 2 | Multipart mp = new MimeMultipart("related"); |
| 199 | 2 | MimeBodyPart mbp1 = new MimeBodyPart(); |
| 200 | //mbp1.setText(plainText); | |
| 201 | 2 | mbp1.setContent(plainText, "text/plain"); |
| 202 | 2 | mp.addBodyPart(mbp1); |
| 203 | 2 | MimeBodyPart mbp2 = new MimeBodyPart(); |
| 204 | 2 | mbp2.setContent(htmlText, "text/html"); |
| 205 | 2 | mp.addBodyPart(mbp2); |
| 206 | ||
| 207 | 2 | if (referenced != null) { |
| 208 | 4 | for (int i = 0; i < referenced.length; i++) { |
| 209 | 2 | File f = referenced[i]; |
| 210 | 2 | if (f != null) { |
| 211 | 0 | MimeBodyPart mbp3 = new MimeBodyPart(); |
| 212 | 0 | FileDataSource fds = new FileDataSource(f); |
| 213 | 0 | mbp3.setDataHandler(new DataHandler(fds)); |
| 214 | 0 | mbp3.setFileName(fds.getName()); |
| 215 | 0 | mp.addBodyPart(mbp3); |
| 216 | } | |
| 217 | } | |
| 218 | } | |
| 219 | 2 | if (attachments != null) { |
| 220 | 6 | for (int i = 0; i < attachments.length; i++) { |
| 221 | 4 | File f = attachments[i]; |
| 222 | 4 | if (f != null) { |
| 223 | 2 | MimeBodyPart mbp4 = new MimeBodyPart(); |
| 224 | 2 | if (f.getName() == null) { |
| 225 | 0 | System.out.println("name is null"); |
| 226 | } | |
| 227 | 2 | FileDataSource fds = new FileDataSource(f); |
| 228 | 2 | mbp4.setDataHandler(new DataHandler(fds)); |
| 229 | 2 | mbp4.setFileName(fds.getName()); |
| 230 | 2 | mp.addBodyPart(mbp4); |
| 231 | } | |
| 232 | } | |
| 233 | } | |
| 234 | // add the Multipart to the message | |
| 235 | 2 | message.setContent(mp); |
| 236 | 0 | } catch (Exception e) { |
| 237 | 0 | e.printStackTrace(); |
| 238 | 0 | throw new EmailException("Problem creating message: " + e.toString()); |
| 239 | 2 | } |
| 240 | // send the message | |
| 241 | 2 | post(message); |
| 242 | 2 | } |
| 243 | ||
| 244 | private static Message initialiseMessage(String smtpServer, String from, | |
| 245 | String to, String replyto, String subject) throws EmailException { | |
| 246 | // Create the JavaMail session | |
| 247 | // The properties for the whole system, sufficient to send a mail | |
| 248 | // and much more besides. | |
| 249 | 8 | java.util.Properties properties = System.getProperties(); |
| 250 | 8 | properties.put("mail.smtp.host", smtpServer); |
| 251 | // this is required if InetAddress.getLocalHost().getHostName() returns null | |
| 252 | // which it does nor me in Eclipse | |
| 253 | 8 | properties.put("mail.smtp.localhost", smtpServer); |
| 254 | 8 | Session session = Session.getInstance(properties, null); |
| 255 | 8 | MimeMessage message = new MimeMessage(session); |
| 256 | // Set the from address | |
| 257 | Address fromAddress; | |
| 258 | try { | |
| 259 | 8 | fromAddress = new InternetAddress(from); |
| 260 | 8 | message.setFrom(fromAddress); |
| 261 | // Parse and set the recipient addresses | |
| 262 | 8 | Address[] toAddresses = InternetAddress.parse(to); |
| 263 | 8 | message.setRecipients(Message.RecipientType.TO, toAddresses); |
| 264 | /* | |
| 265 | * Address[] ccAddresses = InternetAddress.parse(cc); | |
| 266 | * message.setRecipients(Message.RecipientType.CC,ccAddresses); | |
| 267 | * | |
| 268 | * Address[] bccAddresses = InternetAddress.parse(bcc); | |
| 269 | * message.setRecipients(Message.RecipientType.BCC,bccAddresses); | |
| 270 | */ | |
| 271 | 8 | if (replyto != null) { |
| 272 | 0 | Address[] replyTos = InternetAddress.parse(replyto); |
| 273 | 0 | message.setReplyTo(replyTos); |
| 274 | } | |
| 275 | 8 | message.setSubject(subject); |
| 276 | 0 | } catch (Exception e) { |
| 277 | 0 | e.printStackTrace(); |
| 278 | 0 | throw new EmailException("Problem sending message: " + e.toString()); |
| 279 | 8 | } |
| 280 | 8 | return message; |
| 281 | } | |
| 282 | ||
| 283 | private static void post(Message message) throws EmailException { | |
| 284 | try { | |
| 285 | 8 | Transport.send(message); |
| 286 | 2 | } catch (MessagingException e) { |
| 287 | 2 | e.printStackTrace(); |
| 288 | 2 | throw new EmailException("Problem sending message: " + e.toString()); |
| 289 | 6 | } |
| 290 | ||
| 291 | 6 | } |
| 292 | /** | |
| 293 | * @return a fancy email address | |
| 294 | */ | |
| 295 | public static String mailAddress(String name, String email) { | |
| 296 | 0 | return name + " <" + email + ">"; |
| 297 | } | |
| 298 | ||
| 299 | } |