+ Log.warn("", "MAIL FROM:<" + m.envelopeFrom.toString()+">");
+ conn.println("MAIL FROM:<" + m.envelopeFrom.toString()+">"); check(conn.readln(), conn);
+ }
+ conn.println("RCPT TO:<" + m.envelopeTo.toString()+">"); check(conn.readln(), conn);
+ conn.println("DATA"); check(conn.readln(), conn);
+ Headers head = m.headers;
+ head = head.remove("return-path");
+ head = head.remove("bcc");
+ Stream stream = head.getStream();
+ for(String s = stream.readln(); s!=null; s=stream.readln()) {
+ if (s.startsWith(".")) conn.print(".");
+ conn.println(s);
+ }
+ conn.println("");
+ stream = m.getBody().getStream();
+ for(String s = stream.readln(); s!=null; s=stream.readln()) {
+ if (s.startsWith(".")) conn.print(".");
+ conn.println(s);
+ }
+ conn.println(".");
+ String resp = conn.readln();
+ if (resp == null)
+ throw new SMTPException("server " + mx + " closed connection without accepting message");
+ check(resp, conn);
+ Log.warn(SMTP.Outgoing.class, "success: " + mx + " accepted " + m.summary() + "\n["+resp+"]");
+ accepted = true;
+ conn.close();
+ } catch (SMTPException e) {
+ if (accepted) return true;
+ Log.warn(SMTP.Outgoing.class, " unable to send; error=" + e);
+ Log.warn(SMTP.Outgoing.class, " message: " + m.summary());
+ Log.warn(SMTP.Outgoing.class, e);
+ if (e.code >= 500 && e.code <= 599) {
+ try {
+ attempt(m.bounce("unable to deliver: " + e), true);
+ } catch (Exception ex) {
+ Log.error(SMTP.Outgoing.class, "exception while trying to deliver bounce; giving up completely");
+ Log.error(SMTP.Outgoing.class, ex);
+ }
+ return true;
+ }
+ return false;
+ } catch (Exception e) {
+ if (accepted) return true;
+ Log.warn(SMTP.Outgoing.class, " unable to send; error=" + e);
+ Log.warn(SMTP.Outgoing.class, " message: " + m.summary());
+ Log.warn(SMTP.Outgoing.class, e);
+ if (conn != null) Log.warn(SMTP.Outgoing.class, conn.dumpLog());
+ return false;
+ } finally {
+ if (conn != null) conn.close();
+ }
+ return accepted;
+ }
+
+ private static HashSet<Outgoing> threads = new HashSet<Outgoing>();
+ private static int serials = 1;
+ private int serial = serials++;
+ private Mailbox.Iterator it;
+
+ public Outgoing() {
+ synchronized(Outgoing.class) {
+ threads.add(this);
+ }
+ }
+
+ public void wake() {
+ int count = spool.count(Query.all());
+ Log.info(SMTP.Outgoing.class, "outgoing thread #"+serial+" woke up; " + count + " messages to send");
+ try {
+ while(true) {
+ boolean good = false;
+ synchronized(Outgoing.class) {
+ it = spool.iterator();
+ OUTER: for(; it.next(); ) {
+ for(Outgoing o : threads)
+ if (o!=this && o.it != null && o.it.uid()==it.uid())
+ continue OUTER;
+ good = true;
+ break;
+ }
+ }
+ if (!good) break;
+ try {
+ if (attempt(it.cur())) it.delete();
+ } catch (Exception e) {
+ Log.error(SMTP.Outgoing.class, e);
+ }
+ Log.info(this, "sleeping for 3s...");
+ Thread.sleep(3000);
+ }
+ } catch (Exception e) {
+ //if (e instanceof InterruptedException) throw e;
+ Log.error(SMTP.Outgoing.class, e);
+ }
+ Log.info(SMTP.Outgoing.class, "outgoing thread #"+serial+" going back to sleep");
+ it = null;
+ }
+
+ public void run() {
+ try {
+ while(true) {
+ Log.setThreadAnnotation("[outgoing #"+serial+"] ");
+ wake();
+ Thread.sleep(1000);
+ synchronized(Outgoing.class) {
+ Outgoing.class.wait(5 * 60 * 1000);
+ }
+ }
+ } catch (InterruptedException e) { Log.warn(this, e); }
+ }
+ }
+
+ public static InetAddress[] getMailExchangerIPs(String hostName) {
+ InetAddress[] ret;
+ try {
+ Hashtable env = new Hashtable();
+ env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
+ DirContext ictx = new InitialDirContext(env);
+ Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
+ Attribute attr = attrs.get("MX");
+ if (attr == null) {
+ ret = new InetAddress[1];
+ try {
+ ret[0] = InetAddress.getByName(hostName);
+ if (ret[0].equals(IP.getIP(127,0,0,1)) || ret[0].isLoopbackAddress()) throw new UnknownHostException();
+ return ret;
+ } catch (UnknownHostException uhe) {
+ Log.warn(SMTP.class, "no MX hosts or A record for " + hostName);
+ return new InetAddress[0];
+ }
+ } else {
+ ret = new InetAddress[attr.size()];
+ NamingEnumeration ne = attr.getAll();
+ for(int i=0; ne.hasMore();) {
+ String mx = (String)ne.next();
+ // FIXME we should be sorting here
+ mx = mx.substring(mx.indexOf(" ") + 1);
+ if (mx.charAt(mx.length() - 1) == '.') mx = mx.substring(0, mx.length() - 1);
+ InetAddress ia = InetAddress.getByName(mx);
+ if (ia.equals(IP.getIP(127,0,0,1)) || ia.isLoopbackAddress()) continue;
+ ret[i++] = ia;
+ }