【问题标题】:Adding attachment from local filepath in sendmail在 sendmail 中从本地文件路径添加附件
【发布时间】:2018-06-23 22:32:04
【问题描述】:

我正在尝试在 sendgrid 中的路径 /Users/david/Desktop/screenshot5.png 处附加一个本地文件。

    Mail mail = new Mail(from, subject, to, message);

    // add an attachment
    Attachments attachments = new Attachments();
    Base64 x = new Base64();
    String encodedString = x.encodeAsString("/Users/david/Desktop/screenshot5.png");
    attachments.setContent(encodedString);
    attachments.setDisposition("attachment");
    attachments.setFilename("screenshot5.png");
    attachments.setType("image/png");

    mail.addAttachments(attachments);

这样做的正确方法是什么?

【问题讨论】:

    标签: java sendgrid


    【解决方案1】:

    您添加了文件路径。
    您应该添加文件内容:

    Mail mail = new Mail(from, subject, to, message);
    
    // add an attachment
    Attachments attachments = new Attachments();
    File file = new File("/Users/david/Desktop/screenshot5.png");
    byte[] fileContent = Files.readAllBytes(file.toPath());
    String encodedString = Base64.getEncoder().encodeToString(fileContent);
    attachments.setContent(encodedString);
    attachments.setDisposition("attachment");
    attachments.setFilename("screenshot5.png");
    attachments.setType("image/png");
    
    mail.addAttachments(attachments);
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-23
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多