我的程序大多数情况下工作正常。但是,有时我的SMTP服务器似乎关闭了连接或其他东西,程序似乎根本没有超时。
我在commons.mail上设置了以下参数:
SocketConnectionTimeout
了socketTimeout
但它没有按预期工作
当我的程序挂起时,请停留在此部分:
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 }
我的发送电子邮件代码部分如下所示:
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();
}
欢迎任何提示。
提前谢谢。