//Create an array of the recipients as InternetAddress objects
Collection recipients = mail.getRecipients();
InternetAddress addr[] = new InternetAddress[recipients.size()];
int j = 0;
for (Iterator i = recipients.iterator(); i.hasNext(); j++) {
MailAddress rcpt = (MailAddress)i.next();
addr[j] = rcpt.toInternetAddress();
}
//Figure out which servers to try to send to. This collection
// will hold all the possible target servers
Collection targetServers = null;
if (gatewayServer == null) {
MailAddress rcpt = (MailAddress) recipients.iterator().next();
String host = rcpt.getHost();
//Lookup the possible targets
targetServers = getMailetContext().getMailServers(host);
if (targetServers.size() == 0) {
log("No mail server found for: " + host);
return failMessage(mail, new MessagingException("There are no DNS entries for the hostname " + host + ". I cannot determine where to send this message."), false);
}
} else {
targetServers = new Vector();
targetServers.add(gatewayServer);
}
MessagingException lastError = null;
if (addr.length > 0) {
Iterator i = targetServers.iterator();
while ( i.hasNext()) {
try {
String outgoingmailserver = i.next().toString ();
log("attempting delivery of " + mail.getName() + " to host " + outgoingmailserver + " to " + Arrays.asList(addr));
URLName urlname = new URLName("smtp://" + outgoingmailserver);
Properties props = session.getProperties();
//This was an older version of JavaMail
if (mail.getSender() == null) {
props.put("mail.smtp.user", "<>");
props.put("mail.smtp.from", "<>");
} else {
props.put("mail.smtp.user", mail.getSender().toString());
props.put("mail.smtp.from", mail.getSender().toString());
}
//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 = session.getTransport(urlname);
transport.connect();
transport.sendMessage(message, addr);
transport.close();
log("mail (" + mail.getName() + ") sent successfully to " + outgoingmailserver);
return true;
} catch (MessagingException me) {
//MessagingException are horribly difficult to figure out what actually happened.
log("Exception delivering message (" + mail.getName() + ") - " + me.getMessage());
//Assume it is a permanent exception, or prove ourselves otherwise
boolean permanent = true;
if (me.getNextException() != null && me.getNextException() instanceof java.io.IOException) {
//This is more than likely a temporary failure
//If it's an IO exception with no nested exception, it's probably
// some socket or weird IO related problem.
permanent = false;
}
if (me instanceof SendFailedException) {
SendFailedException sfe = (SendFailedException) me;
//This means there was a partial delivery to certain recipients, so
// whatever caused this exception must have been a permanent error no
// matter what type of exception this was.
if (sfe.getValidSentAddresses() != null && sfe.getValidSentAddresses().length > 0) {
permanent = true;
}
}
//Now take action based on whether this was a permanent or temporary action
if (permanent) {
//If it's permanent, fail immediately.
//Note that this has changed as we have all our logic in the outer block
// for what should happen when we have a delivery failure.
throw me;
} else {
//Record what the last error is and continue the loop in case
// there are other servers we could try.
lastError = me;
continue;
}
}
} // end while
//If we encountered an exception while looping through, send the last exception we got
if (lastError != null) {
throw lastError;
}
} else {
log("no recipients specified... not sure how this could have happened.");
}
} catch (SendFailedException sfe) {
//Would like to log all the types of email addresses
if (sfe.getValidSentAddresses() != null) {
Address[] validSent = sfe.getValidSentAddresses();
Collection recipients = mail.getRecipients();
//Remove these addresses for the recipients
for (int i = 0; i < validSent.length; i++) {
try {
MailAddress addr = new MailAddress(validSent[i].toString());
recipients.remove(addr);
} catch (ParseException pe) {
//ignore once debugging done
pe.printStackTrace();
}