提问人:Heidi 提问时间:4/21/2023 最后编辑:Heidi 更新时间:4/21/2023 访问量:39
AWS SES on EC2- 找不到文件
AWS SES on EC2- file not found
问:
问题:找不到文件/目录
我编写了一个代码,使用 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();
}
答: 暂无答案
评论
for
attach
log.debug(at)
fds