亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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("[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;    }}
查看完整描述

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("[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();

    }

  }

}

希望這可以幫助!


查看完整回答
反對 回復 2021-12-18
  • 1 回答
  • 0 關注
  • 232 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號