KickJava   Java API By Example, From Geeks To Geeks.

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


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.actions.ActionSupport;
29
30 import java.util.Properties JavaDoc;
31
32 public class Send extends ActionSupport
33 {
34     String JavaDoc
35         from,
36         to,
37         cc,
38         subject,
39         body,
40         smtpServer;
41
42     // Attributes ----------------------------------------------------
43

44     // getters
45
public String JavaDoc getFrom () { return from; }
46     public String JavaDoc getTo () { return to; }
47     public String JavaDoc getCc () { return cc; }
48     public String JavaDoc getSubject() { return subject; }
49     public String JavaDoc getBody () { return body; }
50     public String JavaDoc getSmtpServer() { return smtpServer; }
51
52     // setters
53
public void setFrom (String JavaDoc in) { from = in; }
54     public void setTo (String JavaDoc in) { to = in; }
55     public void setCc (String JavaDoc in) { cc = in; }
56     public void setSubject(String JavaDoc in) { subject = in; }
57     public void setBody (String JavaDoc in) { body = in; }
58     public void setSmtpServer(String JavaDoc in) { smtpServer = in; }
59
60     // Implementation
61
public void doValidation()
62     {
63         if (to == null || to.length() == 0)
64             addError("to", "no to mail address specified");
65
66         if (subject == null)
67             addError("subject", "no subject");
68         if (body == null)
69             addError("body", "no body");
70     if (smtpServer == null)
71             addErrorMessage("no smtp server specified");
72         if (from == null)
73             addError("from", "no from field");
74     }
75
76     public String JavaDoc doExecute()
77     {
78         try
79         {
80             Properties JavaDoc props = System.getProperties();
81             props.put("mail.smtp.host", smtpServer);
82             javax.mail.Session JavaDoc mailSession = javax.mail.Session.getInstance(props);
83             javax.mail.Message JavaDoc mail = new javax.mail.internet.MimeMessage JavaDoc(mailSession);
84
85             mail.setFrom(new javax.mail.internet.InternetAddress JavaDoc(from));
86             mail.setContent(body, "text/plain"); // FIXME could also support html email
87
mail.setSubject(subject);
88             mail.setRecipient(javax.mail.Message.RecipientType.TO, new javax.mail.internet.InternetAddress JavaDoc(to));
89             if (cc != null && cc.length() != 0)
90                 mail.setRecipient(javax.mail.Message.RecipientType.CC, new javax.mail.internet.InternetAddress JavaDoc(cc));
91             //mail.setRecipient(javax.mail.Message.RecipientType.BCC, new javax.mail.internet.InternetAddress(user.email));
92
mail.setSentDate(new java.util.Date JavaDoc());
93
94             javax.mail.Transport.send(mail);
95         }
96         catch (Exception JavaDoc e)
97         {
98             addErrorMessage("sending email failed, exception thrown ("+e.toString()+")");
99             e.printStackTrace();
100
101             return ERROR;
102         }
103
104         return SUCCESS;
105     }
106 }
107
Popular Tags