在Tomcat 6.0上使用Javamail發送帶附件的電子郵件時遇到問題。Javamail發送帶附件的郵件適用於Windows,但不適用於Linux
在我的機器上使用Tomcat服務器發送郵件時(Windows 7 64),它工作正常。我已經用本地smtp服務器和一個Gmail賬戶作爲中繼來測試它。但是,當我的遠程Tomcat服務器(運行linux)發送郵件時,收到的郵件亂七八糟,沒有主題,附件顯示爲文本(原始爲pdf文件),並且一些郵件標頭與郵件混合在一起。我嘗試過使用在Windows上測試時使用的相同方法:嘗試使用本地smtp服務器,然後使用gmail作爲中繼。它在兩種情況下都不起作用。
我也嘗試更改我的本地JavaMail庫,並使用遠程服務器上的相同內容(從服務器複製到我的機器),但運行本地的結果始終相同。電子郵件以預期的方式收到。
當我發送電子郵件時,它會發送另一份副本給我,所以我可以跟蹤發生了什麼。在日誌下面展示了兩種郵件(原件和複印件),運行使用嵌入在我的j2ee.jar JavaMail的本地測試時(請參見下面的其他細節)JAVAMAIL輸出:
DEBUG: setDebug: JavaMail version 1.3.1
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 587
220 mx.google.com ESMTP jz16sm6498879vdb.9
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
EHLO Felipe-HP
250-mx.google.com at your service, [177.17.162.156]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250-ENHANCEDSTATUSCODES
250 STARTTLS
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 xxxxxxxxx
334 xxxxxxxxx
235 2.7.0 Accepted
DEBUG SMTP: use8bit false
MAIL FROM:< [email protected]>
250 2.1.0 OK jz16sm6498879vdb.9
RCPT TO:< [email protected]>
250 2.1.5 OK jz16sm6498879vdb.9
DEBUG SMTP: Verified Addresses
DEBUG SMTP: [email protected]
DATA
354 Go ahead jz16sm6498879vdb.9
Message-ID: <[email protected]>
From: [email protected]
To: [email protected]
Subject: Boleto Amaua - 06/05/2012
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_1451495453.1335464128994"
------=_Part_0_1451495453.1335464128994
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
------=_Part_0_1451495453.1335464128994
Content-Type: application/octet-stream;
name=boleto-158-255-1335464128903.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=boleto-158-255-1335464128903.pdf
Content-Description: Attached file: boleto-158-255-1335464128903.pdf
------=_Part_0_1451495453.1335464128994--
.
250 2.0.0 OK 1335464133 jz16sm6498879vdb.9
QUIT
DEBUG: setDebug: JavaMail version 1.3.1
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 587
220 mx.google.com ESMTP jz16sm6499184vdb.9
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
EHLO Felipe-HP
250-mx.google.com at your service, [177.17.162.156]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250-ENHANCEDSTATUSCODES
250 STARTTLS
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 xxxxxxxxxx
334 xxxxxxxxxx
235 2.7.0 Accepted
DEBUG SMTP: use8bit false
MAIL FROM:< [email protected]>
250 2.1.0 OK jz16sm6499184vdb.9
RCPT TO:< [email protected]>
250 2.1.5 OK jz16sm6499184vdb.9
DEBUG SMTP: [email protected]
DEBUG SMTP: [email protected]
DATA
354 Go ahead jz16sm6499184vdb.9
Message-ID: <[email protected]>
From: [email protected]
To: [email protected]
Subject: =?ISO-8859-1?Q?C=F3pia_Boleto_Amaua_-_06/05/2012?=
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_1_1973765399.1335464133435"
------=_Part_1_1973765399.1335464133435
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
###################################################################
Mail Content
###################################################################
------=_Part_1_1973765399.1335464133435
Content-Type: application/octet-stream;
name=boleto-158-255-1335464128903.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=boleto-158-255-1335464128903.pdf
Content-Description: Attached file: boleto-158-255-1335464128903.pdf
##################################################################
Attachment content
##################################################################
------=_Part_1_1973765399.1335464133435--
.
250 2.0.0 OK 1335464137 jz16sm6499184vdb.9
QUIT
下面是所示的日誌我的服務器生成:
Loading javamail.default.providers from jar:file:/opt/tomcat60/lib/mail.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
Loading javamail.default.providers from jar:file:/home/xxxxxxx/public_html/WEB-INF/lib/mail.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: getProvider() returning provider protocol=smtp; [email protected]; class=com.sun.mail.smtp.SMTPTransport; vendor=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 587, isSSL false
220 mx.google.com ESMTP dc7sm2927045vdc.6
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
EHLO l50dnn0358.locaweb.com.br
250-mx.google.com at your service, [187.45.198.10]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO l50dnn0358.locaweb.com.br
250-mx.google.com at your service, [187.45.198.10]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", 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:<[email protected]>
250 2.1.0 OK dc7sm2927045vdc.6
RCPT TO:<[email protected]>
250 2.1.5 OK dc7sm2927045vdc.6
DEBUG SMTP: Verified Addresses
DEBUG SMTP: [email protected]
DATA
354 Go ahead dc7sm2927045vdc.6
------=_Part_0_16475596.1335412817148
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
###################################################################
Mail Content
###################################################################
------=_Part_0_16475596.1335412817148
Content-Type: application/octet-stream; name=boleto-1436-1707-1335412817075.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=boleto-1436-1707-1335412817075.pdf
Content-Description: Attached file: boleto-1436-1707-1335412817075.pdf
##################################################################
Attachment content
##################################################################
------=_Part_0_16475596.1335412817148--
.
250 2.0.0 OK 1335412824 dc7sm2927045vdc.6
QUIT
221 2.0.0 closing connection dc7sm2927045vdc.6
DEBUG: getProvider() returning provider protocol=smtp; [email protected]; class=com.sun.mail.smtp.SMTPTransport; vendor=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 587, isSSL false
220 mx.google.com ESMTP c17sm2919516vdj.12
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
EHLO l50dnn0358.locaweb.com.br
250-mx.google.com at your service, [187.45.198.10]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO l50dnn0358.locaweb.com.br
250-mx.google.com at your service, [187.45.198.10]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", 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:<[email protected]>
250 2.1.0 OK c17sm2919516vdj.12
RCPT TO:<[email protected]>
250 2.1.5 OK c17sm2919516vdj.12
DEBUG SMTP: Verified Addresses
DEBUG SMTP: [email protected]
DATA
354 Go ahead c17sm2919516vdj.12
------=_Part_1_22662950.1335412824767
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
###################################################################
Mail Content
###################################################################
------=_Part_1_22662950.1335412824767
Content-Type: application/octet-stream; name=boleto-1436-1707-1335412817075.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=boleto-1436-1707-1335412817075.pdf
Content-Description: Attached file: boleto-1436-1707-1335412817075.pdf
##################################################################
Attachment content
##################################################################
------=_Part_1_22662950.1335412824767--
.
250 2.0.0 OK 1335412828 c17sm2919516vdj.12
QUIT
221 2.0.0 closing connection c17sm2919516vdj.12
在我的遠程服務器上,我們有上下文/ WEB-INF/lib中的JavaMail庫。我們擁有一個mail.jar和一個包含JavaMail的j2ee.jar。另外,在tomcat文件夾中還有另一個mail.jar(可以在日誌中看到)和另一個包含Javamail的j2ee.jar。
我試着用最近的一個替換服務器的/WEB-INF/lib/mail.jar,但日誌甚至沒有改變。在我的本地測試中使用來自服務器的.jars會產生正確的結果。
請注意,我的服務器不顯示一些應該存在的調試行。
下面是用來發送電子郵件的代碼:
Properties props = new Properties();
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.mime.charset", "ISO-8859-1");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", Integer.toString(port));
this.session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(usr, pass);
}
});
MimeMultipart mp = new MimeMultipart("mixed");
MimeBodyPart txtPart = new MimeBodyPart();
if(this.message == null){
if(this.session != null){
this.message = new MimeMessage(this.session);
} else {
throw new Exception("");
}
}
this.session.setDebug(true);
this.message.setFrom(new InternetAddress(this.from));
this.message.setRecipient(Message.RecipientType.TO, new InternetAddress(this.to));
this.message.setSubject(this.subject);
txtPart.setDisposition(Part.INLINE);
txtPart.setText(text);
mp.addBodyPart(txtPart);
for (int i = 0; i < this.attach.size(); i++) {
MimeBodyPart filePart = new MimeBodyPart();
File file = (File) this.attach.elementAt(i);
FileDataSource fds = new FileDataSource(file);
DataHandler dh = new DataHandler(fds);
filePart.setFileName(file.getName());
filePart.setDisposition(Part.ATTACHMENT);
filePart.setDescription("Attached file: " + file.getName());
filePart.setDataHandler(dh);
mp.addBodyPart(filePart);
}
this.message.setContent(mp);
Transport.send(this.message);
任何進一步的細節需要,請詢問。
感謝您的考慮!
它並沒有奏效。我將JavaMail 1.4.5複製到/ WEB-INF/lib /並刪除了j2ee.jar。 JavaMail調試輸出保持不變,就好像我沒有刪除j2ee.jar並將新的JavaMail複製到應用程序庫。我誤解了Tomcat安裝中的j2ee.jar,但我可以確認那裏有一個mail.jar,裏面有JavaMail 1.3.1類。我需要確保刪除該文件能夠解決問題,因爲我們公司在該服務器上有其他產品,並且我不知道是否刪除該mail.jar(在tomcat安裝內)會中斷任何應用程序。謝謝! – b020 2012-04-27 20:00:56
還有一個Tomcat選項可以讓你的應用中的類優先於服務器中的類;這可能有助於這裏。 – 2012-04-27 23:17:23
我找不到任何關於此的內容。也許我在搜索中使用了錯誤的關鍵字。你能指點一下如何改變這個選項的方向嗎? – b020 2012-04-30 18:03:38