gpt4 book ai didi

java - 使用 JavaMail 创建多部分

转载 作者:行者123 更新时间:2023-11-30 08:04:04 25 4
gpt4 key购买 nike

我想创建一个 MimeMessage,它必须有两个部分,如下图所示(Part_0 和 Part_2)

example s/mime

我正在尝试使用下面的代码生成 s/mime

public static void main(String[] a) throws Exception {

// create some properties and get the default Session
Properties props = new Properties();
// props.put("mail.smtp.host", host);

Session session = Session.getInstance(props, null);
// session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
try {

msg.addHeader("Content-Type", "multipart/signed; protocol=\"application/pkcs7-signature;");

msg.setFrom(new InternetAddress(FROM_EMAIL));
InternetAddress[] address = {new InternetAddress(
TO_EMAIL)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test Subject");
msg.setSentDate(new Date());

// create and fill the first message part
MimeBodyPart bodyPart1 = new MimeBodyPart();

bodyPart1.setHeader("Content-Type", "text/html; charset=utf-8");
bodyPart1.setContent("<b>Hello World</b>", "text/html");

Multipart multiPart = new MimeMultipart();
multiPart.addBodyPart(bodyPart1, 0);

try (OutputStream str = Files.newOutputStream(Paths
.get(UNSIGNED_MIME))) {
bodyPart1.writeTo(str);
}

signMime();

MimeBodyPart attachPart = new MimeBodyPart();

String filename = SIGNED_VALUE;

DataSource source = new FileDataSource(filename);

attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName("smime.p7s");
attachPart.addHeader("Content-Type", "application/pkcs7-signature; name=smime.p7s;");
attachPart.addHeader("Content-Transfer-Encoding", "base64");
attachPart.addHeader("Content-Description", "S/MIME Cryptographic Signature");

multiPart.addBodyPart(attachPart);

msg.setContent(multiPart, "multipart/signed; protocol=\"application/pkcs7-signature\"; ");

msg.saveChanges();
try (OutputStream str = Files.newOutputStream(Paths
.get(SIGNED_MIME))) {
msg.writeTo(str);
}

} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}

我使用了两个 MimeBodyPart 但是我总是得到一个 Part_0 并生成如下所示的 eml 文件。

enter image description here

最佳答案

我没有试过编译它,但你想要的是这样的东西。内部多部分是外部多部分的主体部分。

    msg.setFrom(new InternetAddress(FROM_EMAIL));
InternetAddress[] address = {new InternetAddress(
TO_EMAIL)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test Subject");
msg.setSentDate(new Date());

MultipartSigned multiSigned = new MultipartSigned();

// create and fill the first message part
MimeBodyPart bodyPart1 = new MimeBodyPart();

bodyPart1.setText("<b>Hello World</b>", "utf-8", "html");

Multipart multiPart = new MimeMultipart();
multiPart.addBodyPart(bodyPart1);

// add other content to the inner multipart here

MimeBodyPart body = new MimeBodyPart();
body.setContent(multiPart);
multiSigned.addBodyPart(body);

try (OutputStream str = Files.newOutputStream(Paths
.get(UNSIGNED_MIME))) {
body.writeTo(str);
}

signMime();

MimeBodyPart attachPart = new MimeBodyPart();

String filename = SIGNED_VALUE;

attachPart.attachFile(filename,
"application/pkcs7-signature; name=smime.p7s", "base64");
attachPart.setFileName("smime.p7s");
attachPart.addHeader("Content-Description",
"S/MIME Cryptographic Signature");

multiSigned.addBodyPart(attachPart);
msg.setContent(multiSigned);

msg.saveChanges();

你需要这个:

public class MultipartSigned extends MimeMultipart {
public MultipartSigned() {
super("signed");
ContentType ct = new ContentType(contentType);
ct.setParameter("protocol", "application/pkcs7-signature");
contentType = ct.toString();
}
}

您可以通过将更多功能移到 MultipartSigned 类中来使它更简洁。

关于java - 使用 JavaMail 创建多部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35941879/

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