2016-09-30 50 views
1

我跟着Google's information通過SendGrid從App Engine發送電子郵件。這是工作正常使用Java library for SendGrid和提供的示例代碼:如何通過SendGrid(使用Java)向Google App Engine發送的電子郵件添加附件?

import packageName.Sendgrid; 

Sendgrid mail = new Sendgrid("<sendgrid_username>","<sendgrid_password>"); 
mail.setTo("[email protected]") 
    .setFrom("[email protected]") 
    .setSubject("Subject goes here") 
    .setText("Hello World!") 
mail.send(); 

但現在我需要附加的文件。如何才能做到這一點?我在sendgrid-google-java library中找不到addAttachment功能或類似的東西。

回答

3

我只是在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("[email protected]"); 
    email.setFrom("[email protected]"); 
    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; 
    } 
+0

開箱即用?那麼[sendgrid-google-java庫](https://github.com/sendgrid/sendgrid-google-java)的目的是什麼?我會嘗試並報告回來。 – yonojoy

+1

直接使用普通的SendGrid Java API是正確的建議,謝謝。但是,示例代碼並不適用於我(可能是因爲他們切換了API版本?)。我將添加工作代碼作爲答案。 – yonojoy

+0

如果你檢查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("[email protected]"); 
    mail.setFrom(from); 

    String subject = "Hello World from the SendGrid Java Library"; 
    pers.setSubject(subject); 

    Email to = new Email("[email protected]"); 
    pers.addTo(to); 
    Email cc = new Email("[email protected]"); 
    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。

+1

Base64編碼附件內容的一種有用方法是Apache Commons Codec 和Apache Commons IO 以這種方式,'Base64 x = new Base64(); attachments.setContent(x.encodeAsString(IOUtils.toByteArray((InputStream)content)));' – Neill

相關問題