Java 邮件附件在 Tomcat 上不起作用
我有一个通过电子邮件发送确认的应用程序。电子邮件部分利用 Commons Mail API。发送邮件的简单代码如下所示;
import org.apache.commons.mail.*;
...
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL("http://cashew.org/doc.pdf"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Testing attach");
attachment.setName("doc.pdf");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.cashew.com");
email.addTo("[email protected]");
email.setFrom("[email protected]");
email.setSubject("Testing);
email.setMsg("testing message");
// add the attachment
email.attach(attachment);
// send the email
email.send();
我的问题是,当我从 Eclipse 执行此应用程序时,我收到带有附件的电子邮件,没有任何问题。但是当我将应用程序部署到 Tomcat 服务器时(我已经
尝试了版本 5 和 6,但没有任何效果),电子邮件将发送以下内容;
------=_Part_0_25002283.1275298567928
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
testing
Regards,
los
------=_Part_0_25002283.1275298567928
Content-Type: application/pdf; name="doc.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="doc.pdf"
Content-Description: Testing attach
JVBERi0xLjQNJeLjz9MNCjYzIDAgb2JqDTw8L0xpbmVhcml6ZWQgMS9MIDMxMzE4Mi9PIDY1L0Ug
Mjg2NjY5L04gMS9UIDMxMTgwMi9IIFsgMjgzNiAzNzZdPj4NZW5kb2JqDSAgICAgICAgICAgICAg
DQp4cmVmDQo2MyAxMjcNCjAwMDAwMDAwMTYgMDAwMDAgbg0KMDAwMDAwMzM4MCAwMDAwMCBuDQow
MDAwMDAzNTIzIDAwMDAwIG4NCjAwMDAwMDQzMDcgMDAwMDAgbg0KMDAwMDAwNTEwOSAwMDAwMCBu
DQowMDAwMDA2Mjc5IDAwMDAwIG4NCjAwMDAwMDY0MTAgMDAwMDAgbg0KMDAwMDAwNjU0NiAwMDAw
MCBuDQowMDAwMDA3OTY3IDAwMDAwIG4NCjAwMDAwMDkwMjMgMDAwMDAgbg0KMDAwMDAwOTk0OSAw
MDAwMCBuDQowMDAwMDExMDAwIDAwMDAwIG4NCjAwMDAwMTIwNTkgMDAwMDAgbg0KMDAwMDAxMjky
MCAwMDAwMCBuDQowMDAwMDEyOTU0IDAwMDAwIG4NCjAwMDAwMTI5ODIgMDAwMDAgbg0KMDAwMDAx
.......
CnN0YXJ0eHJlZg0KMTE2DQolJUVPRg0K
------=_Part_0_25002283.1275298567928--
我还注意到的一件事是,标题信息不显示 TO 和主题值。嗯,很奇怪。 我必须指出,上面不是DEBUG生成的,它是我的outlook客户端中收到的实际消息。
有人可以帮我吗!
更新:该应用程序非常简单。附件和文本消息也作为单独的部分。
public final void Email(String from, String to, String cc,
String subject, String message, String doc, String bcc) {
MultiPartEmail email = new MultiPartEmail();
try {
if (!(doc == null)) {
EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL("http://cashew.org/doc.pdf"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Testing attach");
attachment.setName("doc.pdf");
email.attach(attachment);
}
email.setHostName("mail.cashew.com");
// [ Set Header details
email.setTo(getAddress(to));
email.setFrom(from);
email.setSubject(subject);
if (!(cc == null)) {
eEmail.setCc(getAddress(cc));
}
if (!(bcc == null)) {
email.setBcc(getAddress(bcc));
}
email.setMsg(message);
email.send();
} catch (Exception ex) {
ex.printStackTrace();
}
}
有谁知道发生了什么事吗?
I have an application which e-mails confirmations. The email part utilises Commons Mail API. The simple code which does the send mail is as shown below;
import org.apache.commons.mail.*;
...
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL("http://cashew.org/doc.pdf"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Testing attach");
attachment.setName("doc.pdf");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.cashew.com");
email.addTo("[email protected]");
email.setFrom("[email protected]");
email.setSubject("Testing);
email.setMsg("testing message");
// add the attachment
email.attach(attachment);
// send the email
email.send();
My problem is, when I execute this application from Eclipse, I get email sent with attachment without any issues. But when i deploy the application to Tomcat server (I have
tried both version 5 & 6 no joy), the e-mail is sent with below content;
------=_Part_0_25002283.1275298567928
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
testing
Regards,
los
------=_Part_0_25002283.1275298567928
Content-Type: application/pdf; name="doc.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="doc.pdf"
Content-Description: Testing attach
JVBERi0xLjQNJeLjz9MNCjYzIDAgb2JqDTw8L0xpbmVhcml6ZWQgMS9MIDMxMzE4Mi9PIDY1L0Ug
Mjg2NjY5L04gMS9UIDMxMTgwMi9IIFsgMjgzNiAzNzZdPj4NZW5kb2JqDSAgICAgICAgICAgICAg
DQp4cmVmDQo2MyAxMjcNCjAwMDAwMDAwMTYgMDAwMDAgbg0KMDAwMDAwMzM4MCAwMDAwMCBuDQow
MDAwMDAzNTIzIDAwMDAwIG4NCjAwMDAwMDQzMDcgMDAwMDAgbg0KMDAwMDAwNTEwOSAwMDAwMCBu
DQowMDAwMDA2Mjc5IDAwMDAwIG4NCjAwMDAwMDY0MTAgMDAwMDAgbg0KMDAwMDAwNjU0NiAwMDAw
MCBuDQowMDAwMDA3OTY3IDAwMDAwIG4NCjAwMDAwMDkwMjMgMDAwMDAgbg0KMDAwMDAwOTk0OSAw
MDAwMCBuDQowMDAwMDExMDAwIDAwMDAwIG4NCjAwMDAwMTIwNTkgMDAwMDAgbg0KMDAwMDAxMjky
MCAwMDAwMCBuDQowMDAwMDEyOTU0IDAwMDAwIG4NCjAwMDAwMTI5ODIgMDAwMDAgbg0KMDAwMDAx
.......
CnN0YXJ0eHJlZg0KMTE2DQolJUVPRg0K
------=_Part_0_25002283.1275298567928--
One thing also I have noticed is, the header information donot show TO and Subject values. Hmm pretty wierd.
I have to point out that, above is not generated of DEBUG, it is the actual message recieved in my outlook client.
Can someone help me please!
Update: The application is pretty simple. Attachment a Part and text message as a seperate part also.
public final void Email(String from, String to, String cc,
String subject, String message, String doc, String bcc) {
MultiPartEmail email = new MultiPartEmail();
try {
if (!(doc == null)) {
EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL("http://cashew.org/doc.pdf"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Testing attach");
attachment.setName("doc.pdf");
email.attach(attachment);
}
email.setHostName("mail.cashew.com");
// [ Set Header details
email.setTo(getAddress(to));
email.setFrom(from);
email.setSubject(subject);
if (!(cc == null)) {
eEmail.setCc(getAddress(cc));
}
if (!(bcc == null)) {
email.setBcc(getAddress(bcc));
}
email.setMsg(message);
email.send();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Do anyone knows what's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是问题
This was the issue