EC2 上的 AWS SES- 找不到文件

问题描述 投票:0回答:0

问题:找不到文件/目录 我编写了一个代码,使用 Java SpringBoot 通过 AWS SES 发送带有附件的电子邮件。 我可以在本地环境中发送电子邮件(使用 C:\Users ~ 位置) 但是当我在服务器环境中时我不能使用我的代码。

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

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;
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");
        }
    }
}

@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 email jakarta-mail amazon-ses
© www.soinside.com 2019 - 2024. All rights reserved.