001: /**
002: * Library name : Primrose - A Java Database Connection Pool.
003: * Published by Ben Keeping, http://primrose.org.uk .
004: * Copyright (C) 2004 Ben Keeping, primrose.org.uk
005: * Email: Use "Contact Us Form" on website
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */package uk.org.primrose;
021:
022: import javax.mail.*;
023: import javax.mail.internet.*;
024: import java.util.*;
025: import java.io.*;
026: import javax.activation.*;
027:
028: /**
029: * @author Ben Keeping
030: *
031: * Generic sendMail class, allows for sending emails with attachements.
032: */
033: public class SendMail {
034: static String mxServer;
035: String toAddress, fromAddress;
036: String subject, text;
037: MimeBodyPart mbp = null;
038: Multipart mp = new MimeMultipart();
039: Logger logger = null;
040:
041: /**
042: * Construct an email. <BR>
043: * If the mxServer is unknown, then the static method SendMail.nslookup() can be called to retrieve the domain's mx server. <BR>
044: * Attachments are optional. <BR>
045: * @param String mxServer - The Mail eXchange server to send the mail to.
046: * @param String toAddress - The recipient.
047: * @param String fromAddress - The sender - can be anyting as long as looks like an email address - eg 'me@somewhere.com'.
048: * @param String subject - The mail's subject.
049: * @param String text - The body of the mail.
050: */
051: public SendMail(Logger logger, String mxServer, String toAddress,
052: String fromAddress, String subject, String text) {
053: SendMail.mxServer = mxServer;
054: this .toAddress = toAddress;
055: this .fromAddress = fromAddress;
056: this .subject = subject;
057: this .text = text;
058: this .logger = logger;
059:
060: }
061:
062: /**
063: * Add an attachment to the mail (from a file on disk).
064: * @param File file - the File object to attach.
065: */
066: public void attach(File file) {
067: try {
068: mbp = new MimeBodyPart();
069: mbp.setFileName(file.getName());
070: mbp
071: .setDataHandler(new DataHandler(new FileDataSource(
072: file)));
073: mp.addBodyPart(mbp);
074: } catch (MessagingException me) {
075: logger.printStackTrace(me);
076: }
077: }
078:
079: class SendThread extends Thread {
080: public void run() {
081: Properties props = System.getProperties();
082: props.put("mail.smtp.host", mxServer);
083: Session session = Session.getDefaultInstance(props, null);
084:
085: try {
086: Message msg = new MimeMessage(session);
087: msg.setFrom(new InternetAddress(fromAddress));
088: msg.setRecipients(Message.RecipientType.TO,
089: InternetAddress.parse(toAddress, false));
090: msg.setSubject(subject);
091: msg.setHeader("X-Mailer", "JavaMail");
092:
093: // If there have been attachments (one or more), then set the text/body
094: // as a MimeBodyPart, else set it on the Message. If this isn't done,
095: // then either text or an attachment is sent - not both !
096: if (mbp != null) {
097: MimeBodyPart mbp2 = new MimeBodyPart();
098: mbp2.setText(text);
099: mp.addBodyPart(mbp2);
100: msg.setContent(mp);
101: } else {
102: msg.setText(text);
103: }
104:
105: Transport.send(msg);
106: } catch (AddressException ae) {
107: logger.printStackTrace(ae);
108: } catch (MessagingException me) {
109: logger.printStackTrace(me);
110: }
111: }
112: }
113:
114: /**
115: * Send a message using the contstructor properties.
116: * If there is also an attachment to send, add it too.
117: */
118: public void send() {
119: new SendThread().start();
120: }
121:
122: /**
123: * Given a domain name like 'hotmail.com', perform an OS nslookup call,
124: * and loop it, looking for the word 'exchanger' in the line. On Linux and Windoze
125: * the mx mail server is always the last word/token in the line, so set it as such.
126: * This pays no attention to the preference of which mx server to use, but could (and should !)
127: * be built in really. Still, never mind.
128: * @param String domain - the domain to lookup.
129: */
130: public String nslookup(String domain) {
131: String mailserver = null;
132: try {
133: Process p = Runtime.getRuntime().exec(
134: "nslookup -type=mx " + domain);
135: BufferedReader br = new BufferedReader(
136: new InputStreamReader(p.getInputStream()));
137:
138: boolean gotMxLine = false;
139: String line = null;
140: String token = null;
141:
142: while ((line = br.readLine()) != null) {
143: gotMxLine = false;
144: //System.out.println(line);
145: StringTokenizer st = new StringTokenizer(line);
146: while (st.hasMoreTokens()) {
147: token = st.nextToken();
148: if (token.equals("exchanger")) {
149: gotMxLine = true;
150: }
151: if (gotMxLine) {
152: mailserver = token;
153: }
154: }
155:
156: }
157: } catch (IOException ioe) {
158: logger.printStackTrace(ioe);
159: return null;
160: }
161:
162: System.out.println("Mail Server to use is :: " + mailserver);
163: return mailserver;
164: }
165:
166: /**
167: * Method main.
168: * @param args
169: * Usuage :: <mxServer> <toAddress> <fromAddress> <subject> <text>
170: */
171: /*
172: public static void main(String args[]) {
173: if (args.length < 5) {
174: System.out.println("Usuage :: <mxServer> <toAddress> <fromAddress> <subject> <text>");
175: System.exit(1);
176: }
177: String msgText = "";
178: for (int i = 4; i < args.length; i++) {
179: msgText += (" " +args[i]);
180: }
181:
182: new SendMail(args[0], args[1], args[2], args[3], msgText).send();
183: }
184: */
185: }
|