001: /**
002: * Copyright (C) 2001 Yasna.com. All rights reserved.
003: *
004: * ===================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * Yasna.com (http://www.yasna.com)."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "Yazd" and "Yasna.com" must not be used to
027: * endorse or promote products derived from this software without
028: * prior written permission. For written permission, please
029: * contact yazd@yasna.com.
030: *
031: * 5. Products derived from this software may not be called "Yazd",
032: * nor may "Yazd" appear in their name, without prior written
033: * permission of Yasna.com.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of Yasna.com. For more information
051: * on Yasna.com, please see <http://www.yasna.com>.
052: */package com.Yasna.util;
053:
054: import com.Yasna.forum.PropertyManager;
055:
056: import java.util.*;
057: import javax.mail.*;
058: import javax.mail.internet.*;
059: import javax.naming.*;
060:
061: public final class MailSender extends Thread {
062:
063: public static final String TEST_HOST = "hurricane";
064: public static final String TEST_FROM = "aflatoon@test.com";
065: public static final String TEST_TO = "aflatoon@test.com";
066: public static final String TEST_SUBJECT = "SMTPMail Test";
067: public static final String TEST_MESSAGE = "This is a test.";
068:
069: private String host;
070: private String from;
071: private String to;
072: private String cc = null;
073: private String bcc = null;
074: private String subject;
075: private String message;
076: private String smtpusername = PropertyManager
077: .getProperty("yazdMailSMTPServer.username");
078: private String smtppassword = PropertyManager
079: .getProperty("yazdMailSMTPServer.password");
080:
081: public MailSender(String strHost, String strFrom, String strTo,
082: String strCc, String strBcc, String strSubject,
083: String strMessage) {
084:
085: host = strHost == null ? TEST_HOST : strHost;
086: from = strFrom == null ? TEST_FROM : strFrom;
087: to = strTo == null ? TEST_TO : strTo;
088: cc = strCc;
089: bcc = strBcc;
090: subject = strSubject == null ? TEST_SUBJECT : strSubject;
091: message = strMessage == null ? TEST_MESSAGE : strMessage;
092: }
093:
094: public void run() {
095: try {
096: sendIt();
097: } catch (Exception e) {
098: System.out
099: .println("Yazd MailSender - Exception while sending a mail "
100: + e);
101: }
102: }
103:
104: private void sendIt() throws Exception {
105: Properties properties;
106: MimeMessage msg;
107: Session session;
108: /*
109: System.out.println("\n-----------------------------------------..." +
110: "\n ...SENDING MAIL..." +
111: "\n-----------------------------------------..." +
112: "\n host: " + host +
113: "\n to: " + to +
114: "\n from: " + from +
115: "\n subject: " + subject +
116: "\n-----------------------------------------..." +
117: "\n" + message +
118: "\n-----------------------------------------..."
119: );
120: */
121: properties = new Properties();
122: properties.put("mail.smtp.host", host);
123: session = Session.getInstance(properties, null);
124: msg = new MimeMessage(session);
125: msg
126: .addFrom(new InternetAddress[] { new InternetAddress(
127: from) });
128: msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
129: to));
130: if (cc != null) {
131: msg.addRecipient(Message.RecipientType.CC,
132: new InternetAddress(cc));
133: }
134: if (bcc != null) {
135: msg.addRecipient(Message.RecipientType.BCC,
136: new InternetAddress(bcc));
137: }
138: msg.setSubject(subject);
139: msg.setText(message);
140: //msg.setHeader("Content-Type", "text/html");
141: //msg.saveChanges();
142: if (smtpusername != null && smtppassword != null) {
143: Transport tr = session.getTransport();
144: tr.connect(host, smtpusername, smtppassword);
145: msg.saveChanges(); // don't forget this
146: tr.sendMessage(msg, msg.getAllRecipients());
147: tr.close();
148: System.out
149: .println("Yazd Forum Software (MailSender.java)-- (authsmtp) Mail sent successfully to:"
150: + to);
151: } else {
152: Transport.send(msg);
153: System.out
154: .println("Yazd Forum Software (MailSender.java)-- Mail sent successfully to:"
155: + to);
156: }
157: }
158:
159: public static void send(String _host, String _from, String _to,
160: String _cc, String _bcc, String _subject, String _message) {
161: MailSender sender = new MailSender(_host, _from, _to, _cc,
162: _bcc, _subject, _message);
163: sender.start();
164: }
165:
166: public static void send(String _host, String _from, String _to,
167: String _subject, String _message) {
168: send(_host, _from, _to, null, null, _subject, _message);
169: }
170:
171: public void sendForTest() throws Exception {
172: sendIt();
173: }
174:
175: public static void send(String _host, String _from, String _to,
176: String _cc, String _subject, String _message) {
177: send(_host, _from, _to, _cc, null, _subject, _message);
178: }
179: }
|