1 20 21 package org.jdesktop.jdic.desktop.internal.impl; 22 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 26 import org.jdesktop.jdic.desktop.Message; 27 import org.jdesktop.jdic.desktop.internal.LaunchFailedException; 28 import org.jdesktop.jdic.desktop.internal.MailerService; 29 30 35 public class MacMailerService implements MailerService { 36 45 public void open(Message msg) throws LaunchFailedException { 46 openComposeWindow(constructMailto(msg)); 47 } 48 49 54 public void open() throws LaunchFailedException { 55 openComposeWindow("mailto:"); 56 } 57 58 61 private void openComposeWindow(String mailto) throws LaunchFailedException { 62 try { 63 Runtime.getRuntime().exec(new String [] { "open", mailto }); 64 Thread.sleep(2000); 65 } catch (IOException ex) { 66 throw new LaunchFailedException("Cannot launch mail composer via " + 67 "command line: " + ex); 68 } catch (InterruptedException interruptedException) { 69 interruptedException = interruptedException; 70 } 71 } 72 73 78 private String constructMailto(Message msg) { 79 String mailto = "mailto:?"; 80 mailto += flatten("to=", msg.getToAddrs()); 81 mailto += flatten("cc=", msg.getCcAddrs()); 82 mailto += flatten("bcc=", msg.getBccAddrs()); 83 mailto += encode("subject=", msg.getSubject()); 84 mailto += encode("body=", msg.getBody()); 85 mailto += flatten("attach=", msg.getAttachments()); 86 return mailto; 87 } 88 89 93 private String encode(String prefix, String value) { 94 if (value != null) { 95 return prefix + URLUTF8Encoder.encode(value) + "&"; 96 } 97 return ""; 98 } 99 100 104 private String flatten(String prefix, Iterator it) { 105 String result = ""; 106 if (it != null) { 107 while (it.hasNext()) { 108 String item = (String ) it.next(); 109 result += prefix + item + "&"; 110 } 111 } 112 return result; 113 } 114 } 115 | Popular Tags |