JAVA MAIL-保存电子邮件时的内联图像。

时间:2017-01-25 18:21:42

标签: java javamail

如何在java邮件中一次性传递给CID以获取内嵌图像。尝试多种方式

邮件的HTML正文有2个CID,但如何传递给64baseencoding图像内容

<img src="cid:1"/><span style="font-size:14pt;font-family:'Times New Roman';"></span><div><span style="font-size:14pt;font-family:'Times New Roman';"><br /></span></div><div><span style="font-size:14pt;font-family:'Times New Roman';"><br /></span></div><div><span style="font-size:14pt;font-family:'Times New Roman';">dasdasd</span><br /><div style="display:none;">disC@Id458719@XMC@Editor</div></div><div><span style="font-size:14pt;font-family:'Times New Roman';"><br /></span></div><div><img src="cid:2**"/><span style="font-size:14pt;font-family:'Times New Roman';"><br /></span>
------=_Part_0_221999332.1485367631063
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-ID: <2> 

iVBORw0KGgoAAAANSUhEUgAAALwAAACXCAYAAACvIjFeAAAaSUlEQVR4Ae1dQWway5Y9Tr6MNP+h
+ZK9MZuwwhsjfaktzcBbmL8xWZhoBNngLIylkb0xWZhs8NPI9h8ZNibSBG+cjckiWDMyXhiPFHsT
vAhsaOkreBEzXwoZyXikYM0oPP1neC+pUTU0NA0Y7DiNY18U0l1Vt+69dep

如何在java邮件中一次性传递给CID以获取内嵌图像。尝试多种方式

DataSource fds = new FileDataSource(file); 
messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setDataHandler(new DataHandler(fds));
messageBodyPart1.setHeader("Content-ID","<"+i+">");
multipart=new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart1);
subLogger.info("Inside downloadEmail 1");

1 个答案:

答案 0 :(得分:-1)

您正在创建发送邮件,而不是保存您收到的邮件,对吗?

JavaMail FAQ有sample code for creating a multipart/related message

    Multipart multipart = new MimeMultipart("related");

    MimeBodyPart htmlPart = new MimeBodyPart();
    // messageBody contains html that references image
    // using something like <img src="cid:XXX"> where
    // "XXX" is an identifier that you make up to refer
    // to the image
    htmlPart.setText(messageBody, "utf-8", "html");
    multipart.addBodyPart(htmlPart);

    MimeBodyPart imgPart = new MimeBodyPart();
    // imageFile is the file containing the image
    imgPart.attachFile(imageFile);
    // or, if the image is in a byte array in memory, use
    // imgPart.setDataHandler(new DataHandler(
    //      new ByteArrayDataSource(bytes, "image/whatever")));

    // "XXX" below matches "XXX" above in html code
    imgPart.setContentID("<XXX>");
    multipart.addBodyPart(imgPart);

    message.setContent(multipart);
相关问题