Sendgrid 无法附加多个文件且未显示错误代码

Sah*_*sar 5 java email-attachments sendgrid

我在 Java 中使用 sendgrid 电子邮件。我正在尝试发送多个附件,但不知何故电子邮件丢失了 1 或 2 个文件。有时,所有附件都被发送。我也检查了状态码。每次都是202。这是不可预测的行为。谁能指出我是否遗漏了什么。

import com.sendgrid.*;
import com.sendgrid.Attachments.Builder;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.List;

public class SendGridUtils {
    private InputStream fileContent;
    private List<File> files;

    public boolean sendEmail() {

        try {
            String sendTo="sento@gmail.com";
            String subject="some_subject";
            String body="some_body";
            String sendGridAPIKey = "some_key";
            String fromEmail = "from@gmail.com";
            String emailTitle = "some_title";
            files.add(new File("abc.png"));
            files.add(new File("xyz.png"));
            files.add(new File("pqr.png"));
            files.add(new File("rty.png"));
            files.add(new File("ghj.png"));

            Email from = new Email(fromEmail, emailTitle);
            Email to = new Email(sendTo);
            Content content = new Content("text/html", body);
            Mail mail = new Mail(from, subject, to, content);

            Personalization personalization = new Personalization();
            personalization.addTo(to);


            mail.addPersonalization(personalization);

            attachFiles(mail);

            SendGrid sg = new SendGrid(sendGridAPIKey);
            Request request = new Request();

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

            request.setBody(mail.build());
            Response response = sg.api(request);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return true;
        } finally {
            if (fileContent != null) {
                try {
                    fileContent.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (files != null) {
                for (File f : files) {
                    if (f.exists()) {
                        f.delete();
                    }
                }


            }
        }
    }

    private void attachFiles(Mail mail) throws IOException {
        if (files != null) {
            for (File f : files) {
                String fName = f.getName();
                if (!StringUtils.isBlank(fName)) {
                    this.fileContent = new FileInputStream(f);
                    Attachments attachments = new Builder(fName, fileContent).withDisposition("attachment")
                            .withType("image/png").build();
                    mail.addAttachments(attachments);
                }
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

Maven 依赖

        <dependency>
            <groupId>com.sendgrid</groupId>
            <artifactId>sendgrid-java</artifactId>
            <version>4.3.0</version>                
        </dependency>
Run Code Online (Sandbox Code Playgroud)

Sag*_*khe 0

附加时使用 try 与 fileContent InputStream 的资源。还添加错误日志记录器。

private void attachFiles(Mail mail) throws IOException {
    if (files != null) {
        for (File f : files) {
            String fName = f.getName();
            if (!StringUtils.isBlank(fName)) {
                try( this.fileContent = new FileInputStream(f) ) {
                    Attachments attachments = new Builder(fName, fileContent).withDisposition("attachment")
                        .withType("image/png").build();
                    mail.addAttachments(attachments);
                } catch (Exception e) {
                    logger.error("Failed to attach file: {}", fName, e);
                }
            }
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)