如何实现发送邮件时可以携带附件原创
金蝶云社区-闪电旋风劈
闪电旋风劈
11人赞赏了该文章 2960次浏览 未经作者许可,禁止转载编辑于2022年07月05日 10:45:24

关键词:

流程服务 消息中心

一、需求

在某些场景下,使用苍穹消息平台接口发送邮件时,可能会有需要把单据上的附件作为邮件附件发送的场景。

二、思路与方案

实现方案

可在构造messageinfo时使用 :

kd.bos.workflow.engine.msg.info.MessageInfo.setAttachment(MessageAttachment attachment),传入附件信息。

三、实现过程

1.调用消息平台接口,构造messageinfo

@Override
       public void itemClick(ItemClickEvent e) {
                     // TODO Auto-generated method stub
 
              if(e.getItemKey().equals("kded_baritemap")) {
                     this.getView().setVisible(false,"attachmentpanel");
              }
              if(e.getItemKey().equals("kded_baritemap1")) {
                     List<Long> receivers =new ArrayList<>();
                     receivers.add(Long.valueOf(RequestContext.get().getUserId()));
                     MessageInfo message = new MessageInfo();
                     message.setType("nuclearwarn");//推送消息的类型   
                     message.setNotifyType("");
                     message.setTitle("核弹来袭!");
                     message.setUserIds(receivers);//接收用户
                     message.setSenderName("附件测试");
                     message.setTag("重要,必读");//业务标签
                     MessageAttachment attachment = buildattachment();
                     message.setAttachment(attachment);
                     message.setContent("骗你的");//详情消息描述
                     MessageCenterServiceHelper.sendMessage(message);//推送消息
              }
              if(e.getItemKey().equals("kded_baritemap")) {
              }
              
        }


2.构造附件信息对象MessageAttachment

反编译一下MessageAttachment的类,就会发现两个重要属性image.png


图1

构造附件对象

private MessageAttachment buildattachment() {
              // TODO Auto-generated method stub
              
              byte[] file = null;
              MessageAttachment aMessageAttachment =new MessageAttachment();
              List<byte[]> bytelist =new ArrayList<>();
              List<String> attachmentNames = new ArrayList<>();
        try {
               //将附件转换成byte数组
               file = urltobyte("/ierp/858574889865445376/202106/kded_wwxtest/kded_workflowtest/1168747785722277888/attachments/f8c5bfe68a59413bb2d4ab54cd4454dd/1.txt");
               bytelist.add(file);
               //附件名的list
               attachmentNames.add("1.txt");
              } catch (Exception e) {
                     log.error(e);
              }
        aMessageAttachment.setAttachments(bytelist);
        aMessageAttachment.setAttachmentNames(attachmentNames);
              return aMessageAttachment;   
       }


附件url转byte[]数组(方法很多,举例一种)

private static byte[] urltobyte(String path) throws Exception
          {
            try
           {
              path = URLDecoder.decode(path, "UTF-8");
              while (path.startsWith("//")) {
                path = path.replaceFirst("//", "/");
              }
            } catch (UnsupportedEncodingException e) {
              log.error(e);
            }
            FileService attachmentFileService = FileServiceFactory.getAttachmentFileService();
            if ((!path.contains(".")) || (!path.contains("/")) || (!attachmentFileService.exists(path))) {
              throw new NullPointerException("path参数错误!文件不存在 : " + path);
            }
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            attachmentFileService.download(path, outStream, null);
            byte[] file = outStream.toByteArray();
            return file;
          }


四、效果图image.png


图2

 

五、开发环境版本

COSMICV4.0.014.0

六、注意事项

本文中附件url为苍穹单据附件的相对路径。

七、参考资料

【开发平台】指导手册

学习成长中心

 


赞 11