| 1 2 package com.espada.bugtracker.util; 3 4 import java.net.URL ; 5 import javax.activation.DataHandler ; 6 import javax.activation.URLDataSource ; 7 import javax.mail.*; 8 import javax.mail.internet.MimeBodyPart ; 9 import javax.mail.internet.MimeMultipart ; 10 11 14 public class MultiPartEmail extends Email 15 { 16 17 protected void init(String mailserver) 18 throws MessagingException 19 { 20 super.init(mailserver); 21 fileServer = null; 22 emailBody = new MimeMultipart (); 23 message.setContent(emailBody); 24 main = new MimeBodyPart (); 25 emailBody.addBodyPart(main); 26 } 27 28 public Email setMsg(String s) 29 throws MessagingException 30 { 31 main.setText(s); 32 return this; 33 } 34 35 public MultiPartEmail attach(EmailAttachment emailattachment) 36 throws MessagingException 37 { 38 URL url = emailattachment.getURL(); 39 if(url == null) 40 try 41 { 42 String s = emailattachment.getPath(); 43 url = new URL ("file", fileServer, s); 44 } 45 catch(Exception exception) 46 { 47 throw new MessagingException("Cannot find file", exception); 48 } 49 return attach(url, emailattachment.getName(), emailattachment.getDescription(), emailattachment.getDisposition()); 50 } 51 52 public MultiPartEmail attach(URL url, String s, String s1) 53 throws MessagingException 54 { 55 return attach(url, s, s1, "attachment"); 56 } 57 58 public MultiPartEmail attach(URL url, String s, String s1, String s2) 59 throws MessagingException 60 { 61 MimeBodyPart mimebodypart = new MimeBodyPart (); 62 emailBody.addBodyPart(mimebodypart); 63 mimebodypart.setDisposition(s2); 64 mimebodypart.setFileName(s); 65 mimebodypart.setDescription(s1); 66 mimebodypart.setDataHandler(new DataHandler (new URLDataSource (url))); 67 return this; 68 } 69 70 public MultiPartEmail(String mailserver) 71 throws MessagingException 72 { 73 fileServer = null; 74 init(mailserver); 75 } 76 77 protected MultiPartEmail() 78 throws MessagingException 79 { 80 fileServer = null; 81 } 82 83 protected MimeMultipart emailBody; 84 protected MimeBodyPart main; 85 private String fileServer; 86 } 87 | Popular Tags |