MessagingException lastError = null;
while ( targetServers.hasNext()) {
try {
HostAddress outgoingMailServer = targetServers.next();
StringBuilder logMessageBuffer =
new StringBuilder(256)
.append("Attempting delivery of ")
.append(mail.getName())
.append(" to host ")
.append(outgoingMailServer.getHostName())
.append(" at ")
.append(outgoingMailServer.getHost())
.append(" for addresses ")
.append(Arrays.asList(addr));
log(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 {
if (authUser != null) {
transport.connect(outgoingMailServer.getHostName(), authUser, authPass);
} else {
transport.connect();
}
} catch (MessagingException me) {
// Any error on connect should cause the mailet to attempt
// to connect to the next SMTP server associated with this
// MX record. Just log the exception. We'll worry about
// failing the message at the end of the loop.
log(me.getMessage());
continue;
}
// if the transport is a SMTPTransport (from sun) some
// performance enhancement can be done.
if (transport.getClass().getName().endsWith(".SMTPTransport")) {
boolean supports8bitmime = false;
try {
Method supportsExtension = transport.getClass().getMethod("supportsExtension", new Class[] {String.class});
supports8bitmime = ((Boolean) supportsExtension.invoke(transport, new Object[] {"8BITMIME"})).booleanValue();
} catch (NoSuchMethodException nsme) {
// An SMTPAddressFailedException with no getAddress method.
} catch (IllegalAccessException iae) {
} catch (IllegalArgumentException iae) {
} catch (InvocationTargetException ite) {
// Other issues with getAddress invokation.
}
// if the message is alredy 8bit or binary and the
// server doesn't support the 8bit extension it has
// to be converted to 7bit. Javamail api doesn't perform
// that conversion, but it is required to be a
// rfc-compliant smtp server.
// Temporarily disabled. See JAMES-638
if (!supports8bitmime) {
try {
convertTo7Bit(message);
} catch (IOException e) {
// An error has occured during the 7bit conversion.
// The error is logged and the message is sent anyway.
log("Error during the conversion to 7 bit.", e);
}
}
} else {
// If the transport is not the one
// developed by Sun we are not sure of how it
// handles the 8 bit mime stuff,
// so I convert the message to 7bit.
try {
convertTo7Bit(message);
} catch (IOException e) {
log("Error during the conversion to 7 bit.", e);
}
}
transport.sendMessage(message, addr);
} finally {
if (transport != null) {
try
{
// James-899: transport.close() sends QUIT to the server; if that fails
// (e.g. because the server has already closed the connection) the message
// should be considered to be delivered because the error happened outside
// of the mail transaction (MAIL, RCPT, DATA).
transport.close();
}
catch (MessagingException e)
{
log("Warning: could not close the SMTP transport after sending mail (" + mail.getName()
+ ") to " + outgoingMailServer.getHostName() + " at " + outgoingMailServer.getHost()
+ " for " + mail.getRecipients() + "; probably the server has already closed the "
+ "connection. Message is considered to be delivered. Exception: " + e.getMessage());
}
transport = null;
}
}
logMessageBuffer =
new StringBuilder(256)
.append("Mail (")
.append(mail.getName())
.append(") sent successfully to ")
.append(outgoingMailServer.getHostName())
.append(" at ")
.append(outgoingMailServer.getHost())
.append(" for ")
.append(mail.getRecipients());
log(logMessageBuffer.toString());
return true;
} catch (SendFailedException sfe) {