1 2 3 package com.espada.bugtracker.util; 4 5 import java.util.*; 6 import javax.mail.*; 7 import javax.mail.internet.InternetAddress ; 8 import javax.mail.internet.MimeMessage ; 9 10 public abstract class Email 11 { 12 13 private Session getMailSession(String mailserver) 14 { 15 java.util.Properties properties = System.getProperties(); 16 properties.put("mail.transport.protocol", "SMTP"); 17 properties.put("mail.host", mailserver); 18 return Session.getDefaultInstance(properties, null); 19 } 20 21 protected void init(String mailserver) 22 throws MessagingException 23 { 24 message = new MimeMessage (getMailSession(mailserver)); 25 toList = new Vector(); 26 ccList = new Vector(); 27 bccList = new Vector(); 28 replyList = new Vector(); 29 setSentDate(new Date()); 30 } 31 32 public Email setFrom(String s, String s1) 33 throws MessagingException 34 { 35 try 36 { 37 if(s1 == null || s1.trim().equals("")) 38 s1 = s; 39 message.setFrom(new InternetAddress (s, s1)); 40 } 41 catch(Exception exception) 42 { 43 throw new MessagingException("cannot set from", exception); 44 } 45 return this; 46 } 47 48 public Email addTo(String s, String s1) 49 throws MessagingException 50 { 51 try 52 { 53 if(s1 == null || s1.trim().equals("")) 54 s1 = s; 55 toList.addElement(new InternetAddress (s, s1)); 56 } 57 catch(Exception exception) 58 { 59 throw new MessagingException("cannot add to", exception); 60 } 61 return this; 62 } 63 64 public Email addCc(String s, String s1) 65 throws MessagingException 66 { 67 try 68 { 69 if(s1 == null || s1.trim().equals("")) 70 s1 = s; 71 ccList.addElement(new InternetAddress (s, s1)); 72 } 73 catch(Exception exception) 74 { 75 throw new MessagingException("cannot add cc", exception); 76 } 77 return this; 78 } 79 80 public Email addBcc(String s, String s1) 81 throws MessagingException 82 { 83 try 84 { 85 if(s1 == null || s1.trim().equals("")) 86 s1 = s; 87 bccList.addElement(new InternetAddress (s, s1)); 88 } 89 catch(Exception exception) 90 { 91 throw new MessagingException("cannot add bcc", exception); 92 } 93 return this; 94 } 95 96 public Email addReplyTo(String s, String s1) 97 throws MessagingException 98 { 99 try 100 { 101 if(s1 == null || s1.trim().equals("")) 102 s1 = s; 103 replyList.addElement(new InternetAddress (s, s1)); 104 } 105 catch(Exception exception) 106 { 107 throw new MessagingException("cannot add replyTo", exception); 108 } 109 return this; 110 } 111 112 public Email setSubject(String s) 113 throws MessagingException 114 { 115 message.setSubject(s); 116 return this; 117 } 118 119 public Email setSentDate(Date date) 120 throws MessagingException 121 { 122 message.setSentDate(date); 123 return this; 124 } 125 126 public abstract Email setMsg(String s) 127 throws MessagingException; 128 129 public void send() 130 throws MessagingException 131 { 132 InternetAddress ainternetaddress[] = new InternetAddress [0]; 133 message.setRecipients(javax.mail.Message.RecipientType.TO, toInternetAddressArray(toList)); 134 message.setRecipients(javax.mail.Message.RecipientType.CC, toInternetAddressArray(ccList)); 135 message.setRecipients(javax.mail.Message.RecipientType.BCC, toInternetAddressArray(bccList)); 136 message.setReplyTo(toInternetAddressArray(replyList)); 137 Transport.send(message); 138 } 139 140 private InternetAddress [] toInternetAddressArray(Vector vector) 141 { 142 int i = vector.size(); 143 InternetAddress ainternetaddress[] = new InternetAddress [i]; 144 for(int j = 0; j < i; j++) 145 ainternetaddress[j] = (InternetAddress )vector.elementAt(j); 146 147 return ainternetaddress; 148 } 149 150 public Email() 151 { 152 } 153 154 public static final String SENDER_EMAIL = "sender.email"; 155 public static final String SENDER_NAME = "sender.name"; 156 public static final String RECEIVER_EMAIL = "receiver.email"; 157 public static final String RECEIVER_NAME = "receiver.name"; 158 public static final String EMAIL_SUBJECT = "email.subject"; 159 public static final String EMAIL_BODY = "email.body"; 160 public static final String MAIL_SERVER = "mail.server"; 161 public static final String MAIL_HOST = "mail.host"; 162 public static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol"; 163 public static final String SMTP = "SMTP"; 164 public static final String TEXT_HTML = "text/html"; 165 public static final String TEXT_PLAIN = "text/plain"; 166 public static final String ATTACHMENTS = "attachments"; 167 public static final String FILE_SERVER = "file.server"; 168 Message message; 169 private Vector toList; 170 private Vector ccList; 171 private Vector bccList; 172 private Vector replyList; 173 } 174 | Popular Tags |