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 15 public class DefaultMailMessageHandler implements MailMessageHandler { 16 17 AppParameter[] sharkParams; 18 19 20 protected static String SMTPMailServer; 21 protected static String incomingMailServer; 22 protected static int SMTPport; 23 protected static int IMAPport; 24 protected static int POP3port; 25 protected static String sourceAddress; 26 protected static String login; 27 protected static String password; 28 protected static String incomingMailProtocol; 29 protected static String 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 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 { 52 53 Properties props = new Properties(); 55 56 props.put("mail.smtp.host",DefaultMailMessageHandler.SMTPMailServer); 58 props.put("mail.smtp.port", "" + DefaultMailMessageHandler.SMTPport); 59 60 props.put("mail.smtp.user", DefaultMailMessageHandler.login); 62 props.put("mail.smtp.auth", "true"); 63 64 javax.mail.Session session = Session.getInstance(props,new SmtpAuthenticator(DefaultMailMessageHandler.login,DefaultMailMessageHandler.password)); 66 67 MimeMessage message = new MimeMessage(session); 69 70 message.setFrom(new InternetAddress(DefaultMailMessageHandler.sourceAddress)); 71 72 String [] 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 Transport.send(message); 82 } 83 84 public String receiveMail () throws Exception { 85 86 HashMap hashmap = new HashMap(); 87 88 Properties props = new Properties(); 90 91 javax.mail.Session session = Session.getInstance(props, null); 93 94 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 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 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 msg.setFlag(Flags.Flag.SEEN, true); 129 break; 130 } 131 } 132 133 134 folder.close(true); 136 store.close(); 137 138 if (msg!=null) { 140 this.setParamsBasedOnMailMessage(msg); 141 } else { 142 this.setParamsBasedOnMailMessage(null); 143 } 144 145 return subject; 146 } 147 148 public String composeMailMessageContent () throws Exception { 149 return sharkParams[2].the_value.toString(); 150 } 151 152 public String composeMailMessageSubject () throws Exception { 153 return sharkParams[1].the_value.toString(); 154 } 155 156 public String [] getEMailDestinationAddresses () throws Exception { 157 String [] sa=new String [] {sharkParams[0].the_value.toString()}; 158 return sa; 159 } 160 161 public void setParamsBasedOnMailMessage (Message mmessage) throws Exception { 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 |