我在Tomcat6中有一个电子邮件会话:
<Resource name="email" scope="Shareable" type="javax.mail.Session" mail.debug="true" mail.smtp.user="XXXX@gmail.com" mail.smtp.password="XXXX" mail.smtp.host="smtp.gmail.com" mail.smtp.port="465" mail.smtp.auth="true" mail.smtp.socketFactory.port="465" mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory" />
我使用以下代码完美发送电子邮件:
javax.mail.Session sesion = getEmailSession();
msg.setFrom(new InternetAddress("XXXX@gmail.com"));
msg.setRecipients(new InternetAddress("XXXX@gmail.com"));
msg.setSubject(MimeUtility.encodeText("About","iso-8859-1","Q"));
Multipart mp = new MimeMultipart();
MimeBodyPart mbp1 = new MimeBodyPart();
MimeBodyPart mbp2 = new MimeBodyPart();
mbp1.setText("Text","ISO-8859-15");
FileDataSource fds = new FileDataSource("/opt/tmp/file.txt");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);
msg.setSentDate(new Date());
Transport.send(msg);
我在我尝试过的所有服务器(Windows和Linux)中都能正常工作,但是在一台服务器中它不起作用。连接突然关闭。
我调试了javamail通信,我有:
DEBUG: JavaMail version 1.4.5
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 true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
220 smtp.gmail.com ESMTP w67sm3313556wmw.17 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
EHLO XXXX
250-smtp.gmail.com at your service, [88.2.96.135]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<XXXX@gmail.com>
250 2.1.0 OK w67sm3313556wmw.17 - gsmtp
RCPT TO:<XXX@gmail.com>
250 2.1.5 OK w67sm3313556wmw.17 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP: XXX@gmail.com
DATA
354 Go ahead w67sm3313556wmw.17 - gsmtp
Date: Wed, 2 Dec 2015 14:49:44 +0100 (CET)
From: XXX@gmail.com
To: XXX@gmail.com
Message-ID: <17032702.26.1449064184719.JavaMail.root@Sage-Portal>
Subject: About
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_25_13365288.1449064184718"
------=_Part_25_13365288.1449064184718
Content-Type: text/plain; charset=ISO-8859-15
Content-Transfer-Encoding: quoted-printable
Text
------=_Part_25_13365288.1449064184718
Content-Type: application/octet-stream; name=file.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=file.txt
JAVAMAIL 1.4.5
------=_Part_25_13365288.1449064184718--
.
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: got response code -1, with response: [EOF]
RSET
DEBUG SMTP: MessagingException while sending
javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
java.net.SocketException: Connection closed by remote host
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2163)
在此服务器中,有时会发送带附件的电子邮件,但通常不会发送。如果电子邮件没有附件,则始终发送。在其他服务器中,电子邮件总是发送。
可能是什么问题:Tomcat6,Gmail,Javamail,服务器......