1 package org.apache.axis2.transport.mail.server; 2 3 import org.apache.axis2.context.ConfigurationContext; 4 5 import javax.mail.*; 6 import javax.mail.internet.MimeMessage ; 7 import java.io.*; 8 import java.net.Socket ; 9 import java.util.ArrayList ; 10 import java.util.Properties ; 11 12 13 17 18 public class SMTPWorker extends Thread { 19 20 BufferedReader reader = null; 21 22 BufferedWriter writer = null; 23 24 ArrayList recivers = new ArrayList (); 25 26 Storage st = null; 27 boolean runThread = true; 28 29 private MimeMessage mail = null; 30 private ConfigurationContext configurationContext = null; 31 32 String temp = ""; 33 boolean dataWriting = false; 34 boolean transmitionEnd = false; 35 36 boolean bodyData = false; 37 38 public SMTPWorker(Socket socket, Storage st,ConfigurationContext configurationContext) { 39 try { 40 this.st = st; 41 this.configurationContext = configurationContext; 42 reader = new BufferedReader(new InputStreamReader(socket 44 .getInputStream())); 45 writer = new BufferedWriter(new OutputStreamWriter(socket 46 .getOutputStream())); 47 } catch (IOException ex) { 48 ex.printStackTrace(); 49 } 50 } 51 52 public void run() { 54 try { 55 initializeClient(); 57 58 while (runThread) { 60 String input = null; 61 62 input = reader.readLine(); 64 65 String retString = processInput(input); 66 67 if (MailConstants.COMMAND_EXIT.equals(retString)){ 68 exitWorker(); 69 } else { 70 if (retString != null) { 71 send(retString); } 73 if(mail!=null && transmitionEnd) { 74 exitWorker(); 75 } 76 } 77 } 78 for (int idx = 0; idx < recivers.size();idx++){ 79 try { 80 MailSorter mSort = new MailSorter(this.st,this.configurationContext); 81 mSort.sort((String )recivers.get(idx), new MimeMessage (mail)); 82 } catch (MessagingException e1) { 83 e1.printStackTrace(); 84 } 85 } 86 88 } catch (IOException e) { 89 System.out.println("ERROR: CLIENT CLOSED THE SOCKET"); 90 } 91 } 92 93 private void send(String s) throws IOException{ 94 writer.write(s); 95 writer.newLine(); 96 writer.flush(); 97 } 98 99 private String processInput(String input) { 100 byte[] CR_LF = new byte[] {0x0D, 0x0A}; 101 if(input==null) return MailConstants.COMMAND_UNKNOWN; 102 if(mail!=null && transmitionEnd) return MailConstants.COMMAND_TRANSMISSION_END; 103 104 if (input.startsWith("MAIL")) { 105 mail = new MimeMessage (Session.getInstance(new Properties (), new Authenticator() { 106 protected PasswordAuthentication getPasswordAuthentication() { 107 return null; 108 } 109 })); 110 111 int start = input.indexOf("<") + 1; 112 int end; 113 114 if(start<=0){ 115 start = input.indexOf("FROM:") + 5; 116 end = input.length(); 117 }else{ 118 end = input.indexOf(">"); 119 } 120 121 String from = input.substring(start,end); 122 123 if(from!=null && !from.trim().equals("")){ 124 MailAddress mailFrom[] = new MailAddress[1]; 126 mailFrom[0] = new MailAddress(from); 127 try { 128 mail.addFrom(mailFrom); 129 } catch (MessagingException e) { 130 e.printStackTrace(); 132 } 133 } 134 135 return MailConstants.MAIL_OK; 136 137 } if (input.startsWith("HELO")) { 138 return MailConstants.HELO_REPLY; 139 140 } else if (input.startsWith("RCPT")) { 141 142 String domain = MailConstants.SERVER_DOMAIN; 143 int start = input.indexOf("<") + 1; 146 int end; 147 148 if(start<=0){ 149 start = input.indexOf("TO:") + 3; 150 154 }else{ 155 159 } 160 161 end = input.indexOf(">"); 162 String toStr = input.substring(start,end); 163 164 try { 165 mail.addRecipient(Message.RecipientType.TO, new MailAddress(toStr)); 166 recivers.add(toStr); 167 } catch (MessagingException e) { 168 e.printStackTrace(); 170 } 171 return MailConstants.RCPT_OK; 172 173 } else if (input.equalsIgnoreCase("DATA")) { 174 dataWriting = true; 175 return MailConstants.DATA_START_SUCCESS; 176 177 } else if (input.equalsIgnoreCase("QUIT")) { 178 dataWriting = true; 179 transmitionEnd = true; 180 return MailConstants.COMMAND_TRANSMISSION_END; 181 182 } else if (input.equals(".")) { 183 dataWriting = false; 184 return MailConstants.DATA_END_SUCCESS; 185 } else if (input.equals("") && !bodyData) { 186 bodyData = true; 187 return null; 188 } else if(mail!=null && dataWriting){ 189 try { 190 if (bodyData) { 191 temp += input; 192 mail.setContent(temp, "text/plain"); 193 System.out.println("\n\n\n---------------" + temp + "---------------\n\n\n"); 194 } else { 195 mail.addHeaderLine(input); 196 } 197 } catch (MessagingException e) { 198 e.printStackTrace(); 200 } 201 return null; 202 203 }else { 204 return MailConstants.COMMAND_UNKNOWN; 205 } 206 207 } 208 209 private void exitWorker() throws IOException { 211 reader.close(); 212 writer.close(); 213 runThread = false; 214 } 215 216 private void initializeClient() throws IOException { 218 if(writer!=null){ 219 send("220 SMTP Server IS UP"); 220 } 221 } 222 } | Popular Tags |