当我发送附件时,我在电子邮件中看不到正文消息(message.setText(this.getEmailBody());).
如果没有附件,电子邮件将显示正文消息.电子邮件将发送到Gmail帐户.任何线索为什么会发生这种情况?
如果没有附件,电子邮件将显示正文消息.电子邮件将发送到Gmail帐户.任何线索为什么会发生这种情况?
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");
解决方法
您需要使用以下内容:
// 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
原文地址:https://www.jb51.cc/java/120563.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。