Changeset 284
- Timestamp:
- 09/04/08 09:06:14 PM (4 months ago)
- Location:
- trunk/LogicMail/src/org/logicprobe/LogicMail
- Files:
-
- 2 modified
-
mail/smtp/SmtpClient.java (modified) (9 diffs)
-
util/StringParser.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/LogicMail/src/org/logicprobe/LogicMail/mail/smtp/SmtpClient.java
r237 r284 8 8 * 9 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer. 11 11 * 2. Redistributions in binary form must reproduce the above copyright 12 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution. 14 14 * 3. Neither the name of the project nor the names of its 15 15 * contributors may be used to endorse or promote products derived … … 29 29 * OF THE POSSIBILITY OF SUCH DAMAGE. 30 30 */ 31 32 31 package org.logicprobe.LogicMail.mail.smtp; 33 32 34 import java.io.IOException;35 import java.util.Calendar;36 33 import org.logicprobe.LogicMail.AppInfo; 37 34 import org.logicprobe.LogicMail.conf.ConnectionConfig; … … 49 46 import org.logicprobe.LogicMail.util.StringParser; 50 47 48 import java.io.IOException; 49 50 import java.util.Calendar; 51 52 51 53 /** 52 54 * Implements an SMTP client 53 55 */ 54 56 public class SmtpClient implements OutgoingMailClient { 57 private static String strCRLF = "\r\n"; 55 58 private OutgoingConfig outgoingConfig; 56 59 private Connection connection; … … 61 64 private String password; 62 65 private boolean configChanged; 63 private static String strCRLF = "\r\n"; 64 66 private MailSettingsListener mailSettingsListener = new MailSettingsListener() { 67 public void mailSettingsSaved(MailSettingsEvent e) { 68 mailSettings_MailSettingsSaved(e); 69 } 70 }; 71 65 72 /** Creates a new instance of SmtpClient */ 66 73 public SmtpClient(GlobalConfig globalConfig, OutgoingConfig outgoingConfig) { 67 74 this.outgoingConfig = outgoingConfig; 68 connection = new Connection( 69 outgoingConfig.getServerName(), 70 outgoingConfig.getServerPort(), 71 outgoingConfig.getServerSSL(), 72 outgoingConfig.getDeviceSide()); 75 connection = new Connection(outgoingConfig.getServerName(), 76 outgoingConfig.getServerPort(), outgoingConfig.getServerSSL(), 77 outgoingConfig.getDeviceSide()); 73 78 smtpProtocol = new SmtpProtocol(connection); 74 79 75 if (outgoingConfig.getUseAuth() > 0) {80 if (outgoingConfig.getUseAuth() > 0) { 76 81 username = outgoingConfig.getServerUser(); 77 82 password = outgoingConfig.getServerPass(); 78 } 79 else { 83 } else { 80 84 username = null; 81 85 password = null; 82 86 } 87 83 88 openStarted = false; 84 89 configChanged = false; 85 90 MailSettings.getInstance().addMailSettingsListener(mailSettingsListener); 86 91 } 87 private MailSettingsListener mailSettingsListener = new MailSettingsListener() { 88 public void mailSettingsSaved(MailSettingsEvent e) { 89 mailSettings_MailSettingsSaved(e); 90 } 91 }; 92 92 93 93 private void mailSettings_MailSettingsSaved(MailSettingsEvent e) { 94 if(MailSettings.getInstance().containsOutgoingConfig(outgoingConfig)) { 95 // Refresh authentication information from the configuration 96 username = outgoingConfig.getServerUser(); 97 password = outgoingConfig.getServerPass(); 98 99 if(!isConnected()) { 100 // Rebuild the connection to include new settings 101 connection = new Connection( 102 outgoingConfig.getServerName(), 103 outgoingConfig.getServerPort(), 104 outgoingConfig.getServerSSL(), 105 outgoingConfig.getDeviceSide()); 106 smtpProtocol = new SmtpProtocol(connection); 107 } 108 else { 109 // Set a flag to make sure we rebuild the Connection object 110 // the next time we close the connection. 111 configChanged = true; 112 } 113 } 114 else { 115 // We have been deleted, so unregister to make sure we 116 // no longer affect the system and can be garbage collected. 117 MailSettings.getInstance().removeMailSettingsListener(mailSettingsListener); 118 } 94 if (MailSettings.getInstance().containsOutgoingConfig(outgoingConfig)) { 95 // Refresh authentication information from the configuration 96 username = outgoingConfig.getServerUser(); 97 password = outgoingConfig.getServerPass(); 98 99 if (!isConnected()) { 100 // Rebuild the connection to include new settings 101 connection = new Connection(outgoingConfig.getServerName(), 102 outgoingConfig.getServerPort(), 103 outgoingConfig.getServerSSL(), 104 outgoingConfig.getDeviceSide()); 105 smtpProtocol = new SmtpProtocol(connection); 106 } else { 107 // Set a flag to make sure we rebuild the Connection object 108 // the next time we close the connection. 109 configChanged = true; 110 } 111 } else { 112 // We have been deleted, so unregister to make sure we 113 // no longer affect the system and can be garbage collected. 114 MailSettings.getInstance().removeMailSettingsListener(mailSettingsListener); 115 } 119 116 } 120 117 121 118 public boolean open() throws IOException, MailException { 122 if (!openStarted) {119 if (!openStarted) { 123 120 connection.open(); 124 121 125 122 // Eat the initial server response 126 123 connection.receive(); 124 127 125 String hostname = System.getProperty("microedition.hostname"); 128 if(hostname == null) { 129 hostname = "localhost"; 126 127 if (hostname == null) { 128 hostname = connection.getLocalAddress(); 130 129 } 131 130 … … 133 132 openStarted = true; 134 133 } 135 if(outgoingConfig.getUseAuth() > 0) { 136 boolean result = smtpProtocol.executeAuth(outgoingConfig.getUseAuth(), username, password); 137 if(!result) { 134 135 if (outgoingConfig.getUseAuth() > 0) { 136 boolean result = smtpProtocol.executeAuth(outgoingConfig.getUseAuth(), 137 username, password); 138 139 if (!result) { 138 140 return false; 139 141 } 140 142 } 143 141 144 isFresh = true; 142 145 openStarted = false; 146 143 147 return true; 144 148 } … … 146 150 public void close() throws IOException, MailException { 147 151 try { 148 smtpProtocol.executeQuit(); 149 } catch (Exception exp) { } 152 smtpProtocol.executeQuit(); 153 } catch (Exception exp) { 154 } 155 150 156 connection.close(); 151 152 if(configChanged) { 153 // Rebuild the connection to include new settings 154 connection = new Connection( 155 outgoingConfig.getServerName(), 156 outgoingConfig.getServerPort(), 157 outgoingConfig.getServerSSL(), 158 outgoingConfig.getDeviceSide()); 157 158 if (configChanged) { 159 // Rebuild the connection to include new settings 160 connection = new Connection(outgoingConfig.getServerName(), 161 outgoingConfig.getServerPort(), 162 outgoingConfig.getServerSSL(), 163 outgoingConfig.getDeviceSide()); 159 164 smtpProtocol = new SmtpProtocol(connection); 160 165 configChanged = false; … … 163 168 164 169 public ConnectionConfig getConnectionConfig() { 165 return outgoingConfig;166 }170 return outgoingConfig; 171 } 167 172 168 173 public boolean isConnected() { … … 186 191 } 187 192 188 public String sendMessage(Message message) throws IOException, MailException { 189 if(!isFresh) { 193 public String sendMessage(Message message) 194 throws IOException, MailException { 195 if (!isFresh) { 190 196 smtpProtocol.executeReset(); 191 197 } 198 192 199 isFresh = false; 193 200 194 201 // serialize the message 195 202 MessageMimeConverter messageMime = new MessageMimeConverter(); 196 203 message.getBody().accept(messageMime); 204 197 205 String mimeStr = messageMime.toMimeString(); 198 206 199 207 MessageEnvelope env = message.getEnvelope(); 200 208 StringBuffer buffer = new StringBuffer(); … … 203 211 buffer.append("From: " + makeCsvString(env.from) + strCRLF); 204 212 buffer.append("To: " + makeCsvString(env.to) + strCRLF); 205 if(env.cc != null && env.cc.length > 0) { 213 214 if ((env.cc != null) && (env.cc.length > 0)) { 206 215 buffer.append("Cc: " + makeCsvString(env.cc) + strCRLF); 207 216 } 208 if(env.replyTo != null && env.replyTo.length > 0) { 217 218 if ((env.replyTo != null) && (env.replyTo.length > 0)) { 209 219 buffer.append("Reply-To: " + makeCsvString(env.replyTo) + strCRLF); 210 220 } 211 buffer.append("Date: " + StringParser.createDateString(Calendar.getInstance().getTime()) + strCRLF); 212 buffer.append("User-Agent: "+AppInfo.getName()+"/"+AppInfo.getVersion() + strCRLF); 221 222 buffer.append("Date: " + 223 StringParser.createDateString(Calendar.getInstance().getTime()) + 224 strCRLF); 225 buffer.append("User-Agent: " + AppInfo.getName() + "/" + 226 AppInfo.getVersion() + strCRLF); 213 227 buffer.append("Subject: " + env.subject + strCRLF); 214 215 if (env.inReplyTo != null) {228 229 if (env.inReplyTo != null) { 216 230 buffer.append("In-Reply-To: " + env.inReplyTo + strCRLF); 217 231 } 218 232 219 233 // Add the body 220 234 buffer.append(mimeStr); 221 235 222 236 // Send the message 223 if (!smtpProtocol.executeMail(stripEmail(env.from[0]))) {237 if (!smtpProtocol.executeMail(stripEmail(env.from[0]))) { 224 238 throw new MailException("Error with sender"); 225 239 } 226 for(int i=0; i<env.to.length; i++) { 227 if(!smtpProtocol.executeRecipient(stripEmail(env.to[i]))) { 240 241 for (int i = 0; i < env.to.length; i++) { 242 if (!smtpProtocol.executeRecipient(stripEmail(env.to[i]))) { 228 243 throw new MailException("Error with recipient"); 229 244 } 230 245 } 231 if(env.cc != null) { 232 for(int i=0; i<env.cc.length; i++) { 233 if(!smtpProtocol.executeRecipient(stripEmail(env.cc[i]))) { 246 247 if (env.cc != null) { 248 for (int i = 0; i < env.cc.length; i++) { 249 if (!smtpProtocol.executeRecipient(stripEmail(env.cc[i]))) { 234 250 throw new MailException("Error with recipient"); 235 251 } 236 252 } 237 253 } 238 if(env.bcc != null) { 239 for(int i=0; i<env.bcc.length; i++) { 240 if(!smtpProtocol.executeRecipient(stripEmail(env.bcc[i]))) { 254 255 if (env.bcc != null) { 256 for (int i = 0; i < env.bcc.length; i++) { 257 if (!smtpProtocol.executeRecipient(stripEmail(env.bcc[i]))) { 241 258 throw new MailException("Error with recipient"); 242 259 } 243 260 } 244 261 } 245 262 246 263 String rawMessage = buffer.toString(); 247 if(!smtpProtocol.executeData(rawMessage)) { 264 265 if (!smtpProtocol.executeData(rawMessage)) { 248 266 throw new MailException("Error sending message"); 249 267 } 250 268 251 269 return rawMessage; 252 270 } 253 271 254 272 private static String makeCsvString(String[] input) { 255 if (input == null || input.length == 0)273 if ((input == null) || (input.length == 0)) { 256 274 return ""; 257 else if(input.length == 1)275 } else if (input.length == 1) { 258 276 return input[0]; 259 else {277 } else { 260 278 StringBuffer buffer = new StringBuffer(); 261 for(int i=0; i<input.length-1; i++) { 279 280 for (int i = 0; i < (input.length - 1); i++) { 262 281 buffer.append(input[i]); 263 282 buffer.append(", "); 264 283 } 265 buffer.append(input[input.length-1]); 284 285 buffer.append(input[input.length - 1]); 286 266 287 return buffer.toString(); 267 288 } 268 289 } 269 290 270 291 private static String stripEmail(String input) { 271 292 int p = input.indexOf('<'); 272 293 int q = input.indexOf('>'); 273 if(p == -1 || q == -1 || q <= p) 294 295 if ((p == -1) || (q == -1) || (q <= p)) { 274 296 return input; 275 else 276 return input.substring(p+1, q); 297 } else { 298 return input.substring(p + 1, q); 299 } 277 300 } 278 301 } -
trunk/LogicMail/src/org/logicprobe/LogicMail/util/StringParser.java
r279 r284 54 54 */ 55 55 public class StringParser { 56 private static final long ONE_SECOND = 1000; 57 private static final long ONE_MINUTE = ONE_SECOND * 60; 58 private static final long ONE_HOUR = ONE_MINUTE * 60; 59 56 60 private StringParser() { 57 61 } … … 432 436 buf.append(' '); 433 437 434 int tzOffset = (cal.getTimeZone().getRawOffset()) / 36000; 438 int tzOffset = cal.getTimeZone().getOffset(1, cal.get(Calendar.YEAR), 439 cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 440 cal.get(Calendar.DAY_OF_WEEK), 441 (int) (((long) cal.get(Calendar.HOUR_OF_DAY) * ONE_HOUR) + 442 (cal.get(Calendar.MINUTE) * ONE_MINUTE) + 443 (cal.get(Calendar.SECOND) * ONE_SECOND))) / 36000; 435 444 436 445 if (tzOffset < 0) {
