| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
package org.melati.util; |
| 45 | |
|
| 46 | |
import java.io.File; |
| 47 | |
import java.io.IOException; |
| 48 | |
|
| 49 | |
import javax.activation.DataHandler; |
| 50 | |
import javax.activation.FileDataSource; |
| 51 | |
import javax.mail.Address; |
| 52 | |
import javax.mail.Message; |
| 53 | |
import javax.mail.MessagingException; |
| 54 | |
import javax.mail.Multipart; |
| 55 | |
import javax.mail.Session; |
| 56 | |
import javax.mail.Transport; |
| 57 | |
import javax.mail.internet.InternetAddress; |
| 58 | |
import javax.mail.internet.MimeBodyPart; |
| 59 | |
import javax.mail.internet.MimeMessage; |
| 60 | |
import javax.mail.internet.MimeMultipart; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public final class Email { |
| 66 | |
|
| 67 | 0 | private Email() { |
| 68 | 0 | } |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public static void send(String smtpServer, String from, String to, |
| 80 | |
String replyto, String subject, String text) |
| 81 | |
throws EmailException, IOException { |
| 82 | 2 | File[] empty = {}; |
| 83 | 2 | sendWithAttachments(smtpServer, from, to, replyto, subject, text, empty); |
| 84 | 2 | } |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public static void sendToList(String smtpServer, String from, |
| 97 | |
String[] toList, String replyto, String subject, |
| 98 | |
String message) throws EmailException, IOException { |
| 99 | 0 | File[] empty = {}; |
| 100 | |
|
| 101 | 0 | for (int i = 0; i < toList.length; i++) |
| 102 | 0 | sendWithAttachments(smtpServer, from, toList[i], replyto, subject, message, empty); |
| 103 | 0 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public static void sendWithAttachments(String smtpServer, String from, |
| 117 | |
String to, String replyto, String subject, String text, File[] attachments) |
| 118 | |
throws EmailException, IOException { |
| 119 | |
|
| 120 | |
|
| 121 | 4 | Message message = initialiseMessage(smtpServer, from, to, replyto, subject); |
| 122 | |
try { |
| 123 | |
|
| 124 | 4 | MimeBodyPart mbp1 = new MimeBodyPart(); |
| 125 | 4 | mbp1.setText(text); |
| 126 | 4 | Multipart mp = new MimeMultipart(); |
| 127 | 4 | mp.addBodyPart(mbp1); |
| 128 | 10 | for (int i = 0; i < attachments.length; i++) { |
| 129 | 6 | File f = attachments[i]; |
| 130 | 6 | if (f != null) { |
| 131 | |
|
| 132 | 2 | MimeBodyPart mbp2 = new MimeBodyPart(); |
| 133 | |
|
| 134 | 2 | FileDataSource fds = new FileDataSource(f); |
| 135 | 2 | mbp2.setDataHandler(new DataHandler(fds)); |
| 136 | 2 | mbp2.setFileName(fds.getName()); |
| 137 | 2 | mp.addBodyPart(mbp2); |
| 138 | |
} |
| 139 | |
} |
| 140 | |
|
| 141 | 4 | message.setContent(mp); |
| 142 | 0 | } catch (Exception e) { |
| 143 | 0 | e.printStackTrace(); |
| 144 | 0 | throw new EmailException("Problem creating message: " + e.toString()); |
| 145 | 4 | } |
| 146 | |
|
| 147 | 4 | post(message); |
| 148 | 4 | } |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public static void sendAsHtmlWithAttachments(String smtpServer, String from, |
| 164 | |
String to, String replyto, String subject, String plainText, |
| 165 | |
String htmlText, File[] referenced, File[] attachments) |
| 166 | |
throws EmailException, IOException { |
| 167 | |
|
| 168 | |
|
| 169 | 2 | Message message = initialiseMessage(smtpServer, from, to, replyto, subject); |
| 170 | |
try { |
| 171 | 2 | Multipart mp = new MimeMultipart("related"); |
| 172 | 2 | MimeBodyPart mbp1 = new MimeBodyPart(); |
| 173 | |
|
| 174 | 2 | mbp1.setContent(plainText, "text/plain"); |
| 175 | 2 | mp.addBodyPart(mbp1); |
| 176 | 2 | MimeBodyPart mbp2 = new MimeBodyPart(); |
| 177 | 2 | mbp2.setContent(htmlText, "text/html"); |
| 178 | 2 | mp.addBodyPart(mbp2); |
| 179 | |
|
| 180 | 2 | if (referenced != null) { |
| 181 | 4 | for (int i = 0; i < referenced.length; i++) { |
| 182 | 2 | File f = referenced[i]; |
| 183 | 2 | if (f != null) { |
| 184 | 0 | MimeBodyPart mbp3 = new MimeBodyPart(); |
| 185 | 0 | FileDataSource fds = new FileDataSource(f); |
| 186 | 0 | mbp3.setDataHandler(new DataHandler(fds)); |
| 187 | 0 | mbp3.setFileName(fds.getName()); |
| 188 | 0 | mp.addBodyPart(mbp3); |
| 189 | |
} |
| 190 | |
} |
| 191 | |
} |
| 192 | 2 | if (attachments != null) { |
| 193 | 6 | for (int i = 0; i < attachments.length; i++) { |
| 194 | 4 | File f = attachments[i]; |
| 195 | 4 | if (f != null) { |
| 196 | 2 | MimeBodyPart mbp4 = new MimeBodyPart(); |
| 197 | 2 | if (f.getName() == null) { |
| 198 | 0 | System.out.println("name is null"); |
| 199 | |
} |
| 200 | 2 | FileDataSource fds = new FileDataSource(f); |
| 201 | 2 | mbp4.setDataHandler(new DataHandler(fds)); |
| 202 | 2 | mbp4.setFileName(fds.getName()); |
| 203 | 2 | mp.addBodyPart(mbp4); |
| 204 | |
} |
| 205 | |
} |
| 206 | |
} |
| 207 | |
|
| 208 | 2 | message.setContent(mp); |
| 209 | 0 | } catch (Exception e) { |
| 210 | 0 | e.printStackTrace(); |
| 211 | 0 | throw new EmailException("Problem creating message: " + e.toString()); |
| 212 | 2 | } |
| 213 | |
|
| 214 | 2 | post(message); |
| 215 | 2 | } |
| 216 | |
|
| 217 | |
private static Message initialiseMessage(String smtpServer, String from, |
| 218 | |
String to, String replyto, String subject) throws EmailException { |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | 6 | java.util.Properties properties = System.getProperties(); |
| 223 | 6 | properties.put("mail.smtp.host", smtpServer); |
| 224 | |
|
| 225 | |
|
| 226 | 6 | properties.put("mail.smtp.localhost", smtpServer); |
| 227 | 6 | Session session = Session.getInstance(properties, null); |
| 228 | 6 | MimeMessage message = new MimeMessage(session); |
| 229 | |
|
| 230 | |
Address fromAddress; |
| 231 | |
try { |
| 232 | 6 | fromAddress = new InternetAddress(from); |
| 233 | 6 | message.setFrom(fromAddress); |
| 234 | |
|
| 235 | 6 | Address[] toAddresses = InternetAddress.parse(to); |
| 236 | 6 | message.setRecipients(Message.RecipientType.TO, toAddresses); |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | 6 | if (replyto != null) { |
| 245 | 0 | Address[] replyTos = InternetAddress.parse(replyto); |
| 246 | 0 | message.setReplyTo(replyTos); |
| 247 | |
} |
| 248 | 6 | message.setSubject(subject); |
| 249 | 0 | } catch (Exception e) { |
| 250 | 0 | e.printStackTrace(); |
| 251 | 0 | throw new EmailException("Problem sending message: " + e.toString()); |
| 252 | 6 | } |
| 253 | 6 | return message; |
| 254 | |
} |
| 255 | |
|
| 256 | |
private static void post(Message message) throws EmailException { |
| 257 | |
try { |
| 258 | 6 | Transport.send(message); |
| 259 | 0 | } catch (MessagingException e) { |
| 260 | 0 | e.printStackTrace(); |
| 261 | 0 | throw new EmailException("Problem sending message: " + e.toString()); |
| 262 | 6 | } |
| 263 | |
|
| 264 | 6 | } |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
public static String mailAddress(String name, String email) { |
| 269 | 0 | return name + " <" + email + ">"; |
| 270 | |
} |
| 271 | |
|
| 272 | |
} |