【问题标题】:SendGrid sent new copy of attachment instead of local fileSendGrid 发送附件的新副本而不是本地文件
【发布时间】:2019-04-03 01:05:01
【问题描述】:

我想使用 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;
    }
}

【问题讨论】:

  • 你在stackoverflow.com/questions/38599079/…查看过类似的问题吗?此外,请检查您的附件类型。 xlsx 输入错误。通常对于 excel 电子表格 2007 及更高版本,使用的内容类型是“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”。正在尝试纠正。
  • 感谢您的回复,是的,我确实检查了stackoverflow.com/questions/38599079/…。我认为,就我而言,没有发送相应的附件。并感谢您纠正包含类型
  • 内容类型是“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet” 没有解决我的问题仍然没有发送本地excel文件。 @moonlighter

标签: java sendgrid sendgrid-api-v3 sendgrid-api-v2


【解决方案1】:

我编写了以下测试类,并成功发送/接收了一封带有内容的 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();
    }
  }
}

希望这会有所帮助!

【讨论】:

  • 非常感谢@moonlighter,这对我帮助很大
  • 很高兴为您提供帮助!有机会时将答案标记为已接受。它可以帮助任何寻求类似问题帮助的人。干杯!
猜你喜欢
  • 2022-08-16
  • 2015-03-15
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 2016-08-05
  • 2016-02-08
  • 2012-07-10
  • 1970-01-01
相关资源
最近更新 更多