001: package com.jat.util.mail;
002:
003: /**
004: * <p>Title: JAT</p>
005: * <p>Description: </p>
006: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
007: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
008: * @author stf
009: * @version 1.1
010: */
011:
012: import javax.mail.Session;
013: import java.util.Properties;
014: import javax.mail.internet.MimeMessage;
015: import javax.mail.Address;
016: import javax.mail.internet.MimeMessage.RecipientType;
017: import javax.mail.MessagingException;
018: import java.util.Date;
019: import javax.mail.Transport;
020: import javax.mail.internet.AddressException;
021: import javax.mail.internet.InternetAddress;
022: import javax.mail.Message;
023:
024: public class SendMail {
025:
026: public SendMail(Session session) {
027: this .session = session;
028: }
029:
030: public SendMail(String smtpHost, String username, String password) {
031: Authentication authentication = new Authentication(username,
032: password);
033: session = Session.getInstance(loadProperties(smtpHost),
034: authentication);
035: }
036:
037: public SendMail(String smtpHost, String username) {
038: this (smtpHost, username, null);
039: }
040:
041: public void send(Address from, String subject, Address[] to,
042: Address[] cc, Address[] bcc, String body)
043: throws MessagingException {
044: MimeMessage msg = this .getMessage();
045: msg.setFrom(from);
046: msg.setSubject(subject);
047: msg.addRecipients(RecipientType.TO, to);
048: msg.addRecipients(RecipientType.CC, cc);
049: msg.addRecipients(RecipientType.BCC, bcc);
050: msg.setText(body);
051: msg.setSentDate(new Date());
052: msg.saveChanges();
053: Transport.send(msg);
054: }
055:
056: public void send(Address from, String subject, String to,
057: String body) throws AddressException, MessagingException {
058: Address[] addr = { new InternetAddress(to) };
059: this .send(from, subject, addr, null, null, body);
060: }
061:
062: public void sendHtml(Address from, String subject, Address[] to,
063: Address[] cc, Address[] bcc, String body)
064: throws MessagingException {
065: MimeMessage msg = this .getMessage();
066: msg.setFrom(from);
067: msg.setSubject(subject);
068: msg.addRecipients(RecipientType.TO, to);
069: msg.addRecipients(RecipientType.CC, cc);
070: msg.addRecipients(RecipientType.BCC, bcc);
071: msg.setContent(body, "text/html");
072: msg.setSentDate(new Date());
073: msg.saveChanges();
074: Transport.send(msg);
075: }
076:
077: public void sendHtml(Address from, String subject, String to,
078: String body) throws AddressException, MessagingException {
079: Address[] addr = { new InternetAddress(to) };
080: this .sendHtml(from, subject, addr, null, null, body);
081: }
082:
083: public MimeMessage getMessage() {
084: return new MimeMessage(this .getSession());
085: }
086:
087: public void sendMessage(Message msg) throws MessagingException {
088: this .session.getTransport().sendMessage(msg, null);
089: }
090:
091: protected Session getSession() {
092: return this .session;
093: }
094:
095: protected Properties getProperties() {
096: return this .session.getProperties();
097: }
098:
099: protected Authentication getAuthentication() {
100: return this .getAuthentication();
101: }
102:
103: private Properties loadProperties(String smtpHost) {
104: Properties props = new Properties();
105: props.put("mail.transport.protocol", "smtp");
106: props.put("mail.smtp.host", smtpHost);
107: return props;
108: }
109:
110: private Session session = null;
111: }
|