AWS SES on EC2- 找不到文件

AWS SES on EC2- file not found

提问人:Heidi 提问时间:4/21/2023 最后编辑:Heidi 更新时间:4/21/2023 访问量:39

问:

问题:找不到文件/目录

我编写了一个代码,使用 Java SpringBoot 通过 AWS SES 发送带有附件的电子邮件。 当我在本地环境中时,我可以发送电子邮件(使用 C:\Users ~ location) 但是当我在服务器环境中时,我无法使用我的代码。

我的代码有什么问题? 我想附加带有 url 的文件。

//Dto

private List<String> emailList = new ArrayList<>();
private List<String> ccList = new ArrayList<>();
private List<String> bccList = new ArrayList<>();
private List<String> attachList = new ArrayList<>();
private String title;
private String content;
//Service

public class EmailAttachService {
    @Autowired
    AmazonSimpleEmailService ses;

    public void sendEmailwithAttachments(EmailAttachSendReqDto reqDto) throws MessagingException, IOException {
        String sender = reqDto.getSenderEmail();
        List<String> recipient = reqDto.getEmailList();
        List<String> cc = reqDto.getCcList();
        List<String> bcc = reqDto.getBccList();
        String subject = reqDto.getTitle();
        List<String> attach = reqDto.getAttachList();
        String content = reqDto.getContent();

        Session session = Session.getDefaultInstance(new Properties());

        MimeMessage message = new MimeMessage(session);
        message.setSubject(subject, "UTF-8");
        message.setFrom(new InternetAddress(sender));

        message.addRecipients(Message.RecipientType.TO,InternetAddress.parse(String.join(",", recipient)));
        message.addRecipients(Message.RecipientType.CC,InternetAddress.parse(String.join(",", cc)));
        message.addRecipients(Message.RecipientType.BCC,InternetAddress.parse(String.join(",", bcc)));

        MimeMultipart msg_body = new MimeMultipart("alternative");

        MimeBodyPart wrap = new MimeBodyPart();

        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(content, "text/html; charset=UTF-8");

        msg_body.addBodyPart(htmlPart);

        wrap.setContent(msg_body);
        MimeMultipart msg = new MimeMultipart("mixed");

        message.setContent(msg);

        msg.addBodyPart(wrap);

        if(attach != null){
            for (String at : attach){
                MimeBodyPart att = new MimeBodyPart();
                DataSource fds = new FileDataSource(at);
                att.setDataHandler(new DataHandler(fds));
                att.setFileName(fds.getName());

                msg.addBodyPart(att);
            }
        }

        try {
            PrintStream out = System.out;
            message.writeTo(out);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            message.writeTo(outputStream);
            RawMessage rawMessage =
                    new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));

            SendRawEmailRequest rawEmailRequest =
                    new SendRawEmailRequest(rawMessage);

            SendRawEmailResult result = ses.sendRawEmail(rawEmailRequest);
            log.debug("Email sent to " + reqDto.getEmailList() + ", Message ID: " + result.getMessageId());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new EmailSendException(ExceptionState.EMAIL_SEND_FAIL,"Email send Fail");
        }
    }
}

//Config

@Bean
    public AmazonSimpleEmailService amazonSimpleEmailService() {
        final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
        final AWSStaticCredentialsProvider awsStaticCredentialProvider = new AWSStaticCredentialsProvider(basicAWSCredentials);

        return AmazonSimpleEmailServiceClientBuilder.standard()
                .withCredentials(awsStaticCredentialProvider)
                .withRegion("ap-northeast-2").build();
    }
java amazon-web-services 电子邮件 jakarta-mail amazon-ses

评论

0赞 John Rotenstein 4/21/2023
哪一行产生了错误?
0赞 Heidi 4/21/2023
''' if(attach != null){ for (String at : attach){ MimeBodyPart att = new MimeBodyPart();数据源 fds = new FileDataSource(at);att.setDataHandler(新数据处理程序(fds));att.setFileName(fds.getName());msg.addBodyPart(att);} } ''' 我认为这个是
0赞 John Rotenstein 4/21/2023
这不是一条线。这是一个循环中的几行。您应该添加一些调试语句,以帮助准确识别导致错误的行。它将是一个尝试访问文件系统的人。我假设问题出在数组中的值之一。当它失败时,看看他们有什么价值观可能会有所帮助。forattachlog.debug(at)fds

答: 暂无答案