java 电子邮件附件在正文中以纯文本形式发送
java email attachment being sent as plain text in body
我在 java 中使用 java 邮件 (1.4.6) 通过电子邮件发送附件时遇到问题。似乎当我附加任何类型的文档并发送时,收件人会在正文中以纯文本格式获取文件。而不是,你猜对了,像你期望的那样发送整个文件并且正文没有受到干扰。
代码
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.");
}
考虑到这一点,我想起了 FileDataSource()
是如何将字符串或文件类型作为参数的重载语句,尝试了这两种方法我得到了相同的结果,但我现在将对文件类型进行更多试验。
编辑:经过更多测试后,我注意到有时文件不会与发送时正文中的任何内容一起出现。
您正在将附件设置为第一个 body 部分。它需要是第二个 body 部分。
此外,考虑升级到 JavaMail 1.5.4 and using the MimeBodyPart.attachFile 方法来附加文件。
对于每个部分,您都必须 set disposition to Part.INLINE for the body and Part.ATTACHMENT 作为附件。 attachFile 方法将为您完成。避免使用 JavaMail 1.4.6 以支持最新版本或至少使用 JavaMail 1.4.7,它包含对 JavaMail 1.4.6 已知问题的修复。
我在 java 中使用 java 邮件 (1.4.6) 通过电子邮件发送附件时遇到问题。似乎当我附加任何类型的文档并发送时,收件人会在正文中以纯文本格式获取文件。而不是,你猜对了,像你期望的那样发送整个文件并且正文没有受到干扰。
代码
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.");
}
考虑到这一点,我想起了 FileDataSource()
是如何将字符串或文件类型作为参数的重载语句,尝试了这两种方法我得到了相同的结果,但我现在将对文件类型进行更多试验。
编辑:经过更多测试后,我注意到有时文件不会与发送时正文中的任何内容一起出现。
您正在将附件设置为第一个 body 部分。它需要是第二个 body 部分。
此外,考虑升级到 JavaMail 1.5.4 and using the MimeBodyPart.attachFile 方法来附加文件。
对于每个部分,您都必须 set disposition to Part.INLINE for the body and Part.ATTACHMENT 作为附件。 attachFile 方法将为您完成。避免使用 JavaMail 1.4.6 以支持最新版本或至少使用 JavaMail 1.4.7,它包含对 JavaMail 1.4.6 已知问题的修复。