0

I want to send dynamically generated excel file programmatically using sendgrid function. Below java program is sending the excel file as an attachment successfully, But It is sending fresh copy (newly generated) of excel file not a file which I like to send. Please let me know Where do I am making mistake ?

public static void main(String[] args) throws IOException, InvalidFormatException {

    final String path = "C:\\Users\\src\\testData\\TestData.xlsx";

    byte[] bFile = Files.readAllBytes(new File(path).toPath());

    Attachments attachments3 = new Attachments();
    Base64 x = new Base64();
    String imageDataString = x.encodeAsString(bFile);
    attachments3.setContent(imageDataString);
    attachments3.setType("xlxs");// "application/pdf"
    attachments3.setFilename("TestData.xlsx");
    attachments3.setDisposition("attachment");
    attachments3.setContentId("Banner");

    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);
    mail.addAttachments(attachments3);

    SendGrid sg = new SendGrid("SG.EJLRKZEvE");
    Request request = new Request();
    try {

        request.setMethod(Method.POST);
        request.setEndpoint("mail/send");
        request.setBody(mail.build());

        Response response = sg.api(request);
        System.out.println(response.getStatusCode());
        System.out.println(response.getBody());
        System.out.println(response.getHeaders());

    } catch (IOException ex) {
        throw ex;
    }
}
3
  • did you check the similar question at stackoverflow.com/questions/38599079/…? Also, check your type for attachment. xlsx is mis-typed. Usually for excel spreadsheet 2007 and onwards, content type used is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet". Trying correcting that. Commented Oct 30, 2018 at 3:06
  • Thanks for reply , Yes, I do checked stackoverflow.com/questions/38599079/… . I think, in my case respective attachment is not been sent. And thanks for correcting on contain type
    – Rama
    Commented Oct 30, 2018 at 4:07
  • content type is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" Not solved my problem Still It is not sending the Local excel file. @moonlighter
    – Rama
    Commented Oct 30, 2018 at 8:58

1 Answer 1

1

I wrote the following test class and successfully sent/received an email with an excel attachment with content. The code is same as yours, other than the type set in the attachment object. Pasting the whole class here so that you can check the imports too. I am using sendgrid-java version 4.3.0. Make sure to move to the 4.3.0 version before you test this code. I have also truncated my api-key below so replace it with your api-key.

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import org.apache.commons.codec.binary.Base64;

import com.sendgrid.Attachments;
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;

public class TestSGEmail {
  public static void main(String args[]) {

    final String path = "/Users/macuser/testxls.xlsx";

    byte[] bFile = null;
    try {
      bFile = Files.readAllBytes(new File(path).toPath());
    } catch (IOException e) {
      e.printStackTrace();
    }

    Attachments attachments3 = new Attachments();
    Base64 x = new Base64();
    String imageDataString = x.encodeAsString(bFile);
    attachments3.setContent(imageDataString);
    attachments3.setType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    attachments3.setFilename("TestData.xlsx");
    attachments3.setDisposition("attachment");
    attachments3.setContentId("Banner");

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

    mail.addAttachments(attachments3);

    SendGrid sg = new SendGrid("SG.tGX184I");

    Request request = new Request();
    try {
      request.setMethod(Method.POST);
      request.setEndpoint("mail/send");
      request.setBody(mail.build());

      Response response = sg.api(request);
      System.out.println(response.getStatusCode());
      System.out.println(response.getBody());
      System.out.println(response.getHeaders());

    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}

Hope this helps!

2
  • Thanks a Lot @moonlighter , This is big help for me
    – Rama
    Commented Oct 31, 2018 at 3:25
  • glad to help! mark the answer as accepted when you get a chance. It helps anybody looking for help with a similar issue. cheers! Commented Oct 31, 2018 at 3:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.