Skip to content

Commit

Permalink
Configure multiple attachment in Email
Browse files Browse the repository at this point in the history
  • Loading branch information
pankajjangid05 committed Apr 6, 2023
1 parent 7251c17 commit cfaa30d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/uci/utils/UtilAppConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public JavaMailSender getJavaMailSender() {
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
props.put("mail.debug", "false");
return mailSender;

}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/uci/utils/model/EmailDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.*;

import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -13,4 +15,5 @@ public class EmailDetails {
private String subject;
private String attachment;
private String attachmentFileName;
private Map<String, String> attachments;
}
49 changes: 42 additions & 7 deletions src/main/java/com/uci/utils/service/EmailServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;

@Service
@Slf4j
Expand Down Expand Up @@ -45,9 +47,12 @@ public void sendSimpleMail(EmailDetails details) {
public void sendMailWithAttachment(EmailDetails details) {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper;
String tempPath = "/tmp/" + details.getAttachmentFileName();
String tempPath = "/tmp/email/";
File file = new File(tempPath);
try {
if (!file.exists()) {
file.mkdirs();
}
mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(sender);
if (details.getRecipient().contains(",")) {
Expand All @@ -58,17 +63,13 @@ public void sendMailWithAttachment(EmailDetails details) {
}
mimeMessageHelper.setText(details.getMsgBody());
mimeMessageHelper.setSubject(details.getSubject());
createTempFile(details.getAttachment(), tempPath);
FileSystemResource fileSystemResource = new FileSystemResource(file);
mimeMessageHelper.addAttachment(fileSystemResource.getFilename(), file);
addAttachments(mimeMessageHelper, details, file.getPath());
javaMailSender.send(mimeMessage);
log.info("Mail sent Successfully...");
} catch (Exception e) {
log.error("Error while sending mail!!! " + e.getMessage());
} finally {
if (file.exists() && file.delete()) {
log.info("file deleted : " + file.getPath());
}
deleteFiles(file);
}
}

Expand All @@ -88,4 +89,38 @@ private void createTempFile(String fileData, String filePath) throws IOException
Files.writeString(path, fileData, StandardCharsets.UTF_8);
log.info("Email attachment temp file is created : " + filePath);
}

private void addAttachments(MimeMessageHelper mimeMessageHelper, EmailDetails emailDetails, String rootPath) {
try {
if (emailDetails != null && emailDetails.getAttachments() != null) {
Map<String, String> attachmentMap = emailDetails.getAttachments();
for (String fileName : attachmentMap.keySet()) {
File file = new File(rootPath + File.separator + fileName + ".txt");
String fileData = attachmentMap.get(fileName);
createTempFile(fileData, file.getPath());
FileSystemResource fileSystemResource = new FileSystemResource(file);
mimeMessageHelper.addAttachment(fileSystemResource.getFilename(), file);
}
} else if (emailDetails != null && emailDetails.getAttachment() != null) {
File file = new File(rootPath + File.separator + emailDetails.getAttachmentFileName() + ".txt");
createTempFile(emailDetails.getAttachment(), file.getPath());
FileSystemResource fileSystemResource = new FileSystemResource(file);
mimeMessageHelper.addAttachment(fileSystemResource.getFilename(), file);
} else {
log.error("No Attachment in Email...");
}
} catch (Exception ex) {
log.error("An error occured : " + ex.getMessage());
}
}

public static void deleteFiles(File dirPath) {
File filesList[] = dirPath.listFiles();
for (File file : filesList) {
if (file.isFile()) {
log.info("Attachments deleted : " + file.getPath());
file.delete();
}
}
}
}

0 comments on commit cfaa30d

Please sign in to comment.