KickJava   Java API By Example, From Geeks To Geeks.

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


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

12
13 package com.openedit.modules.email;
14
15 import java.io.UnsupportedEncodingException JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 import javax.mail.BodyPart JavaDoc;
20 import javax.mail.Message JavaDoc;
21 import javax.mail.MessagingException JavaDoc;
22 import javax.mail.Multipart JavaDoc;
23 import javax.mail.PasswordAuthentication JavaDoc;
24 import javax.mail.Session JavaDoc;
25 import javax.mail.Transport JavaDoc;
26 import javax.mail.internet.InternetAddress JavaDoc;
27 import javax.mail.internet.MimeBodyPart JavaDoc;
28 import javax.mail.internet.MimeMessage JavaDoc;
29 import javax.mail.internet.MimeMultipart JavaDoc;
30
31 import com.openedit.OpenEditRuntimeException;
32
33
34 public class PostMail
35 {
36     protected String JavaDoc fieldSmtpUsername;
37     protected String JavaDoc fieldSmtpPassword;
38     protected String JavaDoc fieldSmtpServer="localhost";
39     protected int fieldPort = 25;
40     protected boolean fieldSmtpSecured=false;
41     
42     public String JavaDoc getSmtpPassword() {
43         return fieldSmtpPassword;
44     }
45
46     public void setSmtpPassword(String JavaDoc inSmtpPassword) {
47         this.fieldSmtpPassword = inSmtpPassword;
48     }
49
50     public boolean isSmtpSecured() {
51         return fieldSmtpSecured;
52     }
53
54     public void setSmtpSecured(boolean inSmtpSecured) {
55         this.fieldSmtpSecured = inSmtpSecured;
56     }
57
58     public String JavaDoc getSmtpUsername() {
59         return fieldSmtpUsername;
60     }
61
62     public void setSmtpUsername(String JavaDoc inSmtpUsername) {
63         this.fieldSmtpUsername = inSmtpUsername;
64     }
65
66     public void postMail(String JavaDoc recipient, String JavaDoc subject, String JavaDoc message, String JavaDoc from)
67         throws MessagingException JavaDoc
68     {
69         postMail(new String JavaDoc[] { recipient }, subject, message, null, from);
70     }
71
72     //returns a new template web email instance preconfigured with sping settings.
73
public TemplateWebEmail getTemplateWebEmail(){
74         TemplateWebEmail email = new TemplateWebEmail();
75         email.setPostMail(this);
76         return email;
77     }
78     
79     public void postMail(
80         String JavaDoc[] recipients, String JavaDoc subject, String JavaDoc inHtml, String JavaDoc inText, String JavaDoc from)
81         throws MessagingException JavaDoc
82     {
83         //Set the host smtp address
84
Properties JavaDoc props = new Properties JavaDoc();
85         // create some properties and get the default Session
86
props.put("mail.smtp.host",fieldSmtpServer);
87         props.put("mail.smtp.port", String.valueOf(fieldPort) );
88         props.put("mail.smtp.auth",new Boolean JavaDoc(fieldSmtpSecured).toString());
89         //props.put("mail.host", server);
90
Session JavaDoc session;
91         //If we need to authenticate, create the authenticator
92
if(fieldSmtpSecured){
93         
94          SmtpAuthenticator auth = new SmtpAuthenticator();
95          session = Session.getDefaultInstance(props,auth);
96         }
97         else{
98              session = Session.getDefaultInstance(props);
99         }
100         //session.setDebug(debug);
101

102         // create a message
103
Message JavaDoc msg = new MimeMessage JavaDoc(session);
104
105         //msg.setDataHandler(new DataHandler(new ByteArrayDataSource(message, "text/html")));
106
if ( inText != null && inHtml != null)
107         {
108             // Create an "Alternative" Multipart message
109
Multipart JavaDoc mp = new MimeMultipart JavaDoc("alternative");
110
111             BodyPart JavaDoc messageBodyPart = new MimeBodyPart JavaDoc();
112             messageBodyPart.setContent(inText, "text/plain");
113             mp.addBodyPart(messageBodyPart);
114
115             messageBodyPart = new MimeBodyPart JavaDoc();
116             messageBodyPart.setContent(inHtml, "text/html");
117             mp.addBodyPart(messageBodyPart);
118
119             msg.setContent(mp);
120         }
121         else if ( inHtml != null)
122         {
123             msg.setContent(inHtml,"text/html");
124         }
125         else
126         {
127             msg.setContent(inText,"text/plain");
128         }
129         // set the from and to address
130
InternetAddress JavaDoc addressFrom = new InternetAddress JavaDoc(from);
131         msg.setFrom(addressFrom);
132         msg.setSentDate(new Date JavaDoc());
133         InternetAddress JavaDoc[] addressTo = new InternetAddress JavaDoc[recipients.length];
134
135         for (int i = 0; i < recipients.length; i++)
136         {
137             String JavaDoc rec = recipients[i];
138             addressTo[i] = new InternetAddress JavaDoc(rec);
139             String JavaDoc personal = addressTo[i].getPersonal();
140             if ( personal != null && personal.indexOf("\"") == -1)
141             {
142                 //check for commas or . and quote it if found
143
if ( personal.indexOf(",") > -1 || personal.indexOf(".") > -1 )
144                 {
145                     personal = "\"" + personal + "\"";
146                     try{
147                         addressTo[i].setPersonal(personal);
148                     } catch ( UnsupportedEncodingException JavaDoc ex)
149                     {
150                         throw new OpenEditRuntimeException(ex);
151                     }
152                 }
153             }
154         }
155
156         msg.setRecipients(Message.RecipientType.TO, addressTo);
157
158         // Optional : You can also set your custom headers in the Email if you Want
159
// msg.addHeader("MyHeaderName", "myHeaderValue");
160
// Setting the Subject and Content Type
161
msg.setSubject(subject);
162
163         //Transport tr = session.getTransport("smtp");
164
//tr.connect(serverandport[0], null, null);
165
//msg.saveChanges(); // don't forget this
166
//tr.sendMessage(msg, msg.getAllRecipients());
167
//tr.close();
168
//msg.setContent(msg, "text/plain");
169

170         Transport.send(msg);
171     }
172
173     public int getPort() {
174         return fieldPort;
175     }
176
177     public void setPort(int inPort) {
178         this.fieldPort = inPort;
179     }
180
181     public String JavaDoc getSmtpServer() {
182         return fieldSmtpServer;
183     }
184
185     public void setSmtpServer(String JavaDoc inSmtpServer) {
186         this.fieldSmtpServer = inSmtpServer;
187     }
188     public class SmtpAuthenticator extends javax.mail.Authenticator JavaDoc {
189         public PasswordAuthentication JavaDoc getPasswordAuthentication() {
190                  return new PasswordAuthentication JavaDoc(fieldSmtpUsername, fieldSmtpPassword);
191         }
192     }
193 }
194
Popular Tags