java mail 添加附件以及邮件中穿插图片方法

部分代码:

     /**
 * 创建邮件中的附件
 * @param filepath 附件的路径
 * @return         生成附件的对象
 * @throws Exception
 *
 * 测试: filepath = e:\测试\tomcat.png
 * e盘下的tomcat图片
 *
 */
public static MimeBodyPart createAttachmentImg(String filepath) throws Exception{
    /*创建一个表示附件的MimeBodyPart对象,并加入附件内容以及相应的信息*/
    MimeBodyPart attachPart=new MimeBodyPart();
    //FileDataSource用于读取文件数据,并返回代表数据的输入输出和数据的MIME类型
    FileDataSource fileDataSource=new FileDataSource(filepath);

    //DataHandler类用于封装FileDataSource对象,并为应用程序提供访问数据的接口
    attachPart.setDataHandler(new DataHandler(fileDataSource));

    //设置附件名称,MimeUtility.encodeText可以处理乱码问题
    // 加上这句话意味着是作为附件存在的,图片或是文件
    //   fileDataSource.getName() -->   tomcat.png
    attachPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
    // 如需要在相关的html代码中显示
    // 此方法添加则意味着是于html中的具备某种关系,表示附件图片的相对路径,图片直接出现在文本里
    attachPart.setHeader("Content-ID", fileDataSource.getName());

    return attachPart;
}

html 代码所做处理:

注意src 写法 表示为图片的相对路径

 <img src="cid:tomcat.png" alt="[tomcat logo]" />

完整




import cn.hutool.core.io.FileUtil;
import org.junit.Test;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class TestSendMail {
    @Test
    public void test() throws Exception {
        String send = "发送给谁" ;
        String userName = "发送者的用户名" ;
        String pwd = "密码" ;

        String subject = "java_mail 邮件发送测试" ;
        String img = "E:\\src\\测试数据\\jsp\\tomcat.png" ;
        String path = "E:\\src\\测试数据\\jsp\\index.jsp" ;

        Message message = null ;

        // 定义邮箱服务器配置
        Properties props=new Properties();
        // 163 邮件服务器地址
        props.put("mail.smtp.host", "smtp.163.com");
        // 163 邮件服务器端口
        props.put("mail.smtp.port", "25");
        // 163 邮件服务器认证属性
        props.put("mail.smtp.auth", true);

        //获取session 并传入用户名,密码
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, pwd);
            }
        });

        message=new MimeMessage(session);
        // 设置发送人地址
        message.setFrom(new InternetAddress(userName));
        //目标用户邮箱地址
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(send));
        // 设置发送日期
        message.setSentDate(new Date());
        //设置主题
        message.setSubject(subject);

        Multipart multipart=new MimeMultipart();

        //获取html 页面代码
        BodyPart bodyPart=new MimeBodyPart();
        StringBuffer sb=readLine1(path);
        bodyPart.setContent(sb.toString(), "text/html;charset=utf-8");

        multipart.addBodyPart(bodyPart);
        //图片附近
        multipart.addBodyPart(createAttachmentImg(img));
        //页面附件
        multipart.addBodyPart(createAttachment(path));
        message.setContent(multipart);
        // 发送
        Transport.send(message);

        System.out.println("完成-------------");

    }

    /**
     * 创建邮件中的附件
     * @param filepath 附件的路径
     * @return         生成附件的对象
     * @throws Exception
     */
    public static MimeBodyPart createAttachment(String filepath) throws Exception{
        //创建一个表示附件的MimeBodyPart对象,并加入附件内容以及相应的信息
        MimeBodyPart attachPart=new MimeBodyPart();

        //FileDataSource用于读取文件数据,并返回代表数据的输入输出和数据的MIME类型
        FileDataSource fileDataSource=new FileDataSource(filepath);

        //DataHandler类用于封装FileDataSource对象,并为应用程序提供访问数据的接口
        attachPart.setDataHandler(new DataHandler(fileDataSource));

        //设置附件名称,MimeUtility.encodeText可以处理乱码问题
        // 加上这句话意味着是作为附件存在的,图片或是文件
        attachPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));

        return attachPart;
    }
    /**
     * 创建邮件中的附件
     * @param filepath 附件的路径
     * @return         生成附件的对象
     * @throws Exception
     *
     * 测试: filepath = e:\测试\tomcat.png
     * e盘下的tomcat图片
     *
     */
    public static MimeBodyPart createAttachmentImg(String filepath) throws Exception{
        MimeBodyPart attachPart = createAttachment(filepath);
        // 如需要在相关的html代码中显示
        // 此方法添加则意味着是于html中的具备某种关系,表示附件图片的相对路径,图片直接出现在文本里
        attachPart.setHeader("Content-ID", new File(filepath).getName());
        return attachPart;
    }

    public StringBuffer readLine1(String path)throws  Exception{
        StringBuffer sb=new StringBuffer();
        List<String> list = FileUtil.readLines(path, "utf-8");
        for(String str : list){
            sb.append(str);
        }
        return  sb ;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值