KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > infra > ThreadMail


1 package org.javabb.infra;
2
3 import java.util.Properties JavaDoc;
4
5 import javax.mail.MessagingException JavaDoc;
6 import javax.mail.internet.MimeMessage JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.springframework.mail.MailException;
11 import org.springframework.mail.javamail.JavaMailSenderImpl;
12 import org.springframework.mail.javamail.MimeMessageHelper;
13
14 /*
15  * Copyright 2004 JavaFree.org
16  *
17  * Licensed under the Apache License, Version 2.0 (the "License");
18  * you may not use this file except in compliance with the License.
19  * You may obtain a copy of the License at
20  *
21  * http://www.apache.org/licenses/LICENSE-2.0
22  *
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  */

29
30 /**
31  * $Id: ThreadMail.java,v 1.3.8.2 2006/04/17 17:47:16 daltoncamargo Exp $
32  *
33  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org
34  * </a> <br>
35  */

36 public class ThreadMail extends Thread JavaDoc {
37
38     private static final Log log = LogFactory.getLog(ThreadMail.class);
39
40     //System objects
41
private MimeMessage JavaDoc message;
42
43     private JavaMailSenderImpl sender;
44
45     //parameters
46
private String JavaDoc mailFrom;
47
48     private String JavaDoc mailTo;
49
50     private String JavaDoc subject;
51
52     private String JavaDoc bodyMessage;
53
54     private boolean htmlMail;
55
56     /**
57      * @param mailFrom -
58      * Mail from
59      * @param mailTo -
60      * Use comma to multiple receivers
61      * @param subject -
62      * Subject of mail
63      * @param bodyMessage -
64      * Mail message
65      * @param htmlMail -
66      * Use true to send an HTML mail
67      */

68     public ThreadMail(String JavaDoc mailFrom, String JavaDoc mailTo, String JavaDoc subject,
69             String JavaDoc bodyMessage, boolean htmlMail) {
70
71         this.mailFrom = mailFrom;
72         this.mailTo = mailTo;
73         this.subject = subject;
74         this.bodyMessage = bodyMessage;
75         this.htmlMail = htmlMail;
76     }
77
78     /**
79      * Method to send mail
80      */

81     public void sendMail() throws MailException, MessagingException JavaDoc {
82
83         //Configurações do APP
84
Configuration conf = new Configuration();
85
86         //Iniciando a configuração do email
87
sender = new JavaMailSenderImpl();
88         sender.setHost(conf.smtpServerHost);
89
90         if (conf.smtpServerUserName != null
91                 && !"".equals(conf.smtpServerUserName)) {
92
93             Properties JavaDoc mailProps = new Properties JavaDoc();
94             mailProps.put("mail.smtp.auth", "true");
95             mailProps.put("mail.smtp.port", "25");
96             sender.setJavaMailProperties(mailProps);
97
98             sender.setUsername(conf.smtpServerUserName);
99             sender.setUsername(conf.smtpServerUserName);
100             sender.setPassword(conf.smtpServerUserPassword);
101         }
102
103         message = sender.createMimeMessage();
104         String JavaDoc reportMails = mailTo.replaceAll(",", ";");
105         String JavaDoc[] dests = reportMails.split(";");
106
107         for (int i = 0; i < dests.length; i++) {
108             //use the true flag to indicate you need a multipart message
109
MimeMessageHelper helper = new MimeMessageHelper(message, true);
110             //helper.setFrom(conf.adminMail);
111
helper.setFrom(mailFrom);
112             helper.setSubject(subject);
113
114             String JavaDoc destSendMail = dests[i].trim();
115
116             helper.setTo(destSendMail);
117
118             // use the true flag to indicate the text included is HTML
119
helper.setText(bodyMessage, htmlMail);
120
121             /*
122              * To attach some file, uncomment this block File file = new
123              * File("/mnt/c/dalton/java/javabb.war");
124              * helper.addAttachment("javabb.war",file);
125              */

126
127             log.info("Sending mail to " + destSendMail);
128             sender.send(message);
129             log.info("Mail was sent!");
130         }
131     }
132
133     /*
134      * @see java.lang.Runnable#run()
135      */

136     public void run() {
137         try {
138             sleep(2000);
139             sendMail();
140         } catch (MailException e) {
141             log.error(e);
142         } catch (MessagingException JavaDoc e) {
143             log.error(e);
144         } catch (InterruptedException JavaDoc e) {
145             log.error(e);
146         }
147     }
148
149 }
Popular Tags