为了账号安全,请及时绑定邮箱和手机立即绑定

SendGrid 发送了附件的新副本而不是本地文件

SendGrid 发送了附件的新副本而不是本地文件

胡说叔叔 2021-12-18 15:40:17
我想使用 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;    }}
查看完整描述

1 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

我编写了以下测试类并成功发送/接收了一封带有 excel 附件内容的电子邮件。除了附件对象中设置的类型之外,代码与您的相同。在这里粘贴整个类,以便您也可以检查导入。我正在使用 sendgrid-java 4.3.0 版。在测试此代码之前,请确保移动到 4.3.0 版本。我还在下面截断了我的 api-key,所以用你的 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("test@example.com");

    String subject = "Hello World from the SendGrid Java Library!";


    Email to = new Email("myemail@example.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.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();

    }

  }

}

希望这可以帮助!


查看完整回答
反对 回复 2021-12-18
  • 1 回答
  • 0 关注
  • 211 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信