{
if( log.isDebugEnabled() )
log.debug(getClass().getSimpleName()+" ("+getName()+").deliver(): Starting mail delivery. qi="+qi);
// Get objects required from QuedItem
MailImpl mail = (MailImpl) qi.getMail();
MimeMessage message = mail.getMessage();
// Get all recipients
Collection<MailAddress> recipients = mail.getRecipients();
// InternetAddress addr[] = new InternetAddress[recipients.size()];
// int j = 0;
// // funky ass look because you can't getElementAt() in a Collection
//
// for (Iterator i = recipients.iterator(); i.hasNext(); j++) {
// MailAddress currentRcpt = (MailAddress) i.next();
// addr[j] = currentRcpt.toInternetAddress();
// }
if( recipients.size() <= 0 )
{
if (log.isDebugEnabled())
log.debug(getClass().getSimpleName()+" ("+getName()+").deliver(): No recipients specified... returning");
return true;
}
Iterator<MailAddress> it = recipients.iterator();
while (it.hasNext()) {
rcpt = (MailAddress) it.next();
if( !qi.recepientHasBeenHandled(rcpt) )
break;
}
InternetAddress[] addr = new InternetAddress[]{rcpt.toInternetAddress()};
// If recipient is null, we could not handle this email
if (rcpt == null)
{
log.error(getClass().getSimpleName()+" ("+getName()+").deliver(): Could not find unhandled recipient.");
return false;
}
String host = rcpt.getHost();
// Lookup the possible targets
// Figure out which servers to try to send to. This collection
// will hold all the possible target servers
Collection<URLName> targetServers = null;
// theoretically it is possible to not hav eone that hasn't been
// handled
// however that's only if something has gone really wrong.
try {
// targetServers = MXLookup.urlsForHost(host); // farking
// unreliable jndi bs
targetServers = getMXRecordsForHost(host);
} catch (Exception e) {
log.error(getClass().getSimpleName()+" ("+getName()+" ).deliver(): Could not get MX for "+host+".",e);
}
if (targetServers == null || targetServers.size() == 0) {
log.warn(getClass().getSimpleName()+" ("+getName()+").deliver(): No mail server found for: " + host);
StringBuffer exceptionBuffer = new StringBuffer(128)
.append(
"I found no MX record entries for the hostname ")
.append(host)
.append(
". I cannot determine where to send this message.");
return failMessage(qi, rcpt, new MessagingException(
exceptionBuffer.toString()), true);
} else if (log.isTraceEnabled()) {
log.trace(getClass().getSimpleName()+" ("+getName()+").deliver(): "+ targetServers.size() + " servers found for "+ host+".");
}
MessagingException lastError = null;
Iterator<URLName> i = targetServers.iterator();
while (i.hasNext()) {
try {
URLName outgoingMailServer = (URLName) i.next();
StringBuffer logMessageBuffer = null;
if( log.isDebugEnabled() )
{
logMessageBuffer = new StringBuffer(256)
.append(getClass().getSimpleName())
.append(" (")
.append(getName())
.append(").deliver(): ")
.append("Attempting delivery of ")
.append(mail.getName())
.append(" to host ")
.append(outgoingMailServer.toString())
.append(" to addresses ")
.append(Arrays.asList(addr));
log.debug(logMessageBuffer.toString());
}
Properties props = session.getProperties();
if (mail.getSender() == null) {
props.put("mail.smtp.from", "<>");
} else {
String sender = mail.getSender().toString();
props.put("mail.smtp.from", sender);
}
// Many of these properties are only in later JavaMail
// versions
// "mail.smtp.ehlo" //default true
// "mail.smtp.auth" //default false
// "mail.smtp.dsn.ret" //default to nothing... appended
// as
// RET= after MAIL FROM line.
// "mail.smtp.dsn.notify" //default to
// nothing...appended as
// NOTIFY= after RCPT TO line.
Transport transport = null;
try {
transport = session.getTransport(outgoingMailServer);
try {
transport.connect();
} catch (MessagingException me) {
log.error(getClass().getSimpleName()+" ("+getName()+").deliver(): Connection failed.",me);
// Any error on connect should cause the mailet
// to
// attempt
// to connect to the next SMTP server associated
// with this MX record,
// assuming the number of retries hasn't been
// exceeded.
if (failMessage(qi, rcpt, me, false)) {
return true;
} else {
continue;
}
}
transport.sendMessage(message, addr);
// log.debug("message sent to " +addr);
/*TODO: catch failures that should result
* in failure with no retries
} catch (SendFailedException sfe){
qi.failForRecipient(que, );
*/
} finally {
if (transport != null) {
transport.close();
transport = null;
}
}
logMessageBuffer = new StringBuffer(256)
.append("Mail (")
.append(mail.getName())
.append(") sent successfully to ")
.append(outgoingMailServer);
log.debug(getClass().getSimpleName()+" ("+getName()+").deliver(): "+logMessageBuffer.toString());
qi.succeededForRecipient(que, rcpt);
return true;
} catch (MessagingException me) {
log.error(getClass().getSimpleName()+" ("+getName()+").deliver(): ", me);
// MessagingException are horribly difficult to figure
// out
// what actually happened.
StringBuffer exceptionBuffer = new StringBuffer(256)
.append("Exception delivering message (")
.append(mail.getName())
.append(") - ")
.append(me.getMessage());
log.warn(exceptionBuffer.toString());
if ((me.getNextException() != null)
&& (me.getNextException() instanceof java.io.IOException)) {