[英]java email attachment being sent as plain text in body
I'm having an issue with sending attachments via email in java using the java mail (1.4.6). 我在使用Java邮件(1.4.6)通过Java通过电子邮件发送附件时遇到问题。 It seems that when I attach a document of any sort and send it, the recipient gets the file in plain text format in the body.
看来,当我附加任何类型的文档并将其发送时,收件人将在正文中获取纯文本格式的文件。 Rather than, you guessed it, sending the whole file like you would expect and the body not interfered with.
并非您猜到了,而是像您期望的那样发送了整个文件,并且身体没有受到干扰。
Code 码
try
{
// Create a message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(Compose.to));
message.setSubject(Compose.subject);
//message.setText(Compose.body);
//If there are no CC's then skip it. This if seemed to decrease send time.
if(Compose.cc != null)
{
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(Compose.cc));
message.saveChanges();
}
else
message.saveChanges();
/*
* For adding the attached file to the email. This time the if
* statement is used to stop the email attachment process if there
* is none. Other wise due to the way I've set it up it'll try to
* send file path and file name as null, and we fail an otherwise valid email.
*/
if(Compose.filename != null)
{
String file = Compose.filepath;
String fileName = Compose.filename;
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(Compose.body);
multipart.addBodyPart(messageBodyPart2);
message.setContent(multipart);
}
else
{
message.setText(Compose.body);
message.saveChanges();
}
//Send the message by javax.mail.Transport .
Transport tr = session.getTransport("smtp"); // Get Transport object from session
tr.connect(smtphost, username, password); // We need to connect
tr.sendMessage(message, message.getAllRecipients()); // Send message
//Notify the user everything functioned fine.
JOptionPane.showMessageDialog(null, "Your mail has been sent.");
}
Thinking over this I remembered how FileDataSource()
is an overloaded statement taking a string or a file type as a parameter, trying both I got the same result but I will experiment more with the the file type now. 考虑这一点,我记得
FileDataSource()
是一个重载的语句,将字符串或文件类型作为参数,尝试两者都得到相同的结果,但是现在我将对文件类型进行更多的实验。
EDIT: After more testing I noticed that sometimes the file will not appear along with whatever was in the body at the time of sending. 编辑:经过更多测试后,我注意到有时文件不会与发送时正文中的任何内容一起出现。
You're setting the attachment as the first body part. 您将附件设置为主体的第一部分。 It needs to be the second body part.
它必须是第二部分。
Also, consider upgrading to JavaMail 1.5.4 and using the MimeBodyPart.attachFile method to attach the file. 另外,请考虑升级到JavaMail 1.5.4,并使用MimeBodyPart.attachFile方法附加文件。
For each part you have to set disposition to Part.INLINE for the body and Part.ATTACHMENT for the attachment. 对于每个零件,您必须将主体的处置设置为Part.INLINE ,并将附件的处置设置为Part.ATTACHMENT 。 The attachFile methods will do that for you.
attachFile方法将为您完成此操作。 Avoid JavaMail 1.4.6 in favor of the latest release or at least use JavaMail 1.4.7 which contains fixes for known issues with JavaMail 1.4.6.
避免使用JavaMail 1.4.6来支持最新版本,或者至少使用JavaMail 1.4.7,其中包含针对JavaMail 1.4.6的已知问题的修复程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.