我想使用 sendgrid 函数以编程方式发送动态生成的 excel 文件。下面的java程序成功地将excel文件作为附件发送,但它发送的是excel文件的新副本(新生成的),而不是我喜欢发送的文件。请让我知道我在哪里犯了错误?
public static void main(String[] args) 抛出 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("ranjit@test.com");
String subject = "Hello World from the SendGrid Java Library!";
Email to = new Email("sachin@test.com");
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;
}
}