1 25 26 package net.killingar.actions.email; 27 28 import net.killingar.Utils; 29 import net.killingar.actions.ActionSupport; 30 import webwork.action.CleanupAware; 31 32 import javax.mail.Store ; 33 import java.util.Properties ; 34 35 public abstract class MailSupport 36 extends ActionSupport 37 implements CleanupAware 38 { 39 41 protected String 43 from, 44 smtpServer, 45 username, 46 password, 47 hostname, 48 protocol = "pop3"; 49 50 protected Integer port; 51 52 protected javax.mail.Session mailSession; 53 protected Store mailStore; 54 55 public String getFrom() { return from; } 57 public String getSmtpServer() { return smtpServer; } 58 59 public void setFrom (String in) { from = in; } 61 public void setSmtpServer (String in) { smtpServer = in; } 62 public void setUsername (String in) { username = in; } 63 public void setPassword (String in) { password = in; } 64 public void setProtocol (String in) { protocol = in; } 65 public void setPort (Integer in){ port = in; } 66 67 public String execute() throws Exception 69 { 70 try 71 { 72 System.err.println("executing mail.Mailsupport.execute()"); 73 75 85 86 if (port == null) { 88 if (protocol != null) 89 port = new Integer (protocol.equals("pop3")?110:143); 90 } 91 92 Properties props = Utils.getProperties("net.killingar.email"); 94 if (protocol != null)props.put("mail.store.protocol", protocol); 95 if (protocol != null)props.put("mail.transport.protocol", protocol); 96 if (smtpServer != null)props.put("mail.smtp.host", smtpServer); if (hostname != null)props.put("mail.host", hostname); if (username != null)props.put("mail.user", username); if (from != null)props.put("mail.from", from); 100 props.put("mail.debug", "false"); 101 mailSession = javax.mail.Session.getInstance(props); 102 mailStore = mailSession.getStore(protocol); 103 mailStore.connect(hostname, port.intValue(), username, password); 104 105 String result = doExecute(); 107 return result; 108 } 109 catch (Exception e) 110 { 111 addErrorMessage("getting mailbox failed, exception thrown ("+e.toString()+")"); 112 e.printStackTrace(); 113 114 return ERROR; 115 } 116 } 117 118 public void cleanup() 119 { 120 try 121 { 122 mailStore.close(); 123 } 124 catch (Exception e){} } 126 } 127 | Popular Tags |