SendGrid メール API、メール添付ファイルの送信 質問する

SendGrid メール API、メール添付ファイルの送信 質問する

Sendgrid を使用して電子メールを送信していますが、次のコードを使用すると正常に動作しますが、添付ファイルがありません。

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

}

しかし、私が必要としているのは添付ファイルを送信することなので、githubソースとWebドキュメントAPIを検索しましたが、何らかの理由でJavadocはありませんでしたが、例はありましたGitHub 送信グリッドそれで、それが機能するまで試しています。いくつかの例外と応答コードを絞り込みました。最初は許可されていない禁止コードを取得していましたが、応答202に改善されました。これは有効でキューに入れられていることを意味します(こちらをご確認ください) とにかく、ここに私のコードがあります。このコードでは、電子メールと添付ファイルは送信されますが、添付ファイルを開くとサイズがゼロになり、ファイルを開いたりプレビューしたりできないというメッセージが表示されます。

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

    }

ご参考までに: sendgridのコンソールから生成されたAPIキーを使用します

ベストアンサー1

コードを実行すると、NetBeansのログに次のメッセージが表示されました。

 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}

この問題を解決するには、Commons Apacheコーデックを使用して添付ファイルをエンコードします。コモンズコーデック 1.8.jarencodeAsStringパッケージからのメソッド

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

応答ではコンテンツの長さも0として返されました出来た

おすすめ記事