我想发送附件给所有的电子邮件收件人。
这是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;
}
将有一个附件列表,因此如何获得该列表并将其发送给所有收件人。
我们将不胜感激。
1条答案
按热度按时间mepcadol1#
创建临时文件:
就像这样使用: