Can't set timeout on apache.commons.mail
My program works great most of the time. However, sometimes my SMTP servers seem to close the connection or something, and the program doesn't seem to time out at all.
I am configuring the following options on commons.mail:
SocketConnectionTimeout
SocketTimeout
But it doesn't work as expected.
When my program stays in this section:
DEBUG: JavaMail version 1.4.4
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "MY_SMTP_RELAY_SHOWS_HERE", port 25, isSSL false
{ No further output here }
My email sending code section looks like this:
try {
// Create the email message
MultiPartEmail mpEmail = new MultiPartEmail();
mpEmail.setDebug(true);
mpEmail.setHostName(mail_relay_hostname);
mpEmail.setSmtpPort(smtp_port);
mpEmail.setSocketConnectionTimeout(socket_Connection_Timeout);
mpEmail.setSocketTimeout(socket_Timeout);
for (String toAddres : email.getTo()) {
mpEmail.addTo(toAddres);
}
for (String ccAddres : email.getCc()) {
mpEmail.addCc(ccAddres);
}
for (String bccAddres : email.getBcc()) {
mpEmail.addBcc(bccAddres);
}
mpEmail.setFrom(email.getFrom());
mpEmail.setSubject(email.getSubject());
mpEmail.setMsg(email.getBody());
if (email.hasAttachment()) {
logger.info("Email has attachments.");
int count = 1;
for (File file : email.getAttachmentList()){
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(file.getAbsolutePath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Attachment"+count++);
attachment.setName(file.getName());
// add the attachment
mpEmail.attach(attachment);
}
} else {
logger.info("Email has not attachments.");
}
// send the email
mpEmail.send();
} catch (EmailException ee) {
// TODO: handle exception
ee.printStackTrace();
}
Any TIP is appreciated.
Thank you in advance.
+3
source to share
No one has answered this question yet
Check out similar questions: