KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9 import javax.mail.URLName JavaDoc ;
10 import javax.mail.Flags JavaDoc ;
11 import org.exoplatform.services.communication.message.*;
12 /**
13  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
14  * @since Sep 6, 2004
15  * @version $Id: BaseMailMessageProtocolPlugin.java,v 1.1 2004/10/14 23:29:22 tuan08 Exp $
16  */

17 abstract public class BaseMailMessageProtocolPlugin extends BaseMessageProtocolPlugin {
18   private MailService mailService_ ;
19   
20   public BaseMailMessageProtocolPlugin(MessageService mservice, MailService mailService) {
21     super(mservice) ;
22     mailService_ = mailService ;
23   }
24   
25   
26   public void sendMessage(Account account,Message message) throws Exception JavaDoc {
27     message.setFrom(account.getReplyToAddress()) ;
28     mailService_.sendMessage(message) ;
29     message.setReceivedDate(new java.util.Date JavaDoc()) ;
30     saveSentMessage(account, message) ;
31   }
32
33   public void synchronize(Account account) throws Exception JavaDoc {
34     String JavaDoc userName = account.getProperty(Account.SERVER_SETTING_USERNAME) ;
35     String JavaDoc password = account.getProperty(Account.SERVER_SETTING_PASSWORD) ;
36     String JavaDoc hostName = account.getProperty(Account.SERVER_SETTING_HOSTNAME) ;
37     //System.out.println("hostname: " + hostName + " username: " + userName + " password: " + password) ;
38
URLName JavaDoc url = getInboxFolderURLName(hostName, userName, password);
39     javax.mail.Folder JavaDoc remoteFolder = mailService_.getMailSession().getFolder(url);
40     remoteFolder.open(javax.mail.Folder.READ_WRITE) ;
41     javax.mail.Message JavaDoc[] message = remoteFolder.getMessages() ;
42     List JavaDoc messageToUpdate = new ArrayList JavaDoc() ;
43     Folder inbox = null ;
44     for(int i = 0; i < message.length; i++) {
45       Flags JavaDoc flags = message[i].getFlags();
46       if(!MailUtil.hasSystemFlag(flags, Flags.Flag.FLAGGED)) {
47         Message newMessage = MailUtil.createMessage(message[i]) ;
48         if(inbox == null) {
49         }
50           inbox = mservice_.getFolder(account, MessageService.INBOX_FOLDER) ;
51         mservice_.createMessage(account, inbox, newMessage) ;
52         messageToUpdate.add(message[i]) ;
53       }
54     }
55     message = new javax.mail.Message JavaDoc[messageToUpdate.size()] ;
56     for(int i = 0; i < message.length; i++) {
57       message[i] = (javax.mail.Message JavaDoc) messageToUpdate.get(i) ;
58     }
59     Flags JavaDoc flags = new Flags JavaDoc(Flags.Flag.FLAGGED) ;
60     remoteFolder.setFlags(message, flags, true) ;
61     remoteFolder.close(true) ;
62   }
63   
64   abstract public URLName JavaDoc getInboxFolderURLName(String JavaDoc host, String JavaDoc userName , String JavaDoc password) ;
65 }
Popular Tags