1 16 17 package org.apache.axis2.transport.mail; 18 19 import org.apache.axis2.addressing.EndpointReference; 20 import org.apache.axis2.context.MessageContext; 21 import org.apache.axis2.description.TransportOutDescription; 22 import org.apache.axis2.engine.AxisFault; 23 import org.apache.axis2.transport.AbstractTransportSender; 24 import org.apache.axis2.util.Utils; 25 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 30 public class MailTransportSender extends AbstractTransportSender { 31 private String host; 32 private String user; 33 private String password; 34 private String smtpPort = "25"; 35 36 private ByteArrayOutputStream byteArrayOutputStream; 37 38 public MailTransportSender() { 39 40 } 41 42 public void finalizeSendWithToAddress(MessageContext msgContext,OutputStream out) 43 throws AxisFault { 44 try { 45 TransportOutDescription transportOut = msgContext.getTransportOut(); 46 user = Utils.getParameterValue(transportOut.getParameter(MailConstants.SMTP_USER)); 47 host = Utils.getParameterValue(transportOut.getParameter(MailConstants.SMTP_HOST)); 48 password = 49 Utils.getParameterValue(transportOut.getParameter(MailConstants.SMTP_PASSWORD)); 50 smtpPort = Utils.getParameterValue(transportOut.getParameter(MailConstants.SMTP_PORT)); 51 if (user != null && host != null && password != null && smtpPort != null) { 52 EMailSender sender = new EMailSender(user, host, smtpPort, password); 53 54 56 57 58 59 String eprAddress = msgContext.getTo().getAddress(); 60 int index = eprAddress.indexOf('/'); 61 String subject = ""; 62 String email = null; 63 if(index >= 0){ 64 subject = eprAddress.substring(index+1); 65 email = eprAddress.substring(0,index); 66 }else{ 67 email = eprAddress; 68 } 69 70 System.out.println(subject); 71 System.out.println(email); 72 73 sender.send(subject, email,new String (byteArrayOutputStream.toByteArray())); 74 } else { 75 throw new AxisFault( 76 "user, port, host or password not set, " 77 + " [user null = " 78 + (user == null) 79 + ", password null= " 80 + (password == null) 81 + ", host null " 82 + (host == null) 83 + ",port null " 84 + (smtpPort == null)); 85 86 } 87 } catch (IOException e) { 88 throw new AxisFault(e); 89 } 90 91 92 } 93 94 public OutputStream startSendWithToAddress(MessageContext msgContext, OutputStream out) throws AxisFault { 95 return out; 96 } 97 98 protected OutputStream openTheConnection(EndpointReference epr,MessageContext msgContext) throws AxisFault { 99 byteArrayOutputStream = new ByteArrayOutputStream (); 100 return byteArrayOutputStream; 101 } 102 103 public OutputStream startSendWithOutputStreamFromIncomingConnection( 105 MessageContext msgContext, 106 OutputStream out) 107 throws AxisFault { 108 throw new UnsupportedOperationException (); 109 110 } 111 public void finalizeSendWithOutputStreamFromIncomingConnection( 112 MessageContext msgContext,OutputStream out) 113 throws AxisFault { 114 } 115 118 public void cleanUp(MessageContext msgContext) throws AxisFault { 119 121 } 122 123 } 124 | Popular Tags |