Google app engine 通过Sendgrid以附件形式发送PDF

Google app engine 通过Sendgrid以附件形式发送PDF,google-app-engine,sendgrid,sendgrid-api-v3,Google App Engine,Sendgrid,Sendgrid Api V3,我正在使用Sendgrid通过GAE上的应用程序发送电子邮件。它的工作很好,但我也希望能够发送PDF作为附件。 我的项目中没有使用Sendgrid.jar文件。我刚刚使用了Sendgrid.java。这个类没有我可以用来添加附件的方法。有人能帮我吗?我个人认为,直接按照中的描述构造JSON请求体比使用Sendgrid的库更容易。我只在自己构造JSON数据后使用Sendgrid库发送请求 构造JSON数据时,您需要至少指定一个文件名和内容(即PDF文件)。确保在将PDF文件添加到JASON数据之前

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

我个人认为,直接按照中的描述构造JSON请求体比使用Sendgrid的库更容易。我只在自己构造JSON数据后使用Sendgrid库发送请求

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


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

下面是一个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方法以ByteArrayOutputStream的形式返回PDF

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;
    }
}
定义上述静态方法,并根据程序需要使用相关参数进行调用