2016-07-26 15 views
3

Verwenden Sie sendgrid zum Senden von E-Mails und es funktioniert gut mit dem folgenden Code , aber es ohne Anhang.SendGrid E-Mail-API senden E-Mail-Anhang

package sendgrid; 

import com.sendgrid.Content; 
import com.sendgrid.Email; 
import com.sendgrid.Mail; 
import com.sendgrid.Method; 
import com.sendgrid.Request; 
import com.sendgrid.Response; 
import com.sendgrid.SendGrid; 
import java.io.IOException; 

public class SendEmail { 
    public static void main(String[] args) throws IOException { 
    Email from = new Email("[email protected]"); 
    String subject = "Hello World from the SendGrid Java Library!"; 

    Email to = new Email("[email protected]"); 
    Content content = new Content("text/plain", "Hello, Email!"); 
    Mail mail = new Mail(from, subject, to, content); 

    SendGrid sg = new SendGrid("SG.rIEh84OgQBybYEJcOMie1wd.AZqqdWNYXbOqTarUJcG-iSg0UtHJtCto4oe6tVzn6es"); 
    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; 
    } 
    } 

} 

Aber ich brauche, was Anlagen sendet mit ihm so, ich suchte Github Quelle und die Web-Dokumentation API, und aus irgendeinem Grunde gibt es keine javadocs, aber es gab ein Beispiel GitHub sendgrid so versuchen, bis es funktioniert, i eingegrenzt einige Ausnahmen und Antwort-Code, zuerst wurde ich nicht autorisierte das verbotene und es wurde besser auf Antwort 202, bedeutet gültig und in der Warteschlange (check here) jeder Weg hier ist mein Code, der Dosis eine E-Mail senden und mit Anhängen, aber wenn Sie die öffnen Anhängen seine Nullgröße und sagt, kann die Datei nicht öffnen oder in der Vorschau anzeigen!

package sendgrid; 

    import com.sendgrid.Attachments; 
    import com.sendgrid.Content; 
    import com.sendgrid.Email; 
    import com.sendgrid.Mail; 
    import com.sendgrid.MailSettings; 
    import com.sendgrid.Method; 
    import com.sendgrid.Request; 
    import com.sendgrid.SendGrid; 
    import com.sendgrid.Setting; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.IOException; 


    public class SendEmailAttachmentV2 { 

     public static void main(String[] args) throws IOException { 
      sendmail(); 
     } 

     // Fully populated Mail object 
     public static void sendmail() throws IOException { 

      com.sendgrid.Response response1; 

      Email from = new Email("[email protected]"); 
      String subject = "Hello World from the SendGrid Java Library!"; 

      Email to = new Email("[email protected]"); 
      Content content = new Content("text/plain", "Hello, Email!"); 
      Mail mail = new Mail(from, subject, to, content); 

      File file = new File("C:\\x.png"); 
      byte[] fileData = null; 
      try { 
       fileData = org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(file)); 
      } catch (IOException ex) { 
      } 

      Attachments attachments3 = new Attachments();    
      attachments3.setContent(new String(fileData, 0, (int) file.length(), "UTF-8")); 
      attachments3.setType("image/png");//"application/pdf" 
      attachments3.setFilename("x.png"); 
      attachments3.setDisposition("attachment"); 
      attachments3.setContentId("Banner"); 
      mail.addAttachments(attachments3); 


      MailSettings mailSettings = new MailSettings(); 
      Setting sandBoxMode = new Setting(); 
      sandBoxMode.setEnable(true); 
      mailSettings.setSandboxMode(sandBoxMode); 

      SendGrid sg = new SendGrid("SG.1Hg78VK0TJ6kexUnByZUYg.LAa5A4GufssZ9lpPQdV6PcZCY6SZ9Xq6LvqfMRG0wesKw"); 
      Request request1 = new Request(); 
      try { 
       request1.method = Method.POST; 
       request1.endpoint = "mail/send"; 

       request1.body = mail.build(); 

       response1 = sg.api(request1); 
       System.out.println(response1.statusCode); 
       System.out.println(response1.body); 
       System.out.println(response1.headers); 

      } catch (IOException ex) { 
       System.out.println(ex); 
      } 
     } 

    } 

FYI: Ihre generierten API-Schlüssel verwenden, die von der Konsole von sendgrid

+0

Sie sollen die Schlüssel aus dem Code Beispiel entfernen – IcedDante

+0

ja dank @IcedDante i zufällig eine nicht realen gesetzt habe ich gelegentliche re es irgendeine Art und Weise – shareef

Antwort

6

erzeugt wird, wenn ich den Code ausgeführt i in Protokollen in Netbeans die folgende Meldung bekam

202 

{X-Frame-Options=DENY, Server=nginx, Connection=keep-alive, 
X-Message-Id=vqVw2RtUShSVQ_ymVEVqaw, Content-Length=0, Date=Tue, 26 
Jul 2016 20:05:54 GMT, Content-Type=text/plain; charset=utf-8} 

Der Trick Um das Problem zu lösen, wird der Anhang mit commons Apache-Codec codiert commons-codec-1.8.jarand its encodeAsString` Methode aus Paket

org.apache.commons.codec.binary.Base64

Attachments attachments3 = new Attachments(); 
     Base64 x = new Base64(); 
     String imageDataString = x.encodeAsString(fileData); 
     attachments3.setContent(imageDataString); 
     attachments3.setType("image/png");//"application/pdf" 
     attachments3.setFilename("x.png"); 
     attachments3.setDisposition("attachment"); 
     attachments3.setContentId("Banner"); 
     mail.addAttachments(attachments3); 

Auch der Content-Length wurde retruned als 0 in Reaktion es gearbeitet.

0

Auf diese Weise können Sie Anhänge mithilfe der SendGrid-API senden.

Mail mail = createEmail(); 
    Attachments attachments = new Attachments(); 
    Base64 x = new Base64(); 
    String encodedString = x.encodeAsString(loadPdfFromClasspath()); 
    attachments.setContent(encodedString); 
    attachments.setDisposition("attachment"); 
    attachments.setFilename("xyx.pdf"); 
    attachments.setType("application/pdf"); 
    mail.addAttachments(attachments); 


try { 
     request.method = com.sendgrid.Method.POST; 
     request.endpoint = "mail/send"; 
     request.body = mail.build(); 
     // Uncomment once connectivity with sendgrid is resolved 
     Response response = sg.api(request); 

}catch (IOException ex) { 
     throw ex; 
    }