1 16 package org.apache.axis.transport.mail; 17 18 import org.apache.axis.AxisFault; 19 import org.apache.axis.Message; 20 import org.apache.axis.MessageContext; 21 import org.apache.axis.components.logger.LogFactory; 22 import org.apache.axis.components.uuid.UUIDGen; 23 import org.apache.axis.components.uuid.UUIDGenFactory; 24 import org.apache.axis.handlers.BasicHandler; 25 import org.apache.axis.transport.http.HTTPConstants; 26 import org.apache.axis.utils.Messages; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.net.pop3.POP3Client; 29 import org.apache.commons.net.pop3.POP3MessageInfo; 30 import org.apache.commons.net.smtp.SMTPClient; 31 import org.apache.commons.net.smtp.SMTPReply; 32 33 import javax.mail.Session ; 34 import javax.mail.internet.InternetAddress ; 35 import javax.mail.internet.MimeMessage ; 36 import javax.mail.internet.MimePart ; 37 import java.io.BufferedReader ; 38 import java.io.ByteArrayInputStream ; 39 import java.io.ByteArrayOutputStream ; 40 import java.io.Reader ; 41 import java.io.Writer ; 42 import java.util.Properties ; 43 44 49 public class MailSender extends BasicHandler { 50 51 protected static Log log = LogFactory.getLog(MailSender.class.getName()); 52 private UUIDGen uuidGen = UUIDGenFactory.getUUIDGen(); 53 54 Properties prop = new Properties (); 55 Session session = Session.getDefaultInstance(prop, null); 56 57 65 public void invoke(MessageContext msgContext) throws AxisFault { 66 67 if (log.isDebugEnabled()) { 68 log.debug(Messages.getMessage("enter00", "MailSender::invoke")); 69 } 70 try { 71 String id = writeUsingSMTP(msgContext); 73 74 readUsingPOP3(id, msgContext); 76 } catch (Exception e) { 77 log.debug(e); 78 throw AxisFault.makeFault(e); 79 } 80 if (log.isDebugEnabled()) { 81 log.debug(Messages.getMessage("exit00", 82 "HTTPDispatchHandler::invoke")); 83 } 84 } 85 86 87 95 private String writeUsingSMTP(MessageContext msgContext) 96 throws Exception { 97 String id = (new java.rmi.server.UID ()).toString(); 98 String smtpHost = msgContext.getStrProp(MailConstants.SMTP_HOST); 99 100 SMTPClient client = new SMTPClient(); 101 client.connect(smtpHost); 102 103 System.out.print(client.getReplyString()); 106 int reply = client.getReplyCode(); 107 if (!SMTPReply.isPositiveCompletion(reply)) { 108 client.disconnect(); 109 AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null); 110 throw fault; 111 } 112 113 client.login(smtpHost); 114 System.out.print(client.getReplyString()); 115 reply = client.getReplyCode(); 116 if (!SMTPReply.isPositiveCompletion(reply)) { 117 client.disconnect(); 118 AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null); 119 throw fault; 120 } 121 122 String fromAddress = msgContext.getStrProp(MailConstants.FROM_ADDRESS); 123 String toAddress = msgContext.getStrProp(MailConstants.TO_ADDRESS); 124 125 MimeMessage msg = new MimeMessage (session); 126 msg.setFrom(new InternetAddress (fromAddress)); 127 msg.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress (toAddress)); 128 129 String action = msgContext.useSOAPAction() 131 ? msgContext.getSOAPActionURI() 132 : ""; 133 134 if (action == null) { 135 action = ""; 136 } 137 138 Message reqMessage = msgContext.getRequestMessage(); 139 140 msg.addHeader(HTTPConstants.HEADER_USER_AGENT, Messages.getMessage("axisUserAgent")); 141 msg.addHeader(HTTPConstants.HEADER_SOAP_ACTION, action); 142 msg.setDisposition(MimePart.INLINE); 143 msg.setSubject(id); 144 145 ByteArrayOutputStream out = new ByteArrayOutputStream (8 * 1024); 146 reqMessage.writeTo(out); 147 msg.setContent(out.toString(), reqMessage.getContentType(msgContext.getSOAPConstants())); 148 149 ByteArrayOutputStream out2 = new ByteArrayOutputStream (8 * 1024); 150 msg.writeTo(out2); 151 152 client.setSender(fromAddress); 153 System.out.print(client.getReplyString()); 154 client.addRecipient(toAddress); 155 System.out.print(client.getReplyString()); 156 157 Writer writer = client.sendMessageData(); 158 System.out.print(client.getReplyString()); 159 writer.write(out2.toString()); 160 writer.flush(); 161 writer.close(); 162 163 System.out.print(client.getReplyString()); 164 if (!client.completePendingCommand()) { 165 System.out.print(client.getReplyString()); 166 AxisFault fault = new AxisFault("SMTP", "( Failed to send email )", null, null); 167 throw fault; 168 } 169 System.out.print(client.getReplyString()); 170 client.logout(); 171 client.disconnect(); 172 return id; 173 } 174 175 180 private void readUsingPOP3(String id, MessageContext msgContext) throws Exception { 181 String pop3Host = msgContext.getStrProp(MailConstants.POP3_HOST); 183 String pop3User = msgContext.getStrProp(MailConstants.POP3_USERID); 184 String pop3passwd = msgContext.getStrProp(MailConstants.POP3_PASSWORD); 185 186 Reader reader; 187 POP3MessageInfo[] messages = null; 188 189 MimeMessage mimeMsg = null; 190 POP3Client pop3 = new POP3Client(); 191 pop3.setDefaultTimeout(60000); 193 194 for (int i = 0; i < 12; i++) { 195 pop3.connect(pop3Host); 196 197 if (!pop3.login(pop3User, pop3passwd)) { 198 pop3.disconnect(); 199 AxisFault fault = new AxisFault("POP3", "( Could not login to server. Check password. )", null, null); 200 throw fault; 201 } 202 203 messages = pop3.listMessages(); 204 if (messages != null && messages.length > 0) { 205 StringBuffer buffer = null; 206 for (int j = 0; j < messages.length; j++) { 207 reader = pop3.retrieveMessage(messages[j].number); 208 if (reader == null) { 209 AxisFault fault = new AxisFault("POP3", "( Could not retrieve message header. )", null, null); 210 throw fault; 211 } 212 213 buffer = new StringBuffer (); 214 BufferedReader bufferedReader = new BufferedReader (reader); 215 int ch; 216 while ((ch = bufferedReader.read()) != -1) { 217 buffer.append((char) ch); 218 } 219 bufferedReader.close(); 220 if (buffer.toString().indexOf(id) != -1) { 221 ByteArrayInputStream bais = new ByteArrayInputStream (buffer.toString().getBytes()); 222 Properties prop = new Properties (); 223 Session session = Session.getDefaultInstance(prop, null); 224 225 mimeMsg = new MimeMessage (session, bais); 226 pop3.deleteMessage(messages[j].number); 227 break; 228 } 229 buffer = null; 230 } 231 } 232 pop3.logout(); 233 pop3.disconnect(); 234 if (mimeMsg == null) { 235 Thread.sleep(5000); 236 } else { 237 break; 238 } 239 } 240 241 if (mimeMsg == null) { 242 pop3.logout(); 243 pop3.disconnect(); 244 AxisFault fault = new AxisFault("POP3", "( Could not retrieve message list. )", null, null); 245 throw fault; 246 } 247 248 String contentType = mimeMsg.getContentType(); 249 String contentLocation = mimeMsg.getContentID(); 250 Message outMsg = new Message(mimeMsg.getInputStream(), false, 251 contentType, contentLocation); 252 253 outMsg.setMessageType(Message.RESPONSE); 254 msgContext.setResponseMessage(outMsg); 255 if (log.isDebugEnabled()) { 256 log.debug("\n" + Messages.getMessage("xmlRecd00")); 257 log.debug("-----------------------------------------------"); 258 log.debug(outMsg.getSOAPPartAsString()); 259 } 260 } 261 } 262 | Popular Tags |