KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > common > SendMailUtil


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/common/SendMailUtil.java,v 1.16 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.16 $
5  * $Date: 2006/04/14 17:05:26 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.common;
42
43 import java.io.*;
44 import java.util.HashMap JavaDoc;
45 import java.util.Map JavaDoc;
46
47 import javax.mail.MessagingException JavaDoc;
48
49 import com.mvnforum.*;
50 import com.mvnforum.db.DAOFactory;
51 import com.mvnforum.db.MemberBean;
52 import com.mvnforum.user.UserModuleConfig;
53 import freemarker.template.*;
54 import net.myvietnam.mvncore.exception.*;
55 import net.myvietnam.mvncore.util.*;
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58
59
60 public class SendMailUtil {
61
62     private static Log log = LogFactory.getLog(SendMailUtil.class);
63
64     private SendMailUtil() {
65     }
66
67     public static void sendActivationCodeEmail(int memberID, String JavaDoc serverName)
68         throws ObjectNotFoundException, DatabaseException, BadInputException, MessagingException JavaDoc, IOException, TemplateException {
69
70         MailMessageStruct mailMessageStruct = getActivationCodeEmail(memberID, serverName);
71         try {
72             MailUtil.sendMail(mailMessageStruct);
73         } catch (UnsupportedEncodingException e) {
74             log.error("Cannot support encoding", e);
75         }
76     }
77
78     // This method can be optimized by accept input of type MemberBean
79
public static MailMessageStruct getActivationCodeEmail(int memberID, String JavaDoc serverName)
80         throws ObjectNotFoundException, DatabaseException, BadInputException, IOException, TemplateException {
81
82         // Now, check that this member is not activated, to prevent the
83
// situation that other people try to annoy this member
84
String JavaDoc activateCode = DAOFactory.getMemberDAO().getActivateCode(memberID);
85         if (activateCode.equals(MemberBean.MEMBER_ACTIVATECODE_ACTIVATED)) {
86             //@todo : localize me
87
throw new BadInputException("Cannot activate an already activated member.");
88         }
89
90         MemberBean memberBean = DAOFactory.getMemberDAO().getMember_forPublic(memberID);
91         String JavaDoc memberName = memberBean.getMemberName();
92         String JavaDoc memberEmail = memberBean.getMemberEmail();
93
94         // generate a Activation code
95
// Note that the activation code does not need security MD5 as in the Password Reset
96
if (activateCode.equals("")) {
97             // only generate activate code when the current value is empty
98
// that is, if there is an activate code, re-use it.
99
activateCode = String.valueOf(System.currentTimeMillis());
100             DAOFactory.getMemberDAO().updateActivateCode(memberID, activateCode);
101         }
102
103         // we have pass the assertion check, go ahead
104
StringBuffer JavaDoc activationUrl = new StringBuffer JavaDoc(256);
105         activationUrl.append(serverName);
106         activationUrl.append(ParamUtil.getContextPath());
107         activationUrl.append(UserModuleConfig.getUrlPattern());
108         activationUrl.append("/activatemember?activatecode=");
109         activationUrl.append(activateCode);
110         activationUrl.append("&member=");
111         activationUrl.append(memberName);
112
113         // Prepare the FreeMarker configuration;
114
Configuration cfg = MVNForumConfig.getFreeMarkerConfiguration();
115
116         //Below is a code to map content of email to template
117
Map JavaDoc root = new HashMap JavaDoc();
118         root.put("serverName", serverName);
119         root.put("MVNForumInfo", MVNForumInfo.getProductDesc());
120         root.put("activationUrl", activationUrl.toString());
121         root.put("memberName", memberName);
122         root.put("activateCode", activateCode);
123
124         StringWriter subjectWriter = new StringWriter(256);
125         Template subjectTemplate = cfg.getTemplate(MVNForumGlobal.TEMPLATE_SENDACTIVATECODE_SUBJECT, "UTF-8");
126         subjectTemplate.process(root, subjectWriter);
127         String JavaDoc subject = subjectWriter.toString();
128
129         StringWriter bodyWriter = new StringWriter(1024);
130         Template bodyTemplate = cfg.getTemplate(MVNForumGlobal.TEMPLATE_SENDACTIVATECODE_BODY, "UTF-8");
131         bodyTemplate.process(root, bodyWriter);
132         String JavaDoc body = bodyWriter.toString();
133
134         log.debug("subject = " + subject);
135         log.debug("body = " + body);
136
137         MailMessageStruct mailMessage = new MailMessageStruct();
138         mailMessage.setFrom(MVNForumConfig.getWebMasterEmail());
139         mailMessage.setTo(memberEmail);
140         mailMessage.setSubject(subject);
141         mailMessage.setMessage(body);
142
143         return mailMessage;
144     }
145 }
146
Popular Tags