java发送带图片的邮件代码

图片目录:

在这里插入图片描述
实现代码:

/**
     * 发送带附件的mail
     *
     * @param subject         邮件标题
     * @param to              收件人列表
     * @param cc              抄送人列表
     * @param bodyText        邮件内容
     * @param attachmentPaths 附件地址列表
     * @throws Exception
     */
    public void send(String subject, List<String> to, List<String> cc, String bodyText, String[] attachmentPaths)
            throws Exception {
        ExchangeService service = getExchangeService();

        EmailMessage msg = new EmailMessage(service);
        msg.setSubject(subject);
        MessageBody body = MessageBody.getMessageBodyFromText(bodyText);
        body.setBodyType(BodyType.HTML);
        msg.setBody(body);

        InputStream image001 = this.getClass().getResourceAsStream("/mail/image001(12-10-14-54-57).png");
        msg.getAttachments().addFileAttachment("image001(12-10-14-54-57).png", image001);
        //msg.getAttachments().getItems().get(0).setIsInline(true);
        msg.getAttachments().getItems().get(0).setContentId("image001(12-10-14-54-57).png");
        InputStream image005 = this.getClass().getResourceAsStream("/mail/image005(12-10-14-54-57).jpg");
        msg.getAttachments().addFileAttachment("image005(12-10-14-54-57).jpg", image005);
        //msg.getAttachments().getItems().get(1).setIsInline(true);
        msg.getAttachments().getItems().get(1).setContentId("image005(12-10-14-54-57).jpg");

        for (String toPerson : to) {
            msg.getToRecipients().add(toPerson);
        }
        if (cc != null) {
            for (String ccPerson : cc) {
                msg.getCcRecipients().add(ccPerson);
            }
        }
        if (attachmentPaths != null) {
            for (String attachmentPath : attachmentPaths) {
                msg.getAttachments().addFileAttachment(attachmentPath);
            }
        }
        msg.send();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值