1 36 package SimpleSMTP; 37 38 import java.io.File ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.net.InetAddress ; 42 import java.nio.charset.Charset ; 43 44 import org.columba.ristretto.auth.AuthenticationException; 45 import org.columba.ristretto.auth.AuthenticationFactory; 46 import org.columba.ristretto.auth.NoSuchAuthenticationException; 47 import org.columba.ristretto.composer.MimeTreeRenderer; 48 import org.columba.ristretto.io.CharSequenceSource; 49 import org.columba.ristretto.io.FileSource; 50 import org.columba.ristretto.log.RistrettoLogger; 51 import org.columba.ristretto.message.Address; 52 import org.columba.ristretto.message.BasicHeader; 53 import org.columba.ristretto.message.Header; 54 import org.columba.ristretto.message.LocalMimePart; 55 import org.columba.ristretto.message.MimeHeader; 56 import org.columba.ristretto.message.MimeType; 57 import org.columba.ristretto.parser.AddressParser; 58 import org.columba.ristretto.parser.ParserException; 59 import org.columba.ristretto.smtp.SMTPException; 60 import org.columba.ristretto.smtp.SMTPProtocol; 61 62 66 public class SimpleSMTP { 67 68 private static final String helpMessage = 69 "Usage : SimpleSMTP smtp-server from-address to-address subject [optional args]\n\n" 70 + "Example: SimpleSMTP smtp.test.com sender@test.com example@test.com \"This is a test message\" --text \"Hello, World!\"\n\n" 71 + "--body\t\tMessage String\n" 72 + "--textfile\tMessage from a text file\n" 73 + "--attachment\tFile Attachment\n" 74 + "--auth\t\tusername password\n" 75 + "--help\t\tShow this help screen\n"; 76 77 81 public static void main(String [] args) { 82 if (args.length < 4 || args[0].equals("--help")) { 83 System.out.println(helpMessage); 84 return; 85 } 86 87 String smtpServer = args[0]; 88 89 Address fromAddress; 91 try { 92 fromAddress = AddressParser.parseAddress(args[1]); 93 } catch (ParserException e) { 94 System.err.println("Invalid from-address : " + e.getSource()); 95 return; 96 } 97 98 Address toAddress; 99 try { 100 toAddress = AddressParser.parseAddress(args[2]); 101 } catch (ParserException e) { 102 System.err.println("Invalid to-address : " + e.getSource()); 103 return; 104 } 105 106 String subject = args[3]; 107 108 String body = "Hello, World!"; 109 String textfile = null; 110 String attachment = null; 111 String user = null; 112 String password = null; 113 boolean verbose = false; 114 115 117 for (int i = 4; i < args.length;) { 118 if (args[i].equals("--body")) { 119 body = args[i + 1]; 120 i += 2; 121 } else if (args[i].equals("--textfile")) { 122 textfile = args[i + 1]; 123 i += 2; 124 } else if (args[i].equals("--attachment")) { 125 attachment = args[i + 1]; 126 i += 2; 127 } else if (args[i].equals("--auth")) { 128 user = args[i + 1]; 129 password = args[i + 2]; 130 i += 3; 131 } else if (args[i].equals("--verbose")) { 132 verbose = true; 133 i++; 134 } 135 } 136 137 if (verbose) { 140 RistrettoLogger.setLogStream(System.out); 141 } 142 143 146 148 Header header = new Header(); 151 BasicHeader basicHeader = new BasicHeader(header); 152 153 basicHeader.setFrom(fromAddress); 157 basicHeader.setTo(new Address [] { toAddress }); 158 basicHeader.setSubject(subject, Charset.forName("ISO-8859-1")); 159 basicHeader.set("X-Mailer", "SimpleSMTP example / Ristretto API"); 160 161 MimeHeader mimeHeader = new MimeHeader(header); 165 mimeHeader.set("Mime-Version", "1.0"); 166 LocalMimePart root = new LocalMimePart(mimeHeader); 167 168 LocalMimePart textPart; 169 170 if (attachment == null) { 172 textPart = root; 173 } else { 174 mimeHeader.setMimeType(new MimeType("multipart", "mixed")); 175 176 textPart = new LocalMimePart(new MimeHeader()); 177 root.addChild(textPart); 178 } 179 180 MimeHeader textHeader = textPart.getHeader(); 182 textHeader.setMimeType(new MimeType("text", "plain")); 183 if (body != null) { 184 185 root.setBody(new CharSequenceSource(body)); 187 } else if (textfile != null) { 188 189 try { 191 root.setBody(new FileSource(new File (textfile))); 192 } catch (IOException e1) { 193 System.err.println(e1.getLocalizedMessage()); 194 return; 195 } 196 } 197 198 199 if (attachment != null) { 201 MimeHeader attachmentHeader = 202 new MimeHeader("application", "octet-stream"); 203 attachmentHeader.setContentTransferEncoding("base64"); 204 attachmentHeader.putDispositionParameter("filename", attachment); 205 206 LocalMimePart attachmentPart = new LocalMimePart(attachmentHeader); 207 try { 208 attachmentPart.setBody(new FileSource(new File (attachment))); 209 } catch (IOException e1) { 210 System.err.println(e1.getLocalizedMessage()); 211 return; 212 } 213 root.addChild(attachmentPart); 214 } 215 216 InputStream messageSource; 217 try { 218 messageSource = MimeTreeRenderer.getInstance().renderMimePart( root ); 220 } catch (Exception e2) { 221 System.err.println(e2.getLocalizedMessage()); 222 return; 223 } 224 225 226 228 SMTPProtocol protocol = new SMTPProtocol(smtpServer); 230 try { 231 protocol.openPort(); 233 234 String capabilities[] = protocol.ehlo(InetAddress.getLocalHost()); 236 237 if(user != null ) { 239 String authCapability = null; 240 for( int i=0; i<capabilities.length; i++) { 241 if( capabilities[i].startsWith("AUTH")) { 242 authCapability = capabilities[i]; 243 break; 244 } 245 } 246 if( authCapability != null ) { 247 try { 248 protocol.auth( AuthenticationFactory.getInstance().getSecurestMethod( authCapability ), user, password.toCharArray() ); 249 } catch (NoSuchAuthenticationException e3) { 250 System.err.println(e3.getLocalizedMessage()); 251 return; 252 } catch ( AuthenticationException e ) { 253 System.err.println(e.getMessage()); 254 return; 255 256 } 257 } else { 258 System.err.println("Server does not support Authentication!"); 259 return; 260 } 261 262 } 263 264 protocol.mail(fromAddress); 266 protocol.rcpt(toAddress); 267 268 protocol.data(messageSource); 270 271 protocol.quit(); 273 274 } catch (IOException e1) { 275 System.err.println(e1.getLocalizedMessage()); 276 return; 277 } catch (SMTPException e1) { 278 System.err.println(e1.getMessage()); 279 return; 280 } 281 282 System.exit(0); 283 } 284 } 285 | Popular Tags |