当前位置: 首页 > 知识库问答 >
问题:

Javamail发送带有附件的邮件可以在Windows上工作,但不能在Linux

贺光华
2023-03-14

在Tomcat 6.0上使用Javamail通过linux发送带有附件的电子邮件时,我遇到了问题。

在我的机器(Windows 7 64)上使用Tomcat服务器发送邮件时,它工作正常。我已经用本地smtp服务器和gmail帐户作为中继进行了测试。但当邮件由运行linux的远程Tomcat服务器发送时,收到的邮件很混乱,没有主题,附件显示为文本(原始是pdf文件),一些邮件标题与邮件混合在一起。我尝试过使用在Windows上测试时使用的方法:尝试使用本地smtp服务器,然后使用gmail作为中继。这两种情况都不起作用。

我还尝试更改本地JavaMail库,并使用远程服务器上的相同库(从服务器复制到我的机器),但运行本地的结果总是相同的。电子邮件以预期的方式接收。

当我发送电子邮件时,它会被发送另一份副本给我,这样我就可以跟踪正在发生的事情。下面的日志显示了在使用嵌入在我的j2ee中的Javamail运行本地测试时,两个邮件(原始邮件和副本)的Javamail输出。jar(详见下文):

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:< xxxxx@xxxxx.xxxx.xxxx>
250 2.1.0 OK jz16sm6498879vdb.9
RCPT TO:< xxxxx@xxxxx.xxxx.xxxx>
250 2.1.5 OK jz16sm6498879vdb.9
DEBUG SMTP: Verified Addresses
DEBUG SMTP:    xxxxx@xxxxx.xxxx.xxxx
DATA
354  Go ahead jz16sm6498879vdb.9
Message-ID: <38047647.1335464129114.JavaMail.Felipe@Felipe-HP>
From:  xxxxx@xxxxx.xxxx.xxxx
To:  xxxxx@xxxxx.xxxx.xxxx
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:< xxxxx@xxxxx.xxxx.xxxx>
250 2.1.0 OK jz16sm6499184vdb.9
RCPT TO:< xxxxx@xxxxx.xxxx.xxxx>
250 2.1.5 OK jz16sm6499184vdb.9
DEBUG SMTP:  xxxxx@xxxxx.xxxx.xxxx
DEBUG SMTP:    xxxxx@xxxxx.xxxx.xxxx
DATA
354  Go ahead jz16sm6499184vdb.9
Message-ID: <895165784.1335464133437.JavaMail.Felipe@Felipe-HP>
From:  xxxxx@xxxxx.xxxx.xxxx
To:  xxxxx@xxxxx.xxxx.xxxx
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; type=javax.mail.Provider$Type@67aece; 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:<xxxxx@xxxxx.xxxx.xxxx>
250 2.1.0 OK dc7sm2927045vdc.6
RCPT TO:<xxxxx@xxxxx.xxxx.xxxx>
250 2.1.5 OK dc7sm2927045vdc.6
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   xxxxx@xxxxx.xxxx.xxxx
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; type=javax.mail.Provider$Type@67aece; 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:<xxxxxxx@xxxxx.xxxx.xxxx>
250 2.1.0 OK c17sm2919516vdj.12
RCPT TO:<xxxxx@xxxxx.xxxx.xxxx>
250 2.1.5 OK c17sm2919516vdj.12
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   xxxxx@xxxxx.xxxx.xxxx
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与最近的一个jar兼容,但日志甚至没有改变。并使用。本地测试中来自服务器的JAR会产生正确的结果。

请注意,我的服务器没有显示一些应该出现的调试行。

以下是用于发送电子邮件的代码:

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);

如需更多详细信息,请咨询。

谢谢你的考虑!

共有1个答案

温源
2023-03-14

尝试从应用程序中删除j2ee.jar;它包含一些JavaMail类的非常旧的版本,可能与mail.jar.中的新版本冲突

(j2ee.jar仅用于编译时;不应在运行时使用。)

 类似资料:
  • 问题内容: Jenkins可以发送邮件通知带有附件的收件人吗?对我来说更重要的是(可选),它也可以从文件* .xls中读取并将其添加到电子邮件文本中。 或者我可以将系统变量添加到通过詹金斯插件发送的数据(外部邮件)中吗? 问题答案: 截至2011年9月的新答案: 是 。问题9018已解决。 从Jenkins email-ext插件2.15版本开始,您可以在Jenkins构建通知中发送附件。在此处查

  • 我试图制作一个跨平台的JavaFX应用程序,它在Windows和OSX机器上工作得很好,但在Linux上不行。 jar是在Intellij思想中使用基本的JavaFX配置构建的。 有人帮忙吗?

  • 我试图在javamail API的帮助下发送两个附件,但发件人正在接收附件作为转发消息传递给发件人的邮件。我也看不到邮件中的正文。我正在向outlook的电子邮件客户端发送邮件。它类似的附件在同一封邮件中以独立邮件的形式出现,当我点击邮件中的独立邮件时,它会显示附件。就像我在同一封邮件中打开了另一封邮件一样。谁能检查一下我做错了什么吗 下面是代码 我只想让发件人在一个窗口中接收附件。

  • 问题内容: 我遇到了一个问题:直到今天,我使用包含以下内容的标头使用PHP发送HTML邮件 现在,我添加了添加附件的功能。为此,我不得不将此行更改为 现在,使用,邮件的其余部分(即普通文本)将显示为文本/纯文本。我怎么能知道附件有效并且邮件文本仍然是HTML? 问题答案: 要发送带有附件的电子邮件,我们需要使用multipart / mixed MIME类型,该类型指定混合类型将包含在电子邮件中。

  • 我需要在我的应用程序中集成邮件服务。我的应用程序应该发送电子邮件,其中必须附上。pdf文件(详细报告)。PDF文件现在位于远程服务器上。 有人能帮我理解如何在黑莓手机上发送带有pdf附件的邮件吗?如果实施是可行的,那么我将如何实施? 以下是我从Blackberry知识库论坛获得的用Blackberry发送邮件的链接 如何-创建附件 如何创建和发送消息

  • 问题内容: 是否有用于NodeJS的库,用于发送带有附件的邮件? 问题答案: 是的,这非常简单,我使用nodemailer: 然后阅读文件并发送电子邮件: