zoukankan      html  css  js  c++  java
  • javamail邮件中插入图片

    转载 http://duanmumu.blog.163.com/blog/static/1911133502012715104016481/

    // TODO Auto-generated method stub

    Properties props =newProperties();
    Session session =Session.getInstance(props);
    Message message =newMimeMessage(session);

    // message.setFrom(new InternetAddress("duan_lonshan@126.com"));

    Multipart msgmultipart =newMimeMultipart("mixed");//mixed表示混合性,这里因为有文本,附件,所以是混合的。
    message.setContent(msgmultipart);

    //添加附件图片
    BodyPart picpart =newMimeBodyPart();
    msgmultipart.addBodyPart(picpart);
    DataSource ds1 =newFileDataSource("e:\login.jpg");
    DataHandler dh1 =newDataHandler(ds1);
    picpart.setDataHandler(dh1);
    picpart.setFileName("login.jpg");
    //添加附件文件
    BodyPart mg =newMimeBodyPart();
    msgmultipart.addBodyPart(mg);
    DataSource ds2 =newFileDataSource("e:\test.xls");
    DataHandler dh2 =newDataHandler(ds2);
    mg.setDataHandler(dh2);
    mg.setFileName("mess.xls");
    //添加文本内容
    BodyPart msgpart =newMimeBodyPart();
    msgmultipart.addBodyPart(msgpart);
    Multipart bodyMultipart =newMimeMultipart("related");//这里的图片和文本是在一起显示的所以他们是关系型的。
    msgpart.setContent(bodyMultipart);

    BodyPart jpgpart =newMimeBodyPart();
    BodyPart htmlpart =newMimeBodyPart();
    bodyMultipart.addBodyPart(jpgpart);
    bodyMultipart.addBodyPart(htmlpart);

    //文本中添加图片
    DataSource ds =newFileDataSource("e:\background.jpg");
    DataHandler jpgdh =newDataHandler(ds);
    jpgpart.setDataHandler(jpgdh);
    jpgpart.setHeader("Content-Location","http://www.test.com/bg.jpg");//这里的地址就是跟img标签中的地址相同。然后就可以在文本中直接显示出来图片。这里的Content-Location固定的,

    htmlpart.setContent("this is the first email <img src='http://www.test.com/bg.jpg'>","text/html;charset=gb2312");
    message.saveChanges();//保存数据
    OutputStream os =newFileOutputStream("e:\dome3.eml");//写出到的文件
    message.writeTo(os);



  • 相关阅读:
    git
    java网络
    配置本地git服务器(gitblit win7)
    atom 插件安装【转载】
    javaIo
    如何在eclipse中设置断点并调试程序
    如何将工程推到github上
    git操作记录
    编码
    node升级7.0以上版本使用gulp时报错
  • 原文地址:https://www.cnblogs.com/freeman-rain/p/3263938.html
Copyright © 2011-2022 走看看