KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > email > SendMailModule


1 /*
2  * Created on Sep 26, 2003
3  *
4 /*
5 Copyright (c) 2003 eInnovation Inc. All rights reserved
6
7 This library is free software; you can redistribute it and/or modify it under the terms
8 of the GNU Lesser General Public License as published by the Free Software Foundation;
9 either version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU Lesser General Public License for more details.
14 */

15 package com.openedit.modules.email;
16
17 import javax.mail.MessagingException JavaDoc;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import com.openedit.OpenEditException;
23 import com.openedit.WebPageRequest;
24 import com.openedit.modules.BaseModule;
25 import com.openedit.page.Page;
26
27 /**
28  * @author Matt Avery, mavery@einnovation.com
29  */

30 public class SendMailModule extends BaseModule
31 {
32     private static final Log log = LogFactory.getLog(SendMailModule.class);
33     public static final String JavaDoc EMAIL_SETTINGS = "emailsettings";
34     private PostMail postMail;
35     
36     public PostMail getPostMail() {
37         return postMail;
38     }
39
40     public void setPostMail(PostMail postMail) {
41         this.postMail = postMail;
42     }
43
44     public SendMailModule()
45     {
46         
47     }
48     
49     private TemplateWebEmail createTemplateEmail( WebPageRequest inContext) throws OpenEditException
50     {
51         TemplateWebEmail email = getPostMail().getTemplateWebEmail();//new TemplateWebEmail();
52
email.loadSettings(inContext, getPageManager());
53         return email;
54     }
55     
56 // private FormWebEmail createFormEmail( WebPageRequest inReq) throws OpenEditException
57
// {
58
// FormWebEmail email = new FormWebEmail();
59
// email.loadSettings(inReq);
60
// return email;
61
// }
62

63     public void sendEmail(WebPageRequest inContext)throws OpenEditException
64     {
65         
66         if ( inContext.getPageValue(EMAIL_SETTINGS) == null)
67         {
68             inContext.putPageValue(EMAIL_SETTINGS,createTemplateEmail(inContext));
69         }
70         TemplateWebEmail webmail = (TemplateWebEmail)inContext.getPageValue(SendMailModule.EMAIL_SETTINGS);
71         webmail.setPostMail(postMail);
72         sendEmail(inContext, webmail);
73     }
74     
75 /* public void sendFormEmail(WebPageRequest inReq) throws OpenEditException
76     {
77         WebEmail webmail = createFormEmail(inReq);
78         webmail.setPostMail(postMail);
79         sendEmail(inReq, webmail);
80     }*/

81     
82     protected void sendEmail(WebPageRequest inReq, WebEmail inWebmail) throws OpenEditException
83     {
84         Page page = inReq.getPage();
85         //use send to send them
86
try
87         {
88             inWebmail.send();
89         }
90         catch (MessagingException JavaDoc e)
91         {
92             log.error(e);
93             String JavaDoc errorpage = page.get("error_page");
94             if (errorpage == null || errorpage.length() <= 0)
95                 errorpage = inReq.getRequestParameter( "error_page" );
96             if ( errorpage != null)
97             {
98                 inReq.redirect( errorpage );
99                 return;
100             }
101
102         }
103         String JavaDoc successpage = page.get("success_page");
104         if (successpage == null || successpage.length() <= 0)
105             successpage = inReq.getRequestParameter( "success_page" );
106         if ( successpage != null)
107         {
108             inReq.redirect( successpage );
109         }
110     }
111
112 }
113
Popular Tags