From 3a992a95c9665b4e87f29f1783cabf9761e83d8f Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Mon, 3 Apr 2023 19:40:09 +0530 Subject: [PATCH] Add email to be sent to devs when error arrise --- pom.xml | 6 +- .../consumers/OutboundKafkaController.java | 27 ++++-- .../com/uci/outbound/entity/EmailDetails.java | 18 ++++ .../uci/outbound/service/EmailService.java | 9 ++ .../outbound/service/EmailServiceImpl.java | 92 +++++++++++++++++++ src/main/resources/application.properties | 10 ++ 6 files changed, 154 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/uci/outbound/entity/EmailDetails.java create mode 100644 src/main/java/com/uci/outbound/service/EmailService.java create mode 100644 src/main/java/com/uci/outbound/service/EmailServiceImpl.java diff --git a/pom.xml b/pom.xml index db2e440..b06cae4 100644 --- a/pom.xml +++ b/pom.xml @@ -150,7 +150,11 @@ 4.9.3 - + + org.springframework.boot + spring-boot-starter-mail + + diff --git a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java index 28cbc8b..aa412dc 100644 --- a/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java +++ b/src/main/java/com/uci/outbound/consumers/OutboundKafkaController.java @@ -4,6 +4,9 @@ import com.uci.adapter.provider.factory.ProviderFactory; import com.uci.dao.models.XMessageDAO; import com.uci.dao.repository.XMessageRepository; +import com.uci.outbound.entity.EmailDetails; +import com.uci.outbound.service.EmailService; +import com.uci.outbound.service.EmailServiceImpl; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import messagerosa.core.model.XMessage; @@ -14,6 +17,7 @@ import io.fusionauth.domain.api.LoginRequest; import messagerosa.xml.XMessageParser; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.event.EventListener; import org.springframework.data.redis.core.HashOperations; @@ -38,27 +42,33 @@ public class OutboundKafkaController { @Autowired private XMessageRepository xMessageRepo; - + @Autowired private RedisCacheService redisCacheService; - + private HashOperations hashOperations; //to access Redis cache + @Autowired + private EmailServiceImpl emailService; + + @Value("${spring.mail.recipient}") + private String recipient; + @EventListener(ApplicationStartedEvent.class) public void onMessage() { - + reactiveKafkaReceiver .doOnNext(new Consumer>() { @Override public void accept(ReceiverRecord msg) { - log.info("kafka message receieved!"); + log.info("kafka message receieved!"); XMessage currentXmsg = null; try { currentXmsg = XMessageParser.parse(new ByteArrayInputStream(msg.value().getBytes())); sendOutboundMessage(currentXmsg); } catch (Exception e) { e.printStackTrace(); - } + } } }) @@ -78,6 +88,7 @@ public void accept(Throwable e) { * @throws Exception */ public void sendOutboundMessage(XMessage currentXmsg) throws Exception { + emailService.sendMailWithAttachment("Error in Outbound", "PFA", recipient, currentXmsg, "XMessage.txt"); String channel = currentXmsg.getChannelURI(); String provider = currentXmsg.getProviderURI(); IProvider iprovider = factoryProvider.getProvider(provider, channel); @@ -85,6 +96,7 @@ public void sendOutboundMessage(XMessage currentXmsg) throws Exception { .doOnError(new Consumer() { @Override public void accept(Throwable e) { + emailService.sendMailWithAttachment("Error in Outbound", "PFA", recipient, currentXmsg, "XMessage.txt"); log.error("Exception in processOutBoundMessageF:"+e.getMessage()); } }).subscribe(new Consumer() { @@ -110,6 +122,7 @@ public void accept(XMessageDAO xMessageDAO) { } }); }catch(Exception e){ + emailService.sendMailWithAttachment("Error in Outbound", "PFA", recipient, xMessage, "XMessage.txt"); log.error("Exception in convertXMessageToDAO:" + e.getMessage()); try{ log.error("The current XMessage was " + xMessage.toXML()); @@ -126,8 +139,8 @@ public void accept(XMessageDAO xMessageDAO) { } }); } - + private String redisKeyWithPrefix(String key) { - return System.getenv("ENV")+"-"+key; + return System.getenv("ENV")+"-"+key; } } diff --git a/src/main/java/com/uci/outbound/entity/EmailDetails.java b/src/main/java/com/uci/outbound/entity/EmailDetails.java new file mode 100644 index 0000000..a00adab --- /dev/null +++ b/src/main/java/com/uci/outbound/entity/EmailDetails.java @@ -0,0 +1,18 @@ +package com.uci.outbound.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@ToString +public class EmailDetails { + private String recipient; + private String msgBody; + private String subject; + private String attachment; + private String attachmentFileName; +} \ No newline at end of file diff --git a/src/main/java/com/uci/outbound/service/EmailService.java b/src/main/java/com/uci/outbound/service/EmailService.java new file mode 100644 index 0000000..b228a19 --- /dev/null +++ b/src/main/java/com/uci/outbound/service/EmailService.java @@ -0,0 +1,9 @@ +package com.uci.outbound.service; + +import com.uci.outbound.entity.EmailDetails; + +public interface EmailService { + public void sendSimpleMail(EmailDetails details); + + public void sendMailWithAttachment(EmailDetails details); +} diff --git a/src/main/java/com/uci/outbound/service/EmailServiceImpl.java b/src/main/java/com/uci/outbound/service/EmailServiceImpl.java new file mode 100644 index 0000000..941f287 --- /dev/null +++ b/src/main/java/com/uci/outbound/service/EmailServiceImpl.java @@ -0,0 +1,92 @@ +package com.uci.outbound.service; + + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import javax.mail.internet.MimeMessage; + +import com.uci.outbound.entity.EmailDetails; +import lombok.extern.slf4j.Slf4j; +import messagerosa.core.model.XMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.FileSystemResource; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Service; + +@Service +@Slf4j +public class EmailServiceImpl implements EmailService { + @Autowired + private JavaMailSender javaMailSender; + + @Value("${spring.mail.username}") + private String sender; + + public void sendSimpleMail(EmailDetails details) { + try { + SimpleMailMessage mailMessage = new SimpleMailMessage(); + mailMessage.setFrom(sender); + mailMessage.setTo(details.getRecipient()); + mailMessage.setText(details.getMsgBody()); + mailMessage.setSubject(details.getSubject()); + javaMailSender.send(mailMessage); + log.info("Mail Sent Successfully..."); + } catch (Exception e) { + log.error("Error while Sending Mail" + e.getMessage()); + } + } + + public void sendMailWithAttachment(EmailDetails details) { + MimeMessage mimeMessage = javaMailSender.createMimeMessage(); + MimeMessageHelper mimeMessageHelper; + String tempPath = "/tmp/" + details.getAttachmentFileName(); + File file = new File(tempPath); + try { + mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); + mimeMessageHelper.setFrom(sender); + if (details.getRecipient().contains(",")) { + String recipients[] = details.getRecipient().split(","); + mimeMessageHelper.setTo(recipients); + } else { + mimeMessageHelper.setTo(details.getRecipient()); + } + mimeMessageHelper.setText(details.getMsgBody()); + mimeMessageHelper.setSubject(details.getSubject()); + createTempFile(details.getAttachment(), tempPath); + FileSystemResource fileSystemResource = new FileSystemResource(file); + mimeMessageHelper.addAttachment(fileSystemResource.getFilename(), file); + 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()); + } + } + } + + public void sendMailWithAttachment(String subject, String body, String recipient, XMessage xMessage, String attachmentFileName) { + EmailDetails emailDetails = new EmailDetails(); + emailDetails.setSubject(subject); + emailDetails.setMsgBody(body); + emailDetails.setRecipient(recipient); + emailDetails.setAttachment(xMessage.toString()); + emailDetails.setAttachmentFileName(attachmentFileName); + log.info("EmailDetails :" + emailDetails); + sendMailWithAttachment(emailDetails); + } + + private void createTempFile(String fileData, String filePath) throws IOException { + Path path = Paths.get(filePath); + Files.writeString(path, fileData, StandardCharsets.UTF_8); + log.info("Email attachment temp file is created : " + filePath); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0e79a95..22990da 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -118,3 +118,13 @@ file.cdn.selected=${SELECTED_FILE_CDN:#{"azure"}} #for notification key enable/disable when notification triggered fcm.notificationKeyEnable=${NOTIFICATION_KEY_ENABLE:#{"true"}} + +#Email Config SMTP +spring.mail.host=${EMAIL_HOST:#{""}} +spring.mail.port=${EMAIL_PORT:#{""}} +spring.mail.username=${EMAIL_USERNAME:#{""}} +spring.mail.password=${EMAIL_PASSWORD:#{""}} +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.recipient=${RECIPIENT_EMAILS:#{""}} +