1 34 35 package com.micronova.jsp.tag; 36 37 import com.micronova.util.*; 38 import java.util.*; 39 import javax.mail.*; 40 import javax.mail.internet.*; 41 import javax.activation.*; 42 43 public class MailTag extends MapTag 44 { 45 protected String _url; 46 protected Properties _properties; 47 protected Transport _transport; 48 protected Session _session; 49 50 public void init() 51 { 52 super.init(); 53 54 _url = null; 55 _transport = null; 56 _export = null; 57 _properties = null; 58 _session = null; 59 } 60 61 public Transport getTransport() 62 { 63 return _transport; 64 } 65 66 public Session getSession() 67 { 68 return _session; 69 } 70 71 public MailTag getTransportSource() 72 { 73 MailTag transportSource = null; 74 75 if (_transport != null) 76 { 77 return this; 78 } 79 80 String url = _url; 81 82 if (url == null) 83 { 84 transportSource = (MailTag)getAncestorTag("com.micronova.jsp.tag.MailTag"); 85 86 if (transportSource != null) 87 { 88 return transportSource.getTransportSource(); 89 } 90 } 91 else 92 { 93 try 94 { 95 Properties properties = _properties; 96 97 if (properties == null) 98 { 99 properties = new Properties(); 100 } 101 102 URLName urlName = new URLName(url); 103 104 Session session = MailTransport.getSession(properties, urlName); 105 106 Transport transport = session.getTransport(urlName); 107 108 transport.connect(); 109 110 _session = session; 111 112 _transport = transport; 113 114 transportSource = this; 115 } 116 catch (Exception e) 117 { 118 e.printStackTrace(); 119 } 120 } 121 122 return transportSource; 123 } 124 125 public void closeTransport() 126 { 127 Transport transport = _transport; 128 129 if (transport != null) 130 { 131 _transport = null; 132 133 try 134 { 135 transport.close(); 136 } 137 catch (Exception e) 138 { 139 e.printStackTrace(); 140 } 141 } 142 } 143 144 protected void cleanup() 145 { 146 closeTransport(); 147 148 super.cleanup(); 149 } 150 151 public void setUrl(Object expression) throws Exception 152 { 153 _url = (String )evaluateAttribute("url", expression, String .class); 154 155 closeTransport(); 156 } 157 158 public void setProperties(Object expression) throws Exception 159 { 160 NestedMap propertyMap = (NestedMap)(new NestedMap(expression)).get("__param"); 161 162 Properties properties = new Properties(); 163 164 Iterator iterator = propertyMap.entrySet().iterator(); 165 166 while (iterator.hasNext()) 167 { 168 Map.Entry entry = (Map.Entry)iterator.next(); 169 170 properties.put(entry.getKey().toString(), entry.getValue().toString()); 171 } 172 173 _properties = properties; 174 } 175 176 public Object processValue(Object tagValue) throws Exception 177 { 178 if (tagValue != null) 179 { 180 NestedMap map = (NestedMap)tagValue; 181 182 if (!map.isEmpty()) 183 { 184 String from = map.getString("from"); 185 186 MailTag transportSource = getTransportSource(); 187 188 Transport transport = transportSource.getTransport(); 189 Session session = transportSource.getSession(); 190 191 MailTransport.send(session, transport, map); 192 } 193 } 194 195 return tagValue; 196 } 197 } 198 | Popular Tags |