KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > util > Mailer


1 package com.dotmarketing.util;
2
3
4 /*
5  * mail class using using javamail API.
6  * Sends an email and writes the email to a file
7  */

8 import java.io.UnsupportedEncodingException JavaDoc;
9 import java.util.Properties JavaDoc;
10 import javax.activation.DataHandler JavaDoc;
11 import javax.activation.FileDataSource JavaDoc;
12 import javax.mail.Message JavaDoc;
13 import javax.mail.MessagingException JavaDoc;
14 import javax.mail.Session JavaDoc;
15 import javax.mail.Transport JavaDoc;
16 import javax.mail.internet.InternetAddress JavaDoc;
17 import javax.mail.internet.MimeBodyPart JavaDoc;
18 import javax.mail.internet.MimeMessage JavaDoc;
19 import javax.mail.internet.MimeMultipart JavaDoc;
20
21 /**
22  * Description of the Class
23  *
24  *@author will
25  *@created May 8, 2002
26  */

27 public class Mailer {
28     MimeMultipart JavaDoc sendingAttachments;
29     MimeMultipart JavaDoc sendingText;
30     String JavaDoc bcc;
31     String JavaDoc cc;
32     String JavaDoc errorMessage;
33     String JavaDoc fromEmail;
34     String JavaDoc fromName;
35     String JavaDoc result;
36     String JavaDoc subject;
37     String JavaDoc toEmail;
38     String JavaDoc toName;
39     long recipientId;
40
41     public Mailer() {
42         result = null;
43         sendingAttachments = new MimeMultipart JavaDoc();
44         sendingText = new MimeMultipart JavaDoc();
45         errorMessage = null;
46     }
47
48     public void setBcc(String JavaDoc x) {
49         bcc = x;
50     }
51
52
53     /**
54      * Returns the bcc.
55      * @return String
56      */

57     public String JavaDoc getBcc() {
58         return bcc;
59     }
60
61     public void setCc(String JavaDoc x) {
62         cc = x;
63     }
64
65     /**
66      * Returns the cc.
67      * @return String
68      */

69     public String JavaDoc getCc() {
70         return cc;
71     }
72
73     /**
74      * Sets the errorMessage.
75      * @param errorMessage The errorMessage to set
76      */

77     public void setErrorMessage(String JavaDoc errorMessage) {
78         if(errorMessage != null)
79         
80         this.errorMessage = errorMessage.trim();
81     }
82
83     /**
84      * Returns the errorMessage.
85      * @return String
86      */

87     public String JavaDoc getErrorMessage() {
88         return errorMessage;
89     }
90
91     /**
92      * Sets the fromEmail.
93      * @param fromEmail The fromEmail to set
94      */

95     public void setFromEmail(String JavaDoc fromEmail) {
96         this.fromEmail = fromEmail;
97     }
98
99     /**
100      * Returns the fromEmail.
101      * @return String
102      */

103     public String JavaDoc getFromEmail() {
104         return fromEmail;
105     }
106
107     public void setFromName(String JavaDoc x) {
108         fromName = x;
109     }
110
111     /**
112      * Returns the fromName.
113      * @return String
114      */

115     public String JavaDoc getFromName() {
116         return fromName;
117     }
118
119     public void setHTMLAndTextBody(String JavaDoc HTMLBody) {
120         try {
121             MimeBodyPart JavaDoc bp = new MimeBodyPart JavaDoc();
122             bp.setContent(HTMLBody, "text/html");
123             sendingText.addBodyPart(bp);
124             bp = new MimeBodyPart JavaDoc();
125
126             String JavaDoc x = HTMLBody.replaceAll("<[^>]*>", " ");
127
128             while (x.indexOf(" ") > -1) {
129                 x = x.replaceAll(" ", " ");
130             }
131
132             setTextBody(x);
133         } catch (Exception JavaDoc f) {
134             Logger.error(this, "failed to setHTMLAndTextBody:" + f, f);
135         }
136     }
137
138     public void setHTMLBody(String JavaDoc HTMLBody) {
139         try {
140             MimeBodyPart JavaDoc bp = new MimeBodyPart JavaDoc();
141             bp.setContent(HTMLBody, "text/html");
142             sendingText.addBodyPart(bp);
143         } catch (Exception JavaDoc f) {
144             Logger.error(this, "failed to setHTMLBody:" + f, f);
145         }
146     }
147
148     /**
149      * Sets the recipientId.
150      * @param recipientId The recipientId to set
151      */

152     public void setRecipientId(long recipientId) {
153         this.recipientId = recipientId;
154     }
155
156     /**
157      * Returns the recipientId.
158      * @return int
159      */

160     public long getRecipientId() {
161         return recipientId;
162     }
163
164     /**
165      * Returns the result.
166      * @return String
167      */

168     public String JavaDoc getResult() {
169         return result;
170     }
171
172     public void setSubject(String JavaDoc x) {
173         subject = x;
174     }
175
176     /**
177      * Returns the subject.
178      * @return String
179      */

180     public String JavaDoc getSubject() {
181         return subject;
182     }
183
184     public void setTextBody(String JavaDoc TextBody) {
185         try {
186             MimeBodyPart JavaDoc bp = new MimeBodyPart JavaDoc();
187             bp.setContent(TextBody, "text/plain");
188             sendingText.addBodyPart(bp, 0);
189         } catch (Exception JavaDoc f) {
190             Logger.error(this, "failed to setTextBody:" + f, f);
191         }
192     }
193
194     /**
195      * Sets the toEmail.
196      * @param toEmail The toEmail to set
197      */

198     public void setToEmail(String JavaDoc toEmail) {
199         this.toEmail = toEmail;
200     }
201
202     /**
203      * Returns the toEmail.
204      * @return String
205      */

206     public String JavaDoc getToEmail() {
207         return toEmail;
208     }
209
210     public void setToName(String JavaDoc x) {
211         toName = x;
212     }
213
214     /**
215      * Returns the toName.
216      * @return String
217      */

218     public String JavaDoc getToName() {
219         return toName;
220     }
221
222     public void addAttachment(java.io.File JavaDoc file) {
223         if ((file != null) && file.exists() && !file.isDirectory()) {
224             try {
225                 MimeBodyPart JavaDoc mbp = new MimeBodyPart JavaDoc();
226                 FileDataSource JavaDoc fds = new FileDataSource JavaDoc(file.getAbsolutePath());
227                 mbp.setDataHandler(new DataHandler JavaDoc(fds));
228                 mbp.setFileName(file.getName());
229                 sendingAttachments.addBodyPart(mbp);
230             } catch (Exception JavaDoc f) {
231                 Logger.error(this, "failed to addAttachment:" + f, f);
232             }
233         }
234     }
235
236     /**
237      * Description of the Method
238      */

239     public boolean sendMessage() {
240         MimeMultipart JavaDoc mp = new MimeMultipart JavaDoc();
241
242         try {
243             //if there is a text and an html section
244
if (sendingText.getCount() > 1) {
245                 sendingText.setSubType("alternative");
246             }
247
248             //if we are sending attachments
249
if (sendingAttachments.getCount() > 0) {
250                 MimeBodyPart JavaDoc bp = new MimeBodyPart JavaDoc();
251                 bp.setContent(sendingAttachments);
252                 mp.addBodyPart(bp, 0);
253                 bp = new MimeBodyPart JavaDoc();
254                 bp.setContent(sendingText);
255                 mp.addBodyPart(bp, 0);
256             } else {
257                 mp = sendingText;
258             }
259
260
261             // Get system properties
262
Properties JavaDoc props = System.getProperties();
263
264             // Setup mail server
265
String JavaDoc smtpServer = Config.getStringProperty("SMTP_SERVER");
266             props.put("mail.smtp.host", smtpServer);
267
268             Logger.debug(this, "Delivering mail using: " + smtpServer + " as server.");
269             
270             // Get session
271
Session JavaDoc session = Session.getDefaultInstance(props, null);
272             MimeMessage JavaDoc message = new MimeMessage JavaDoc(session);
273
274             if ((fromEmail != null) && (fromName != null)) {
275                 message.setFrom(new InternetAddress JavaDoc(fromEmail, fromName));
276             } else if (fromEmail != null) {
277                 message.setFrom(new InternetAddress JavaDoc(fromEmail));
278             }
279
280             if (toName != null) {
281                 String JavaDoc[] recipients = toEmail.split("[;,]");
282                 for (String JavaDoc recipient : recipients) {
283                     message.addRecipient(Message.RecipientType.TO, new InternetAddress JavaDoc(recipient, toName));
284                 }
285             } else {
286                 String JavaDoc[] recipients = toEmail.split("[;,]");
287                 for (String JavaDoc recipient : recipients) {
288                     message.addRecipient(Message.RecipientType.TO, new InternetAddress JavaDoc(recipient));
289                 }
290             }
291             if (UtilMethods.isSet(cc)) {
292                 String JavaDoc[] recipients = cc.split("[;,]");
293                 for (String JavaDoc recipient : recipients) {
294                     message.addRecipient(Message.RecipientType.CC, new InternetAddress JavaDoc(recipient));
295                 }
296             }
297             if (UtilMethods.isSet(bcc)) {
298                 String JavaDoc[] recipients = bcc.split("[;,]");
299                 for (String JavaDoc recipient : recipients) {
300                     message.addRecipient(Message.RecipientType.BCC, new InternetAddress JavaDoc(recipient));
301                 }
302             }
303
304             message.setSubject(subject);
305             message.setContent(mp);
306             Transport.send(message);
307             result = "Send Ok";
308             return true;
309         } catch (javax.mail.SendFailedException JavaDoc f) {
310             String JavaDoc error = String.valueOf(f);
311             errorMessage = error.substring(error.lastIndexOf("javax.mail.SendFailedException:") + "javax.mail.SendFailedException:".length(), (error.length()-1));
312             result = "Failed:" + error;
313             Logger.error(Mailer.class, f.toString(), f);
314             return false;
315         } catch (MessagingException JavaDoc f) {
316             String JavaDoc error = String.valueOf(f);
317             errorMessage = error.substring(error.lastIndexOf("javax.mail.MessagingException:")+ "javax.mail.MessagingException:".length(), (error.length()-1));
318             result = "Failed:" + error;
319             Logger.error(Mailer.class, f.toString(), f);
320             return false;
321         }
322             catch (UnsupportedEncodingException JavaDoc f) {
323             String JavaDoc error = String.valueOf(f);
324             errorMessage = error.substring(error.lastIndexOf("java.io.UnsupportedEncodingException:") + "java.io.UnsupportedEncodingException:".length(), (error.length()-1));
325             result = "Failed:" + error ;
326             Logger.error(Mailer.class, f.toString(), f);
327             return false;
328         }
329         
330         
331         
332         
333         
334         
335     }
336 }
337
Popular Tags