【问题标题】:Send a PDF as an attachment through Sendgrid通过 Sendgrid 将 PDF 作为附件发送
【发布时间】:2017-03-20 09:53:06
【问题描述】:

我正在使用 Sendgrid 通过 GAE 上的应用程序发送电子邮件。它工作正常,但我也希望能够将 PDF 作为附件发送。 我没有在我的项目中使用 Sendgrid.jar 文件。我刚刚使用了 Sendgrid.java。而且这个类没有我可以添加附件的方法。有人可以帮我吗?

【问题讨论】:

    标签: google-app-engine sendgrid sendgrid-api-v3


    【解决方案1】:

    这是一个 servlet 的代码,它通过 Sendgrid 发送带有 PDF 作为附件的邮件:

        @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        ....
    
        ByteArrayOutputStream os = null;
        try {
            PDFGenerator pdfGenerator = new PDFGenerator(invoiceOut);
            os = pdfGenerator.getPDFOutputStream();
        } catch (Exception e) {
            ....
        }
    
        SendGrid sendgrid = new SendGrid(Constants.SENDGRID_API_KEY);
        SendGrid.Email email = new SendGrid.Email();
        email.addTo(....);
        email.setFrom(....);
        email.setFromName(....);
        email.setSubject(....);
        email.setHtml("......");
    
        ByteBuffer buf = null;
        if (os == null) {
            //error...
        } else {
            buf = ByteBuffer.wrap(os.toByteArray());
        }
    
        InputStream attachmentDataStream = new ByteArrayInputStream(buf.array());
    
        try {
            email.addAttachment("xxxxx.pdf", attachmentDataStream);
            SendGrid.Response response = sendgrid.send(email);
    
        } catch (IOException e) {
            ....
            throw new RuntimeException(e);
        } catch (SendGridException e) {
            ....
            throw new RuntimeException(e);
        }
    
    }
    

    PDFGenerator 是我的类之一,其中 getPDFOutputStream 方法将 PDF 作为 ByteArrayOutputStream 返回。

    【讨论】:

      【解决方案2】:
      public static boolean sendEmail(String fromMail, String title, String toMail, String message) throws IOException {
          Email from = new Email(fromMail);
          String subject = title;
          Email to = new Email(toMail);
      
          Content content = new Content("text/html", message);
          Mail mail = new Mail(from, subject, to, content);
      
          Path file = Paths.get("file path");
          Attachments attachments = new Attachments();
          attachments.setFilename(file.getFileName().toString());
          attachments.setType("application/pdf");
          attachments.setDisposition("attachment");
      
          byte[] attachmentContentBytes = Files.readAllBytes(file);
          String attachmentContent = Base64.getMimeEncoder().encodeToString(attachmentContentBytes);
          String s = Base64.getEncoder().encodeToString(attachmentContentBytes);
          attachments.setContent(s);
          mail.addAttachments(attachments);
      
          SendGrid sg = new SendGrid("sendgrid api key");
          Request request = new Request();
      
          request.setMethod(Method.POST);
          request.setEndpoint("mail/send");
          request.setBody(mail.build());
          Response response = sg.api(request);
      
          if (response != null) {
              return true;
          } else {
              return false;
          }
      }
      

      定义上述静态方法并根据您的程序需要使用相关参数进行调用。

      【讨论】:

        【解决方案3】:

        我个人发现直接构造 JSON 请求正文(如 API docs 中所述)比使用 Sendgrid 的库更容易。在我自己构建 JSON 数据后,我只使用 Sendgrid 库发送请求。

        在构建 JSON 数据时,您至少需要指定文件名和内容(即 PDF 文件)。确保在将 PDF 文件添加到 JASON 数据之前对其进行 Base64 编码。

        我会包含一些代码,但我使用的是 Python 而不是 Java,所以不确定这是否会有所帮助。

        【讨论】:

          猜你喜欢
          • 2015-06-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多