KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > message > impl > MailServiceImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.message.impl;
6
7 import java.util.Properties JavaDoc;
8 import javax.mail.*;
9 import javax.mail.Session JavaDoc;
10 import javax.mail.Transport JavaDoc;
11 import javax.mail.internet.InternetAddress JavaDoc;
12 import javax.mail.internet.MimeMessage JavaDoc;
13 import org.exoplatform.services.communication.message.*;
14 import org.exoplatform.commons.exception.ExoMessageException;
15 import org.exoplatform.container.configuration.*;
16 import org.exoplatform.container.configuration.ConfigurationManager;
17 /**
18  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
19  * @since Oct 13, 2004
20  * @version $Id: MailServiceImpl.java,v 1.3 2004/11/03 01:23:39 tuan08 Exp $
21  */

22 public class MailServiceImpl implements MailService {
23   private Session JavaDoc mailSession_;
24   private String JavaDoc smtpServer_ ;
25   
26   public MailServiceImpl(ConfigurationManager confService) throws Exception JavaDoc {
27     ServiceConfiguration sconf = confService.getServiceConfiguration(MailService.class) ;
28     ValueParam param = (ValueParam)sconf.getParameter("outgoing.mail.server") ;
29     smtpServer_ = param.getValue() ;
30     Properties JavaDoc props = System.getProperties();
31     props.put("mail.smtp.host", smtpServer_);
32     ExoAuthenticator auth = new ExoAuthenticator("exo.platform", "exo2004") ;
33     mailSession_ = Session.getDefaultInstance(props, null);
34   }
35   
36   public Session JavaDoc getMailSession() { return mailSession_ ; }
37   
38   public String JavaDoc getOutgoingMailServer() { return smtpServer_ ; }
39   
40   public void sendMessage(org.exoplatform.services.communication.message.Message message) throws Exception JavaDoc {
41     try {
42       MimeMessage JavaDoc mimeMessage = new MimeMessage JavaDoc(mailSession_);
43       InternetAddress JavaDoc author = new InternetAddress JavaDoc(message.getFrom());
44       mimeMessage.setFrom(author);
45       
46       mimeMessage.setFlags(fillFlags(message.getFlagsAsArray()), true);
47       String JavaDoc addr = message.getTo() ;
48       if(addr != null && addr.length() > 0) {
49         mimeMessage.addRecipients(javax.mail.Message.RecipientType.TO, addr);
50       }
51       addr = message.getCC() ;
52       if(addr != null && addr.length() > 0) {
53         mimeMessage.addRecipients(javax.mail.Message.RecipientType.CC, addr);
54       }
55       addr = message.getBCC() ;
56       if(addr != null && addr.length() > 0) {
57         mimeMessage.addRecipients(javax.mail.Message.RecipientType.BCC, addr);
58       }
59       mimeMessage.setSubject(message.getSubject());
60       mimeMessage.setText(message.getBody());
61       Transport.send(mimeMessage);
62     } catch (javax.mail.MessagingException JavaDoc ex) {
63       ex.printStackTrace();
64       Object JavaDoc[] args = {ex.getMessage()} ;
65       throw new ExoMessageException("MessageService.send-message-fail", args) ;
66     }
67   }
68   
69   private Flags fillFlags(String JavaDoc[] flagsAsArray) {
70     Flags flags = new Flags();
71     for (int i = 0; i < flagsAsArray.length; i++) {
72       String JavaDoc flag = flagsAsArray[i];
73       if(MessageHeader.ANSWERED_FLAG.equals(flag)){
74         flags.add(Flags.Flag.ANSWERED);
75       } else if(MessageHeader.DELETED_FLAG.equals(flag)){
76         flags.add(Flags.Flag.DELETED);
77       } else if(MessageHeader.DRAFT_FLAG.equals(flag)){
78         flags.add(Flags.Flag.DRAFT);
79       } else if(MessageHeader.RECENT_FLAG.equals(flag)){
80         flags.add(Flags.Flag.RECENT);
81       } else if(MessageHeader.SEEN_FLAG.equals(flag)){
82         flags.add(Flags.Flag.SEEN);
83       } else if(MessageHeader.USER_FLAG.equals(flag)){
84         flags.add(Flags.Flag.USER);
85       } else if(MessageHeader.FLAGGED_FLAG.equals(flag)){
86         flags.add(Flags.Flag.FLAGGED);
87       } else {
88         //user flag
89
flags.add(flag);
90       }
91     }
92     return flags;
93   }
94 }
95
Popular Tags