01: /*
02: * Created on May 11, 2003
03: */
04: package net.sf.jportlet.service.mail;
05:
06: import java.util.Date;
07:
08: import javax.activation.DataHandler;
09:
10: import javax.mail.Address;
11: import javax.mail.Message;
12: import javax.mail.MessagingException;
13: import javax.mail.Session;
14: import javax.mail.Transport;
15: import javax.mail.internet.MimeMessage;
16:
17: import javax.naming.InitialContext;
18: import javax.naming.NamingException;
19:
20: import net.sf.jportlet.service.PortletServiceAdapter;
21: import net.sf.jportlet.service.PortletServiceConfig;
22: import net.sf.jportlet.service.PortletServiceException;
23:
24: /**
25: * @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
26: */
27: public class MailServiceImpl extends PortletServiceAdapter implements
28: MailService {
29: //~ Instance fields --------------------------------------------------------
30:
31: private String _jndiSession;
32:
33: //~ Methods ----------------------------------------------------------------
34:
35: /**
36: * @see net.sf.jportlet.service.mail.MailService#send(java.net.InetAddress, java.net.InetAddress[], java.lang.String, java.lang.String, java.lang.String)
37: */
38: public void send(Address from, Address to[], String subject,
39: String body, String mimeType) throws NamingException,
40: MessagingException {
41: InitialContext ic = new InitialContext();
42: Session session = (Session) ic.lookup(_jndiSession);
43: Message msg = new MimeMessage(session);
44:
45: if (from != null) {
46: msg.setFrom(from);
47: } else {
48: msg.setFrom();
49: }
50: msg.setRecipients(javax.mail.Message.RecipientType.TO, to);
51: msg.setSubject(subject);
52:
53: msg.setDataHandler(new DataHandler(body, mimeType));
54:
55: //msg.setHeader( "X-Mailer", "JavaMailer" );
56: msg.setSentDate(new Date());
57:
58: Transport.send(msg);
59: }
60:
61: /**
62: * @see net.sf.jportlet.service.PortletService#getServiceName()
63: */
64: public String getServiceName() {
65: return MailService.NAME;
66: }
67:
68: /**
69: * @see net.sf.jportlet.service.PortletService#init(net.sf.jportlet.service.PortletServiceConfig)
70: */
71: public void init(PortletServiceConfig config)
72: throws PortletServiceException {
73: super .init(config);
74:
75: _jndiSession = getInitParameter("session");
76: if (_jndiSession == null) {
77: throw new PortletServiceException(
78: "session parameter is required");
79: }
80: }
81: }
|