KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > mail > Mail


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.util.mail;
27
28 import org.radeox.util.logging.Logger;
29 import org.snipsnap.app.Application;
30 import org.snipsnap.config.Configuration;
31
32 import javax.mail.*;
33 import javax.mail.internet.InternetAddress JavaDoc;
34 import javax.mail.internet.MimeBodyPart JavaDoc;
35 import javax.mail.internet.MimeMessage JavaDoc;
36 import javax.mail.internet.MimeMultipart JavaDoc;
37 import java.net.InetAddress JavaDoc;
38 import java.net.MalformedURLException JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.net.UnknownHostException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collection JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.Properties JavaDoc;
45
46 /**
47  * Mail manager to send and receive mail
48  *
49  * @author Stephan J. Schmidt
50  * @version $Id: Mail.java 1606 2004-05-17 10:56:18Z leo $
51  */

52
53 public class Mail {
54   public static Mail instance;
55
56   public static synchronized Mail getInstance() {
57     if (null == instance) {
58       instance = new Mail();
59     }
60     return instance;
61   }
62
63   private Session session;
64
65   public Mail() {
66     Properties JavaDoc props = new Properties JavaDoc();
67     Configuration config = Application.get().getConfiguration();
68     String JavaDoc mailhost = config.getMailHost();
69     InetAddress JavaDoc addr = null;
70     try {
71       addr = InetAddress.getByName(mailhost);
72     } catch (UnknownHostException JavaDoc e) {
73       Logger.debug("Mail: '" + mailhost + "': unknown host address");
74       try {
75         addr = InetAddress.getByName("mailhost");
76       } catch (UnknownHostException JavaDoc e1) {
77         try {
78           addr = InetAddress.getByName("mail");
79         } catch (UnknownHostException JavaDoc e2) {
80           Logger.debug("Mail: unable to detect mail host automatically, please configure by hand");
81         }
82       }
83     }
84
85     if (null != mailhost) {
86       props.put("mail.smtp.host", addr.getHostName());
87       session = Session.getDefaultInstance(props, null);
88       // session.setDebug(true); // Verbose!
89
}
90   }
91
92
93   public void sendMail(String JavaDoc sender, String JavaDoc recipient, String JavaDoc subject, String JavaDoc content) {
94     Collection JavaDoc recientList = new ArrayList JavaDoc();
95     recientList.add(recipient);
96     sendMail(sender, recientList, subject, content);
97   }
98
99   public void sendMail(String JavaDoc recipient, String JavaDoc subject, String JavaDoc content) {
100     Collection JavaDoc recipientList = new ArrayList JavaDoc();
101     recipientList.add(recipient);
102     //@TODO get admin mail / host
103
Configuration configuration = Application.get().getConfiguration();
104     String JavaDoc sender = configuration.getMailDomain();
105     if (null == sender) {
106       sender = configuration.getUrl();
107       try {
108         sender = new URL JavaDoc(sender).getHost();
109       } catch (MalformedURLException JavaDoc e) {
110         sender = "this-is-a-bug.org";
111       }
112     }
113     sendMail("do-not-reply@" + sender, recipientList, subject, content);
114   }
115
116   public void sendMail(String JavaDoc sender, Collection JavaDoc recipientList, String JavaDoc subject, String JavaDoc content) {
117     try {
118       Message JavaDoc mesg = new MimeMessage JavaDoc(session);
119       mesg.setFrom(new InternetAddress JavaDoc(sender));
120 // InternetAddress toAddress = null;
121
Iterator JavaDoc iterator = recipientList.iterator();
122       while (iterator.hasNext()) {
123         String JavaDoc recpt = (String JavaDoc) iterator.next();
124         mesg.addRecipient(Message.RecipientType.TO, new InternetAddress JavaDoc(recpt));
125       }
126
127       // CC Address
128
//InternetAddress ccAddress = new InternetAddress(message_cc);
129
//mesg.addRecipient(Message.RecipientType.CC, ccAddress);
130
mesg.setSubject(subject);
131
132       // Now the message body.
133
Multipart JavaDoc mp = new MimeMultipart JavaDoc();
134
135       //BodyPart textPart = new MimeBodyPart();
136
//textPart.setText(message_body); // sets type to "text/plain"
137

138       BodyPart JavaDoc htmlPart = new MimeBodyPart JavaDoc();
139       htmlPart.setContent(content, "text/html");
140       mp.addBodyPart(htmlPart);
141       mesg.setContent(mp);
142
143       Transport.send(mesg);
144
145     } catch (MessagingException ex) {
146       Exception JavaDoc e;
147       if ((e = ex.getNextException()) != null) {
148         Logger.warn(ex.getMessage(), e);
149       }
150     }
151     return;
152   }
153 }
154
Popular Tags