1 40 package org.dspace.core; 41 42 import java.io.File ; 43 import java.text.MessageFormat ; 44 import java.util.ArrayList ; 45 import java.util.Date ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.Properties ; 49 50 import javax.activation.DataHandler ; 51 import javax.activation.FileDataSource ; 52 import javax.mail.Address ; 53 import javax.mail.Authenticator ; 54 import javax.mail.BodyPart ; 55 import javax.mail.Message ; 56 import javax.mail.MessagingException ; 57 import javax.mail.Multipart ; 58 import javax.mail.PasswordAuthentication ; 59 import javax.mail.Session ; 60 import javax.mail.Transport ; 61 import javax.mail.internet.InternetAddress ; 62 import javax.mail.internet.MimeBodyPart ; 63 import javax.mail.internet.MimeMessage ; 64 import javax.mail.internet.MimeMultipart ; 65 66 126 public class Email 127 { 128 137 138 139 private String content; 140 141 142 private String subject; 143 144 145 private List arguments; 146 147 148 private List recipients; 149 150 151 private String replyTo; 152 153 private List attachments; 154 155 158 Email() 159 { 160 arguments = new ArrayList (50); 161 recipients = new ArrayList (50); 162 attachments = new ArrayList (10); 163 subject = ""; 164 content = ""; 165 replyTo = null; 166 } 167 168 174 public void addRecipient(String email) 175 { 176 recipients.add(email); 177 } 178 179 187 void setContent(String cnt) 188 { 189 content = cnt; 190 arguments = new ArrayList (); 191 } 192 193 199 void setSubject(String s) 200 { 201 subject = s; 202 } 203 204 210 public void setReplyTo(String email) 211 { 212 replyTo = email; 213 } 214 215 221 public void addArgument(Object arg) 222 { 223 arguments.add(arg); 224 } 225 226 public void addAttachment(File f, String name) 227 { 228 attachments.add(new FileAttachment(f, name)); 229 } 230 231 235 public void reset() 236 { 237 arguments = new ArrayList (50); 238 recipients = new ArrayList (50); 239 attachments = new ArrayList (10); 240 replyTo = null; 241 } 242 243 249 public void send() throws MessagingException 250 { 251 String server = ConfigurationManager.getProperty("mail.server"); 253 String from = ConfigurationManager.getProperty("mail.from.address"); 254 255 Properties props = System.getProperties(); 257 props.put("mail.smtp.host", server); 258 259 Session session; 261 262 String username = ConfigurationManager.getProperty("mail.server.username"); 264 String password = ConfigurationManager.getProperty("mail.server.password"); 265 266 if (username != null) 267 { 268 props.put("mail.smtp.auth", "true"); 269 SMTPAuthenticator smtpAuthenticator = new SMTPAuthenticator( 270 username, password); 271 session = Session.getDefaultInstance(props, smtpAuthenticator); 272 } 273 else 274 { 275 session = Session.getDefaultInstance(props); 276 } 277 278 MimeMessage message = new MimeMessage (session); 280 281 Iterator i = recipients.iterator(); 283 284 while (i.hasNext()) 285 { 286 message.addRecipient(Message.RecipientType.TO, new InternetAddress ( 287 (String ) i.next())); 288 } 289 290 Object [] args = arguments.toArray(); 292 String fullMessage = MessageFormat.format(content, args); 293 Date date = new Date (); 294 295 message.setSentDate(date); 296 message.setFrom(new InternetAddress (from)); 297 message.setSubject(subject); 298 if (attachments.isEmpty()) 299 { 300 message.setText(fullMessage); 301 } 302 else 303 { 304 Multipart multipart = new MimeMultipart (); 305 BodyPart messageBodyPart = new MimeBodyPart (); 307 messageBodyPart.setText(fullMessage); 308 multipart.addBodyPart(messageBodyPart); 309 310 for (Iterator iter = attachments.iterator(); iter.hasNext();) 311 { 312 FileAttachment f = (FileAttachment) iter.next(); 313 messageBodyPart = new MimeBodyPart (); 315 messageBodyPart.setDataHandler(new DataHandler ( 316 new FileDataSource (f.file))); 317 messageBodyPart.setFileName(f.name); 318 multipart.addBodyPart(messageBodyPart); 319 } 320 message.setContent(multipart); 321 } 322 323 if (replyTo != null) 324 { 325 Address [] replyToAddr = new Address [1]; 326 replyToAddr[0] = new InternetAddress (replyTo); 327 message.setReplyTo(replyToAddr); 328 } 329 330 Transport.send(message); 331 } 332 333 339 private class FileAttachment 340 { 341 public FileAttachment(File f, String n) 342 { 343 this.file = f; 344 this.name = n; 345 } 346 347 File file; 348 349 String name; 350 } 351 352 355 private class SMTPAuthenticator extends Authenticator 356 { 357 private String name; 359 360 private String password; 362 363 public SMTPAuthenticator(String n, String p) 364 { 365 name = n; 366 password = p; 367 } 368 369 protected PasswordAuthentication getPasswordAuthentication() 370 { 371 return new PasswordAuthentication (name, password); 372 } 373 } 374 } 375 | Popular Tags |