001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.util.mail;
017:
018: import java.util.Date;
019:
020: import javax.servlet.ServletException;
021:
022: import org.apache.struts.action.ActionServlet;
023: import org.apache.struts.action.PlugIn;
024: import org.apache.struts.config.ModuleConfig;
025:
026: /**
027: * 邮件发送的插件
028: * 该插件在struts-config.xml中的配置如下
029: <plug-in className="dlog4j.util.mail.MailSenderPlugin">
030: <!-- 发件人邮件地址 -->
031: <set-property property="mail" value="[email]"/>
032: <!-- SMTP服务器地址 -->
033: <set-property property="host" value="[smtp_server_addr]"/>
034: <!-- SMTP服务器端口,如果不指定则使用默认25 -->
035: <set-property property="port" value="25"/>
036: <!-- 邮件帐号 -->
037: <set-property property="username" value="[account]"/>
038: <!-- 邮件密码 -->
039: <set-property property="password" value="[password]"/>
040: </plug-in>
041: * @author Winter Lau
042: */
043: public class MailSenderPlugin extends Mailer implements PlugIn {
044:
045: protected String mail;
046: protected String host;
047: protected int port = 25;
048: protected String username;
049: protected String password;
050:
051: private ActionServlet servlet;
052:
053: /* (non-Javadoc)
054: * @see org.apache.struts.action.PlugIn#init(org.apache.struts.action.ActionServlet, org.apache.struts.config.ModuleConfig)
055: */
056: public void init(ActionServlet servlet, ModuleConfig config)
057: throws ServletException {
058: this .servlet = servlet;
059: mailer = this ;
060: }
061:
062: /**
063: * 邮件发送
064: * @param mails
065: * @param title
066: * @param content
067: * @param isHtml
068: */
069: public void send(final String sendername, final String[] mails,
070: final String title, final String content) {
071: new Thread() {
072: public void run() {
073: MailSender sender = MailSender.getHtmlMailSender(host,
074: port, username, password);
075: try {
076: sender.setSubject(title);
077: sender.setSendDate(new Date());
078: sender.setMailFrom(mail, sendername);
079: sender.setMailContent(content);
080: sender.setMailTo(mails, "to");
081: sender.sendMail();
082: } catch (Exception e) {
083: servlet.log("发送邮件失败,title=" + title
084: + ",content=" + content, e);
085: }
086: }
087: }.start();
088: }
089:
090: /* (non-Javadoc)
091: * @see org.apache.struts.action.PlugIn#destroy()
092: */
093: public void destroy() {
094: }
095:
096: public String getHost() {
097: return host;
098: }
099:
100: public void setHost(String host) {
101: this .host = host;
102: }
103:
104: public String getMail() {
105: return mail;
106: }
107:
108: public void setMail(String mail) {
109: this .mail = mail;
110: }
111:
112: public String getPassword() {
113: return password;
114: }
115:
116: public void setPassword(String password) {
117: this .password = password;
118: }
119:
120: public int getPort() {
121: return port;
122: }
123:
124: public void setPort(int port) {
125: this .port = port;
126: }
127:
128: public String getUsername() {
129: return username;
130: }
131:
132: public void setUsername(String username) {
133: this.username = username;
134: }
135: }
|