gpt4 book ai didi

JavaMail MimeMultiPart 不发送全部内容

转载 作者:行者123 更新时间:2023-12-01 13:49:39 26 4
gpt4 key购买 nike

我已经被一个奇怪的问题困扰了一段时间,但我确实没有发现我的代码有任何问题。我正在尝试使用 java 发送一封电子邮件,其中我将图像附加到消息中。首先我有纯文本消息,然后是附加图像并使用 html 显示它。

我认为它应该是这样的:

+-----------------------------------------------+ 
| multipart/related |
| +---------------------------+ +------------+ |
| |multipart/alternative | | image/gif | |
| | +-----------+ +---------+ | | | |
| | |text/plain | |text/html| | | | |
| | +-----------+ +---------+ | | | |
| +---------------------------+ +------------+ |
+-----------------------------------------------+

这是我的代码:

public void sendEmail(Email email) throws MessagingException {
String[] recipients = email.getRecipients();
// Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
//props.put("mail.smtps.auth", "true");

// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);

// create a message
MimeMessage msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(email.getFrom());
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
msg.setSubject(email.getSubject());

BodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(email.getMessage(), "text/plain");

BodyPart mbp2 = new MimeBodyPart();
mbp2.setContent("<html><body><br/><br/><br/><img src=\"cid:image"><br/>"
+ "Support<br/>"
+ "Carl Olofsson<br/>"
+ "Management Unit<br/>"
+ "----<br/>"
+ "----<br/>"
+ "Sweden<br/>"
+ "www.-----.com<br/></body></html>", "text/html");

Multipart contentMultipart = new MimeMultipart("alternative");
contentMultipart.addBodyPart(mbp1);
contentMultipart.addBodyPart(mbp2);

BodyPart contentBodyPart = new MimeBodyPart();
contentBodyPart.setContent(contentMultipart);

BodyPart mbp3 = new MimeBodyPart();
DataSource ds = new FileDataSource("C:/Users/000/Desktop/image.jpg");
mbp3.setDataHandler(new DataHandler(ds));
mbp3.setHeader("Content-ID","image");

Multipart entireMultipart = new MimeMultipart("related");
entireMultipart.addBodyPart(contentBodyPart);
entireMultipart.addBodyPart(mbp3);

// attaching the multi-part to the message
msg.setContent(entireMultipart);

// set the message content here
Transport t = session.getTransport("smtp");
try {
t.connect(host, username, password);
t.sendMessage(msg, msg.getAllRecipients());
} catch(Throwable throwable){
LOG.error(throwable.getMessage());
}finally {
t.close();
}
}

}

消息已发送,但它显示的唯一内容是带有图像的“页脚”,而不是消息!

你能看出代码有什么问题吗?

提前致谢!

最佳答案

图像与文本部分并不真正相关。尝试反转多部分:

multipart/alternative
text/plain
multipart/related
text/html
image/gif

关于JavaMail MimeMultiPart 不发送全部内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20067358/

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