【问题标题】:How to add an attachment to an email sent from Google App Engine via SendGrid (in Java)?如何在通过 SendGrid(Java)从 Google App Engine 发送的电子邮件中添加附件?
【发布时间】:2016-09-30 07:17:03
【问题描述】:

我关注了Google's information,通过 SendGrid 从 App Engine 发送了一封电子邮件。使用Java library for SendGrid 和提供的示例代码可以正常工作:

import packageName.Sendgrid;

Sendgrid mail = new Sendgrid("<sendgrid_username>","<sendgrid_password>");
mail.setTo("foo@bar.com")
    .setFrom("me@bar.com")
    .setSubject("Subject goes here")
    .setText("Hello World!")
mail.send();

但现在我需要附加一个文件。如何才能做到这一点?我在sendgrid-google-java library 中找不到addAttachment-function 或类似的东西。

【问题讨论】:

    标签: java google-app-engine sendgrid


    【解决方案1】:

    我只是在 appengine 上使用 SendGrid Java API,而不是特定的谷歌。 这是一个例子:

    import com.sendgrid.*;
    
    public class SendGridExample {
      public static void main(String[] args) {
        SendGrid sendgrid = new SendGrid("SENDGRID_APIKEY");
    
        SendGrid.Email email = new SendGrid.Email();
    
        email.addTo("test@sendgrid.com");
        email.setFrom("you@youremail.com");
        email.setSubject("Sending with SendGrid is Fun");
        email.setHtml("and easy to do anywhere, even with Java");
    
        SendGrid.Response response = sendgrid.send(email);
      }
    }
    

    https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/java.html

    您可以使用以下 3 个功能之一向此电子邮件对象添加附件:

        public Email addAttachment(String name, File file) throws IOException, FileNotFoundException {
            return this.addAttachment(name, new FileInputStream(file));
        }
    
        public Email addAttachment(String name, String file) throws IOException {
            return this.addAttachment(name, new ByteArrayInputStream(file.getBytes()));
        }
    
        public Email addAttachment(String name, InputStream file) throws IOException {
            this.attachments.put(name, file);
            return this;
        }
    

    【讨论】:

    • 开箱即用?那么sendgrid-google-java library 的目的是什么?我会尝试并报告。
    • 直接使用普通的 SendGrid Java API 是正确的建议,谢谢+。但是,示例代码对我不起作用(可能是因为他们切换了 API 版本?)。我将添加工作代码作为答案。
    • 如果你查看sendgrid-google-java的github页面,你会发现它已经2年没有更新了,所以我不会使用它。我的代码确实可能是一个较旧的 API。我很高兴你能成功!
    【解决方案2】:

    使用 sendgrid-java 从 GAE 发送电子邮件的工作方式为 suggested by Serge Hendrickx。 仅供参考,这是我最终使用的代码(最新的sendgrid-java.jar):

    public static Mail buildAttachmentEmailExample(String fileName, String base64EncodedFileContent, String contentType) throws IOException {
    
        Mail mail = new Mail();
        Personalization pers = new Personalization();
    
        Email from = new Email("test@example.com");
        mail.setFrom(from); 
    
        String subject = "Hello World from the SendGrid Java Library";
        pers.setSubject(subject);
    
        Email to = new Email("test@example.com");
        pers.addTo(to);
        Email cc = new Email("test2@example.com");
        pers.addCc(cc);
    
        Content content = new Content("text/plain", "some text here");
        mail.addContent(content);
    
        Attachments attachments = new Attachments();
        attachments.setContent(base64EncodedFileContent);
        attachments.setType(contentType);
        attachments.setFilename(fileName);
        mail.addAttachments(attachments);
    
        mail.addPersonalization(pers);
        return mail;
    }
    
    public static void sendMail(Mail mail) throws IOException {
        SendGrid sg = new SendGrid("SENDGRID_API_KEY");
    
        Request request = new Request();
        try {
          request.method = Method.POST;
          request.endpoint = "mail/send";
          request.body = mail.build();
          Response response = sg.api(request);
          System.out.println(response.statusCode);
          System.out.println(response.body);
          System.out.println(response.headers);
        } catch (IOException ex) {
          throw ex;
        }
    }
    
    public static void test() throws IOException {
    
        Mail mail = buildAttachmentEmailExample("test.txt", "WW91IGhhdmUgdG9vIG11Y2ggdGltZSE=", "text/plain");
        sendMail(mail);
    }
    

    代码基于https://github.com/sendgrid/sendgrid-java/blob/master/examples/helpers/mail/Example.java 中的示例,并利用更新的 SendGrid v3 API。

    【讨论】:

    • 对附件内容进行base64编码的一种有用方法是Apache Commons Codec commons.apache.org/proper/commons-codec>和Apache Commons IO commons.apache.org/proper/commons-io>,Base64 x = new Base64(); attachments.setContent(x.encodeAsString(IOUtils.toByteArray((InputStream) content)));
    猜你喜欢
    • 1970-01-01
    • 2012-03-24
    • 2010-10-27
    • 2016-10-27
    • 2020-02-26
    • 2012-04-10
    • 2016-10-19
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多