java – 发送附件时不显示正文消息

当我发送附件时,我在电子邮件中看不到正文消息(message.setText(this.getEmailBody());).
如果没有附件,电子邮件显示正文消息.电子邮件将发送到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

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...