1   /*
2    * Portions of this file are Copyright (C) The Apache Software Foundation. All
3    * rights reserved.
4    *
5    * This software is published under the terms of the Apache Software License
6    * version 1.1, a copy of which has been included with this distribution in
7    * the LICENSE file.
8    */
9   
10  
11  package org.masukomi.aspirin.core;
12  
13  import org.apache.mailet.MailAddress;
14  
15  import org.masukomi.aspirin.core.dnsserver.DNSServer;
16  
17  import java.util.Iterator;
18  import java.util.Properties;
19  
20  import javax.mail.*;
21  
22  import javax.mail.internet.*;
23  
24  
25  /***
26   * @author
27   */
28  public class SendMail extends Thread {
29      //~ Instance fields --------------------------------------------------------
30  
31      /*** DOCUMENT ME! */
32      private MimeMessage message;
33  
34      //~ Constructors -----------------------------------------------------------
35  
36      /***
37       * Creates a new SendMail object.
38       *
39       * @param message DOCUMENT ME!
40       */
41      public SendMail(MimeMessage message) {
42          this.message = message;
43  
44          String threadName = "SendMail for message ID ";
45  
46          try {
47              threadName += message.getMessageID();
48          } catch (MessagingException e) {
49              threadName += "unknown";
50          }
51  
52          setName(threadName);
53      }
54  
55      //~ Methods ----------------------------------------------------------------
56  
57      /***
58       * @todo make it send to everyone in the list
59       * @todo consider using javamail?
60       * @see java.lang.Runnable#run()
61       */
62      public void run() {
63          try {
64              Session session = Session.getInstance(System.getProperties(), null);
65  
66              
67              InternetAddress[] addr = (InternetAddress[]) message
68                  .getAllRecipients();
69  
70              
71  
72              //Figure out which servers to try to send to.  This collection
73              //  will hold all the possible target servers
74              for (int x = 0; x < addr.length; x++) {
75                  MailAddress rcpt = new MailAddress(addr[x]);
76                  String host = rcpt.getHost();
77  
78                  //Lookup the possible targets
79                  Iterator mxRecords = DNSServer.findMXRecords(host).iterator();
80  
81                  if ((mxRecords != null) && (mxRecords.hasNext())) {
82                      boolean success = false;
83  
84                      while ((!success) && (mxRecords.hasNext())) {
85                          try {
86                              String outgoingMailServer = mxRecords.next()
87                                                                   .toString();
88                              StringBuffer logMessageBuffer = new StringBuffer(256);
89                              logMessageBuffer.append(
90                                  "Attempting delivery of mail")
91                                              .append(" to host ").append(outgoingMailServer);
92  
93                              //.append(" to addresses ")
94                              //.append(Arrays
95                              //.asList(addr));
96                              //log(logMessageBuffer.toString());
97                              URLName urlname = new URLName("smtp://"
98                                      + outgoingMailServer);
99  
100                             Properties props = session.getProperties();
101 
102                             //This was an older version of JavaMail
103                             if (message.getSender() == null) {
104                                 props.put("mail.smtp.user", "<>");
105                                 props.put("mail.smtp.from", "<>");
106                             } else {
107                                 String sender = message.getSender().toString();
108                                 props.put("mail.smtp.user", sender);
109                                 props.put("mail.smtp.from", sender);
110                             }
111 
112                             //Many of these properties are only in later JavaMail versions
113                             //"mail.smtp.ehlo"  //default true
114                             //"mail.smtp.auth"  //default false
115                             //"mail.smtp.dsn.ret"  //default to nothing... appended as RET= after MAIL FROM line.
116                             //"mail.smtp.dsn.notify" //default to nothing...appended as NOTIFY= after RCPT TO line.
117                             Transport transport = null;
118 
119                             try {
120                                 transport = session.getTransport(urlname);
121 
122                                 try {
123                                     transport.connect();
124                                 } catch (MessagingException me) {
125                                     // Any error on connect should cause the mailet to attempt
126                                     // to connect to the next SMTP server associated with this MX record,
127                                     // assuming the number of retries hasn't been exceeded.
128 
129                                     /*if (failMessage(mail, me, false)) {
130                                        return true;
131                                        } else {
132                                      */
133                                     continue;
134 
135                                     //}
136                                 }
137 
138                                 transport.sendMessage(message, addr);
139                             } finally {
140                                 if (transport != null) {
141                                     transport.close();
142                                     transport = null;
143                                 }
144                             }
145 
146                             /*logMessageBuffer = new StringBuffer(256).append(
147                                "Mail (").append(mail.getName())
148                                                                .append(") sent successfully to ")
149                                                                .append(outgoingMailServer);
150                                log(logMessageBuffer.toString());
151                              */
152                             //return true;
153                             success = true;
154                         } catch (MessagingException me) {
155                             //MessagingException are horribly difficult to figure out what actually happened.
156                             StringBuffer exceptionBuffer = new StringBuffer(256);
157                             exceptionBuffer.append(
158                                 "Exception delivering message (")
159                                            //.append(mail.getName()).append(") - ")
160                                            .append(me.getMessage());
161 
162                             //log(exceptionBuffer.toString());
163                             System.out.println(exceptionBuffer.toString());
164 
165                             //Assume it is a permanent exception, or prove ourselves otherwise
166                             boolean permanent = true;
167 
168                             if ((me.getNextException() != null)
169                                     && (me.getNextException() instanceof java.io.IOException)) {
170                                 //This is more than likely a temporary failure
171                                 // If it's an IO exception with no nested exception, it's probably
172                                 // some socket or weird I/O related problem.
173                                 //lastError = me;
174                                 continue;
175                             }
176 
177                             // This was not a connection or I/O error particular to one
178                             // SMTP server of an MX set.  Instead, it is almost certainly
179                             // a protocol level error.  In this case we assume that this
180                             // is an error we'd encounter with any of the SMTP servers
181                             // associated with this MX record, and we pass the exception
182                             // to the code in the outer block that determines its severity.
183                             throw me;
184                         }
185                     }
186                 } else {
187                     System.out.println("No mail server found for: " + host);
188 
189                     StringBuffer exceptionBuffer = new StringBuffer(128).append(
190                             "There are no DNS entries for the hostname ")
191                                                                         .append(host)
192                                                                         .append(".  I cannot determine where to send this message.");
193 
194                     System.out.println(exceptionBuffer);
195                 }
196             }
197         } catch (Exception e) {
198         }
199     }
200 }
201 
This page was automatically generated by Maven