KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > util > mail > MailSenderPlugin


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.util.mail;
17
18 import java.util.Date JavaDoc;
19
20 import javax.servlet.ServletException JavaDoc;
21
22 import org.apache.struts.action.ActionServlet;
23 import org.apache.struts.action.PlugIn;
24 import org.apache.struts.config.ModuleConfig;
25
26 /**
27  * 邮件发送的插件
28  * 该插件在struts-config.xml中的配置如下
29     <plug-in className="dlog4j.util.mail.MailSenderPlugin">
30         <!-- 发件人邮件地址 -->
31         <set-property property="mail" value="[email]"/>
32         <!-- SMTP服务器地址 -->
33         <set-property property="host" value="[smtp_server_addr]"/>
34         <!-- SMTP服务器端口,如果不指定则使用默认25 -->
35         <set-property property="port" value="25"/>
36         <!-- 邮件帐号 -->
37         <set-property property="username" value="[account]"/>
38         <!-- 邮件密码 -->
39         <set-property property="password" value="[password]"/>
40     </plug-in>
41  * @author Winter Lau
42  */

43 public class MailSenderPlugin extends Mailer implements PlugIn {
44
45     protected String JavaDoc mail;
46     protected String JavaDoc host;
47     protected int port = 25;
48     protected String JavaDoc username;
49     protected String JavaDoc password;
50     
51     private ActionServlet servlet;
52     
53     /* (non-Javadoc)
54      * @see org.apache.struts.action.PlugIn#init(org.apache.struts.action.ActionServlet, org.apache.struts.config.ModuleConfig)
55      */

56     public void init(ActionServlet servlet, ModuleConfig config) throws ServletException JavaDoc {
57         this.servlet = servlet;
58         mailer = this;
59     }
60
61     /**
62      * 邮件发送
63      * @param mails
64      * @param title
65      * @param content
66      * @param isHtml
67      */

68     public void send(final String JavaDoc sendername,
69                      final String JavaDoc[] mails,
70                      final String JavaDoc title,
71                      final String JavaDoc content)
72     {
73         new Thread JavaDoc(){
74             public void run(){
75                 MailSender sender = MailSender.getHtmlMailSender(host,port,username,password);
76                 try{
77                     sender.setSubject(title);
78                     sender.setSendDate(new Date JavaDoc());
79                     sender.setMailFrom(mail, sendername);
80                     sender.setMailContent(content);
81                     sender.setMailTo(mails, "to");
82                     sender.sendMail();
83                 }catch(Exception JavaDoc e){
84                     servlet.log("发送邮件失败,title="+title+",content="+content,e);
85                 }
86             }
87         }.start();
88     }
89     
90     /* (non-Javadoc)
91      * @see org.apache.struts.action.PlugIn#destroy()
92      */

93     public void destroy() {
94     }
95
96     public String JavaDoc getHost() {
97         return host;
98     }
99     public void setHost(String JavaDoc host) {
100         this.host = host;
101     }
102     public String JavaDoc getMail() {
103         return mail;
104     }
105     public void setMail(String JavaDoc mail) {
106         this.mail = mail;
107     }
108     public String JavaDoc getPassword() {
109         return password;
110     }
111     public void setPassword(String JavaDoc password) {
112         this.password = password;
113     }
114     public int getPort() {
115         return port;
116     }
117     public void setPort(int port) {
118         this.port = port;
119     }
120     public String JavaDoc getUsername() {
121         return username;
122     }
123     public void setUsername(String JavaDoc username) {
124         this.username = username;
125     }
126 }
127
Popular Tags