如何通过SendGrid(Java)向Google App Engine发送的电子邮件添加附件?

时间:2016-09-30 07:17:04

标签: java google-app-engine sendgrid

我跟着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 - 函数或类似内容。

2 个答案:

答案 0 :(得分:4)

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

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;
    }

答案 1 :(得分: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。