KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > espada > bugtracker > util > Email


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