【问题标题】:Sendgrid failing to attach multiple files and showing no error codeSendgrid 无法附加多个文件并且没有显示错误代码
【发布时间】:2020-04-14 14:33:38
【问题描述】:

我正在使用带有 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);
                }
            }
        }
    }

}

Maven 依赖

        <dependency>
            <groupId>com.sendgrid</groupId>
            <artifactId>sendgrid-java</artifactId>
            <version>4.3.0</version>                
        </dependency>

【问题讨论】:

    标签: java sendgrid email-attachments


    【解决方案1】:

    在附加时使用 try with resource for 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);
                    }
                }
            }
        }
     }
    

    【讨论】:

      猜你喜欢
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 2020-08-23
      • 1970-01-01
      相关资源
      最近更新 更多