I want to send attachments to all the recipients of email.
This is the Json.
{
"webId": 1001,
"clientId": 592571,
"startDate": null,
"endDate": null,
"externalKey": null,
"fromEmail": "[email protected]",
"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": "[email protected]",
"status": "PENDING",
"smtpStatusCode": 123,
"recipientType": "TO"
},
{
"webId": 1102,
"clientId": 592571,
"startDate": null,
"endDate": null,
"emailId": 1001,
"emailAddress": "[email protected]",
"status": "PENDING",
"smtpStatusCode": 123,
"recipientType": "TO"
}
],
"emailAttachmentModel": [
{
"webId": 1201,
"clientId": 592571,
"emailId": 1001,
"key": "Test Key",
"location": "Test Location",
"name": "TestName.txt",
"content": "VkdocGN5QnBjeUJoSUhSbGMzUWdkR1Y0ZENCQmRIUmhZMmh0Wlc1MERRb05DbFJvYVhNZ2FYTWdZU0IwWlhOMElIUmxlSFFnUVhSMFlXTm9iV1Z1ZEEwS0RRcFVhR2x6SUdseklHRWdkR1Z6ZENCMFpYaDBJRUYwZEdGamFHMWxiblFOQ2cwS1ZHaHBjeUJwY3lCaElIUmxjM1FnZEdWNGRDQkJkSFJoWTJodFpXNTBEUW9OQ2xSb2FYTWdhWE1nWVNCMFpYTjBJSFJsZUhRZ1FYUjBZV05vYldWdWRBMEs="
}
]
}
Now i have a text file on my PC which i have converted to BASE64 and set it to content field. Now how to get this content to email attachment.
This is my Function
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;
}
There will be a list of attachments so how to get that list and send it to all the recipients.
Help will be appreciated.
question from:
https://stackoverflow.com/questions/65847321/send-email-with-attachment-getting-data-from-content-bytea-postgresql