KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > actions > email > MailSupport


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.actions.email;
27
28 import net.killingar.Utils;
29 import net.killingar.actions.ActionSupport;
30 import webwork.action.CleanupAware;
31
32 import javax.mail.Store JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 public abstract class MailSupport
36     extends ActionSupport
37     implements CleanupAware
38 {
39     // Types ---------------------------------------------------------
40

41     // Attributes ----------------------------------------------------
42
protected String JavaDoc
43         from,
44         smtpServer,
45         username,
46         password,
47         hostname,
48         protocol = "pop3";
49
50     protected Integer JavaDoc port;
51
52     protected javax.mail.Session JavaDoc mailSession;
53     protected Store JavaDoc mailStore;
54
55     // getters
56
public String JavaDoc getFrom() { return from; }
57     public String JavaDoc getSmtpServer() { return smtpServer; }
58
59     // setters
60
public void setFrom (String JavaDoc in) { from = in; }
61     public void setSmtpServer (String JavaDoc in) { smtpServer = in; }
62     public void setUsername (String JavaDoc in) { username = in; }
63     public void setPassword (String JavaDoc in) { password = in; }
64     public void setProtocol (String JavaDoc in) { protocol = in; }
65     public void setPort (Integer JavaDoc in){ port = in; }
66
67     // Implementation
68
public String JavaDoc execute() throws Exception JavaDoc
69     {
70         try
71         {
72             System.err.println("executing mail.Mailsupport.execute()");
73             //javax.servlet.http.HttpServletRequest req = webwork.action.ServletActionContext.getRequest();
74

75             /*
76             String
77                 protocol = "pop3";// = mgr.getOptions().get("email protocol"),
78
79             username = "test";// = mgr.getOptions().get("email username"),
80             password = "test";// = mgr.getOptions().get("email password"),
81             hostname = "localhost";// = mgr.getOptions().get("email hostname");
82             smtpServer = "smtpserver.swip.net";
83             from = "test@killingar.net";
84             */

85
86             if (port == null) // set default
87
{
88                 if (protocol != null)
89                     port = new Integer JavaDoc(protocol.equals("pop3")?110:143);
90             }
91
92             // connect
93
Properties JavaDoc props = Utils.getProperties("net.killingar.email");
94             if (protocol != null)props.put("mail.store.protocol", protocol);
95             if (protocol != null)props.put("mail.transport.protocol", protocol);
96             if (smtpServer != null)props.put("mail.smtp.host", smtpServer);//"smtp.home.se"); // mail server
97
if (hostname != null)props.put("mail.host", hostname); // mail server
98
if (username != null)props.put("mail.user", username); // mail username
99
if (from != null)props.put("mail.from", from);
100             props.put("mail.debug", "false");
101             mailSession = javax.mail.Session.getInstance(props);
102             mailStore = mailSession.getStore(protocol);
103             mailStore.connect(hostname, port.intValue(), username, password);
104
105             String JavaDoc result = doExecute();//super.execute();
106

107             return result;
108         }
109         catch (Exception JavaDoc e)
110         {
111             addErrorMessage("getting mailbox failed, exception thrown ("+e.toString()+")");
112             e.printStackTrace();
113
114             return ERROR;
115         }
116     }
117
118     public void cleanup()
119     {
120         try
121         {
122             mailStore.close();
123         }
124         catch (Exception JavaDoc e){} // ignore all errors
125
}
126 }
127
Popular Tags