Java – no body message is displayed when sending attachments
•
Java
When I send an attachment, I can't see the body message in the email (message. Settext (this. Getemailbody());)
MimeMessage message = new MimeMessage(session_m);
message.setFrom(new InternetAddress(this.getEmailSender()));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(this.getEmailRecipient()));
message.setSubject(this.getEmailSubject());
message.setText(this.getEmailBody()); //This won't be displayed if set attachments
Multipart multipart = new MimeMultipart();
for(String file: getAttachmentNameList()){
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.attachFile(this.attachmentsDir.concat(file.trim()));
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
}
Transport.send(message);
System.out.println("Email has been sent");
Solution
You need to use the following:
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(body);
messageBodyPart.setContent(body,"text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//Add the bodypart for the attachment(s)
// Send the complete message parts
message.setContent(multipart); //message is of type - MimeMessage
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
