通过postgresql发送带有附件的电子邮件从内容获取数据

dgsult0t  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(398)

我想发送附件给所有的电子邮件收件人。
这是json。

{
 "webId": 1001,
 "clientId": 592571,
 "startDate": null,
 "endDate": null,
 "externalKey": null,
 "fromEmail": "abc@gmail.com",
 "fromName": "Test",
 "subject": "hello new",
 "body": "This is a test mail new with attachment",
 "status": "SUCCESS",
 "sentDate": null,
 "sendAttempts": 1,
 "permanentFailure": false,
 "emailRecipientModel": [
 {
      "webId": 1101,
      "clientId": 592571,
      "startDate": null,
      "endDate": null,
      "emailId": 1001,
      "emailAddress": "xyz@gmail.com",
      "status": "PENDING",
      "smtpStatusCode": 123,
      "recipientType": "TO"
 },
 {
      "webId": 1102,
      "clientId": 592571,
      "startDate": null,
      "endDate": null,
      "emailId": 1001,
      "emailAddress": "abc123@gmail.com",
      "status": "PENDING",
      "smtpStatusCode": 123,
      "recipientType": "TO"
 }
 ],
 "emailAttachmentModel": [
 {
      "webId": 1201,
      "clientId": 592571,
      "emailId": 1001,
      "key": "Test Key",
      "location": "Test Location",
      "name": "TestName.txt",
      "content": "VkdocGN5QnBjeUJoSUhSbGMzUWdkR1Y0ZENCQmRIUmhZMmh0Wlc1MERRb05DbFJvYVhNZ2FYTWdZU0IwWlhOMElIUmxlSFFnUVhSMFlXTm9iV1Z1ZEEwS0RRcFVhR2x6SUdseklHRWdkR1Z6ZENCMFpYaDBJRUYwZEdGamFHMWxiblFOQ2cwS1ZHaHBjeUJwY3lCaElIUmxjM1FnZEdWNGRDQkJkSFJoWTJodFpXNTBEUW9OQ2xSb2FYTWdhWE1nWVNCMFpYTjBJSFJsZUhRZ1FYUjBZV05vYldWdWRBMEs="
 }
 ]
}

现在我有一个文本文件在我的电脑上,我已经转换为base64和设置为内容字段。现在如何将此内容发送到电子邮件附件。
这是我的职责

public Email sendEmail(Email email){

    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

    List<EmailConfig> emailConfigs = emailConfigRepository.findAllByClientWebId(email.getClient().getWebId());

    EmailConfig emailConfig;

    if (emailConfigs.iterator().hasNext()){
        emailConfig = emailConfigs.iterator().next();
    }else{
        throw new ResourceNotFoundException(HttpStatus.NOT_FOUND.getReasonPhrase());
    }

    javaMailSender.setHost(emailConfig.getHostName());
    javaMailSender.setPort(emailConfig.getPortNo());
    javaMailSender.setUsername(emailConfig.getEmail());
    javaMailSender.setPassword(emailConfig.getPassword());

    Properties javaMailProperties = new Properties();
    javaMailProperties.put("mail.smtp.starttls.enable", "true");
    javaMailProperties.put("mail.smtp.auth", "true");
    javaMailProperties.put("mail.transport.protocol", "smtp");
    javaMailProperties.put("mail.debug", "true");

    try{

        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setFrom(email.getFromEmail());

        String recipients = email.getEmailRecipient().stream()
            .map(EmailRecipient::getEmailAddress)
            .collect( Collectors.joining( "," ) );

        helper.setTo(InternetAddress.parse(recipients));
        helper.setSubject(email.getSubject());
        helper.setText(email.getBody());
        //helper.addAttachment(emailAttachment.getName(),file);        //need help here

        javaMailSender.setJavaMailProperties(javaMailProperties);
        javaMailSender.send(message);
    }catch (Exception exception){
        exception.printStackTrace();
        email.setStatus(EmailStatus.FAILED);
    }

    return email;
}

将有一个附件列表,因此如何获得该列表并将其发送给所有收件人。
我们将不胜感激。

mepcadol

mepcadol1#

创建临时文件:

File file = File.createTempFile(emailAttachment.getName(), ".tmp");
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(emailAttachment.getContent());
fileWriter.close();

就像这样使用:

helper.addAttachment(emailAttachment.getName(), file);

相关问题