简体   繁体   English

我尝试在MimeMessage的Java方法中发送带有主体内容附件的电子邮件时,主体内容未发送

[英]Body content is not being sent while I am trying to send an email with attachment with main body content in Java methods of MimeMessage

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.setFileName("abc.xls");
message.setText("Fill the content:");

Above is the main part of code which I am using. 上面是我正在使用的代码的主要部分。 While I use the above code, i don't see main body content "Fill the content" in the sent mail. 当我使用上面的代码时,在发送的邮件中看不到主体内容“填充内容”。 There are other posts and comments which have piece of code which works that's this problem cab be resolved by using MimeMultipart & MimeBodyPart class. 还有其他帖子和评论,其中包含一段有效的代码,可以使用MimeMultipart和MimeBodyPart类来解决。 But no where it's explained the reason of why the above code is not working. 但是没有任何地方可以解释上述代码为何无法正常工作的原因。

I am also aware that using setFileName is not enough to add the content present inside the file, it's just used to add the attachment without content. 我还知道,使用setFileName不足以添加文件中存在的内容,它仅用于添加不包含内容的附件。

Note: I am using javax.mail-1.5.0.jar 注意:我正在使用javax.mail-1.5.0.jar

Can you please explain the reason of the above code not working? 您能否解释上述代码无法正常工作的原因?

Thanks in advance. 提前致谢。

A mail that contains a text message and one or more attachments must be a MultiPart message, because that's the way such a mail is constructed so the receiving mail client understands it. 包含文本消息和一个或多个附件的邮件必须是MultiPart邮件,因为那样构造邮件就可以使接收邮件的客户端理解它。

In your simple example, you are not constructing a mail that has an excel file abc.xls as attachment; 在您的简单示例中,您将不构建具有excel文件abc.xls作为附件的邮件。 instead, you create a text mail and tell the client that the body of this mail should be named abc.xls . 而是创建一个文本邮件,并告诉客户端该邮件的主体应命名为abc.xls Most likely, the receiving mail client will offer a text file with the content Fill the content: , inappropriately named abc.xls , as an attachment of an otherwise empty mail; 接收邮件客户端很可能会提供一个文本文件,其中包含内容Fill the content: abc.xls不恰当地命名为abc.xls ,否则将是空邮件的附件; opening the supposed Excel file will probably cause Excel to import this text file. 打开假定的Excel文件可能会导致Excel导入此文本文件。

TL;DR: Use MimeMultiPart to create mails with attachments. TL; DR:使用MimeMultiPart创建带有附件的邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM