gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:59:25 25 4
gpt4 key购买 nike

当我发送附件时,我没有在电子邮件中看到正文消息 (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

关于java - 发送附件时不显示正文消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14547435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com