KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > toolagent > DefaultMailMessageHandler


1 package org.enhydra.shark.toolagent;
2
3 import org.enhydra.shark.api.internal.toolagent.AppParameter;
4 import org.enhydra.shark.api.RootException;
5
6 import javax.mail.*;
7 import java.io.*;
8 import java.util.*;
9 import javax.mail.internet.*;
10 import org.enhydra.shark.api.internal.working.CallbackUtilities;
11
12 /*
13  * @author Sasa Bojanic
14  */

15 public class DefaultMailMessageHandler implements MailMessageHandler {
16
17    AppParameter[] sharkParams;
18
19
20    protected static String JavaDoc SMTPMailServer;
21    protected static String JavaDoc incomingMailServer;
22    protected static int SMTPport;
23    protected static int IMAPport;
24    protected static int POP3port;
25    protected static String JavaDoc sourceAddress;
26    protected static String JavaDoc login;
27    protected static String JavaDoc password;
28    protected static String JavaDoc incomingMailProtocol;
29    protected static String JavaDoc storeFolderName;
30
31    public void configure (CallbackUtilities cus,AppParameter[] aps) throws RootException {
32       this.sharkParams=aps;
33
34       if (DefaultMailMessageHandler.SMTPMailServer==null) {
35          DefaultMailMessageHandler.SMTPMailServer=cus.getProperty("DefaultMailMessageHandler.SMTPMailServer");
36          DefaultMailMessageHandler.incomingMailServer=cus.getProperty("DefaultMailMessageHandler.IncomingMailServer");
37          try {
38             DefaultMailMessageHandler.SMTPport=Integer.parseInt(cus.getProperty("DefaultMailMessageHandler.SMTPPortNo"));
39             DefaultMailMessageHandler.IMAPport=Integer.parseInt(cus.getProperty("DefaultMailMessageHandler.IMAPPortNo"));
40             DefaultMailMessageHandler.POP3port=Integer.parseInt(cus.getProperty("DefaultMailMessageHandler.POP3PortNo"));
41          } catch (Exception JavaDoc ex) {}
42          DefaultMailMessageHandler.sourceAddress=cus.getProperty("DefaultMailMessageHandler.SourceAddress");
43          DefaultMailMessageHandler.login=cus.getProperty("DefaultMailMessageHandler.Login");
44          DefaultMailMessageHandler.password=cus.getProperty("DefaultMailMessageHandler.Password");
45          DefaultMailMessageHandler.incomingMailProtocol=cus.getProperty("DefaultMailMessageHandler.IncomingMailProtocol");
46          DefaultMailMessageHandler.storeFolderName=cus.getProperty("DefaultMailMessageHandler.StoreFolderName");
47       }
48    }
49
50
51    public void sendMail () throws Exception JavaDoc {
52
53       // Get system properties
54
Properties props = new Properties();
55
56       // Setup mail server
57
props.put("mail.smtp.host",DefaultMailMessageHandler.SMTPMailServer);
58       props.put("mail.smtp.port", "" + DefaultMailMessageHandler.SMTPport);
59
60       // User name
61
props.put("mail.smtp.user", DefaultMailMessageHandler.login);
62       props.put("mail.smtp.auth", "true");
63
64       // Get session
65
javax.mail.Session JavaDoc session = Session.getInstance(props,new SmtpAuthenticator(DefaultMailMessageHandler.login,DefaultMailMessageHandler.password));
66
67       // Define message
68
MimeMessage message = new MimeMessage(session);
69
70       message.setFrom(new InternetAddress(DefaultMailMessageHandler.sourceAddress));
71
72       String JavaDoc[] dests=getEMailDestinationAddresses();
73       if (dests!=null) {
74          for (int i=0; i<dests.length; i++) {
75             message.addRecipient(Message.RecipientType.TO, new InternetAddress(dests[i]));
76          }
77       }
78       message.setSubject(this.composeMailMessageSubject());
79       message.setContent(this.composeMailMessageContent(), "text/plain");
80       // Send message
81
Transport.send(message);
82    }
83
84    public String JavaDoc receiveMail () throws Exception JavaDoc {
85
86       HashMap hashmap = new HashMap();
87
88       // Get system properties
89
Properties props = new Properties();
90
91       // Get session
92
javax.mail.Session JavaDoc session = Session.getInstance(props, null);
93
94       // Get the store
95
int imPort=DefaultMailMessageHandler.POP3port;
96       if (!DefaultMailMessageHandler.incomingMailProtocol.equals("pop3")) {
97          imPort=DefaultMailMessageHandler.IMAPport;
98       }
99       Store store = session.getStore(DefaultMailMessageHandler.incomingMailProtocol);
100       store.connect(DefaultMailMessageHandler.incomingMailServer,imPort,DefaultMailMessageHandler.login,DefaultMailMessageHandler.password);
101
102       // Get folder
103
Folder folder = null;
104       Message messages[] = null;
105       Message msg=null;
106       folder = store.getFolder(DefaultMailMessageHandler.storeFolderName);
107       folder.open(Folder.READ_WRITE);
108       messages = folder.getMessages();
109
110       String JavaDoc subject=null;
111       if (messages!=null && messages.length>0) {
112          for (int i=0; i<messages.length; i++) {
113             Flags flags=messages[i].getFlags();
114             Flags.Flag[] flagarr=flags.getSystemFlags();
115             boolean valid=true;
116             System.out.println("Checking flags for mail message "+messages[i].getSubject());
117             for (int j=0; j<flagarr.length; j++) {
118                if (flagarr[j].equals(Flags.Flag.SEEN) || flagarr[j].equals(Flags.Flag.ANSWERED) ||
119                      flagarr[j].equals(Flags.Flag.DELETED)) {
120                   valid=false;
121                   break;
122                }
123             }
124             if (!valid) continue;
125             msg=messages[i];
126             subject = msg.getSubject();
127             // Once we have the subject we mark message as seen
128
msg.setFlag(Flags.Flag.SEEN, true);
129             break;
130          }
131       }
132
133
134       // Close connection
135
folder.close(true);
136       store.close();
137
138       // here we use handler to set parameters based on mail content
139
if (msg!=null) {
140          this.setParamsBasedOnMailMessage(msg);
141       } else {
142          this.setParamsBasedOnMailMessage(null);
143       }
144
145       return subject;
146    }
147
148    public String JavaDoc composeMailMessageContent () throws Exception JavaDoc {
149       return sharkParams[2].the_value.toString();
150    }
151
152    public String JavaDoc composeMailMessageSubject () throws Exception JavaDoc {
153       return sharkParams[1].the_value.toString();
154    }
155
156    public String JavaDoc[] getEMailDestinationAddresses () throws Exception JavaDoc {
157       String JavaDoc[] sa=new String JavaDoc[] {sharkParams[0].the_value.toString()};
158       return sa;
159    }
160
161    public void setParamsBasedOnMailMessage (Message mmessage) throws Exception JavaDoc {
162       if (mmessage!=null) {
163          sharkParams[0].the_value=mmessage.getSubject();
164       } else {
165          sharkParams[0].the_value=null;
166       }
167    }
168
169 }
170
Popular Tags