01: /*
02: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
03: * @version $Id: MailServiceImpl.java 6385 2007-10-25 14:02:26Z wehrens $
04: */
05: package org.gridsphere.services.core.mail.impl;
06:
07: import org.gridsphere.portlet.service.PortletServiceException;
08: import org.gridsphere.portlet.service.spi.PortletServiceConfig;
09: import org.gridsphere.portlet.service.spi.PortletServiceFactory;
10: import org.gridsphere.portlet.service.spi.PortletServiceProvider;
11: import org.gridsphere.services.core.mail.MailMessage;
12: import org.gridsphere.services.core.mail.MailService;
13: import org.gridsphere.services.core.portal.PortalConfigService;
14:
15: import javax.mail.Message;
16: import javax.mail.Session;
17: import javax.mail.Transport;
18: import javax.mail.internet.InternetAddress;
19: import javax.mail.internet.MimeMessage;
20: import java.util.Date;
21: import java.util.Properties;
22:
23: /**
24: * <code>MailServiceImpl</code> is a mail utility used to send {@link org.gridsphere.services.core.mail.MailMessage}s.
25: */
26: public class MailServiceImpl implements PortletServiceProvider,
27: MailService {
28:
29: private PortalConfigService portalConfigService = null;
30:
31: public void init(PortletServiceConfig config) {
32: portalConfigService = (PortalConfigService) PortletServiceFactory
33: .createPortletService(PortalConfigService.class, true);
34: }
35:
36: public void destroy() {
37: }
38:
39: /**
40: * Send an email regarding job completion
41: *
42: * @param msg the MailMessage containing e-mail parameters
43: * @throws PortletServiceException if an error occurs sending the message
44: */
45: public void sendMail(MailMessage msg)
46: throws PortletServiceException {
47: Properties props = System.getProperties();
48: String mailServiceHost = portalConfigService
49: .getProperty(PortalConfigService.MAIL_SERVER);
50: if (mailServiceHost != null)
51: props.put("mail.smtp.host", mailServiceHost);
52: String mailServicePort = portalConfigService
53: .getProperty("mailport");
54: if (mailServicePort != null)
55: props.put("mail.smtp.port", mailServicePort);
56: Session session = Session.getDefaultInstance(props, null);
57: session.setDebug(true);
58: Message mimeMessage = new MimeMessage(session);
59: System.err.println(msg.toString());
60:
61: try {
62: InternetAddress from = new InternetAddress(msg.getSender());
63: InternetAddress to[] = InternetAddress.parse(msg
64: .getEmailAddress());
65: mimeMessage.setFrom(from);
66: mimeMessage.setRecipients(Message.RecipientType.TO, to);
67: mimeMessage.setSubject(msg.getSubject());
68: mimeMessage.setSentDate(new Date());
69: mimeMessage.setText(msg.getBody());
70: Transport.send(mimeMessage);
71: } catch (Exception e) {
72: throw new PortletServiceException(e);
73: }
74: }
75: }
|