From 14798353408822ee4cd181a6983f998245eaf0f8 Mon Sep 17 00:00:00 2001 From: Julia Butenhoff Date: Thu, 7 Jul 2022 12:16:58 -0500 Subject: [PATCH] Added an Email Service to send postcards - Credentials not working properly - Needs testing --- pom.xml | 4 ++ .../UBFunkeysServer/Galaxy/GalaxyServer.java | 32 +++++++++-- .../UBFunkeysServer/service/EmailService.java | 7 +++ .../service/impl/EmailServiceImpl.java | 57 +++++++++++++++++++ 4 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/icedberries/UBFunkeysServer/service/EmailService.java create mode 100644 src/main/java/com/icedberries/UBFunkeysServer/service/impl/EmailServiceImpl.java diff --git a/pom.xml b/pom.xml index 8a654c8..2dcb451 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,10 @@ spring-boot-starter-tomcat provided + + org.springframework.boot + spring-boot-starter-mail + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/com/icedberries/UBFunkeysServer/Galaxy/GalaxyServer.java b/src/main/java/com/icedberries/UBFunkeysServer/Galaxy/GalaxyServer.java index fa5b885..a810f04 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/Galaxy/GalaxyServer.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/Galaxy/GalaxyServer.java @@ -1,7 +1,9 @@ package com.icedberries.UBFunkeysServer.Galaxy; +import com.icedberries.UBFunkeysServer.service.EmailService; import org.apache.commons.lang3.StringUtils; import org.apache.tomcat.util.http.fileupload.IOUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpStatus; @@ -12,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.HandlerMapping; import org.w3c.dom.Document; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; import javax.servlet.http.HttpServletRequest; @@ -22,10 +25,14 @@ import javax.xml.parsers.ParserConfigurationException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.StringReader; @RestController public class GalaxyServer { + @Autowired + private EmailService emailService; + /** * This is only used as part of the updater to pass files to the client as requested via URL path */ @@ -42,9 +49,7 @@ public class GalaxyServer { int errorCode = 0; try { // Try to open the file as a resource - InputStream file = resource.getInputStream(); - - byte[] fileContent = org.apache.commons.io.IOUtils.toByteArray(file); + byte[] fileContent = org.apache.commons.io.IOUtils.toByteArray(resource.getInputStream()); // Check if the file exists if (fileContent.length > 0) { @@ -96,7 +101,7 @@ public class GalaxyServer { // Parse the xml body of the request DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - Document doc = dBuilder.parse(xmlBody); + Document doc = dBuilder.parse(new InputSource(new StringReader(xmlBody))); doc.getDocumentElement().normalize(); // Get the root element @@ -139,7 +144,22 @@ public class GalaxyServer { private ResponseEntity sendPostcard(Document request) { System.out.println("[Galaxy][POST] postcard request received"); - //TODO: IMPLEMENT METHOD - return null; + //TODO: PARSE OUT DATA FROM REQUEST + + // Parse data from the request + String to = "cockatoo242@gmail.com"; + String subject = "Greetings From Funkiki Island"; + String body = "Wow, check out this awesome postcard!"; + String fileName = "card_0.jpg"; + + // Try to send the postcard to the email + String response = ""; + if (emailService.sendMailWithAttachment(to, subject, body, fileName)) { + // Send successful + response = ""; + } else { + response = ""; + } + return new ResponseEntity<>(response, HttpStatus.OK); } } diff --git a/src/main/java/com/icedberries/UBFunkeysServer/service/EmailService.java b/src/main/java/com/icedberries/UBFunkeysServer/service/EmailService.java new file mode 100644 index 0000000..8e0b3eb --- /dev/null +++ b/src/main/java/com/icedberries/UBFunkeysServer/service/EmailService.java @@ -0,0 +1,7 @@ +package com.icedberries.UBFunkeysServer.service; + +public interface EmailService { + + Boolean sendMailWithAttachment(String to, String subject, String body, String fileToAttach); + +} diff --git a/src/main/java/com/icedberries/UBFunkeysServer/service/impl/EmailServiceImpl.java b/src/main/java/com/icedberries/UBFunkeysServer/service/impl/EmailServiceImpl.java new file mode 100644 index 0000000..44295dc --- /dev/null +++ b/src/main/java/com/icedberries/UBFunkeysServer/service/impl/EmailServiceImpl.java @@ -0,0 +1,57 @@ +package com.icedberries.UBFunkeysServer.service.impl; + +import com.icedberries.UBFunkeysServer.service.EmailService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.mail.MailException; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.mail.javamail.MimeMessagePreparator; +import org.springframework.stereotype.Service; + +import javax.mail.Message; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +@Service("emailService") +public class EmailServiceImpl implements EmailService { + + @Autowired + private JavaMailSender mailsender; + + @Value("${spring.mail.username}") + private String from; + + @Override + public Boolean sendMailWithAttachment(String to, String subject, String body, String fileToAttach) { + MimeMessagePreparator preparator = new MimeMessagePreparator() { + @Override + public void prepare(MimeMessage mimeMessage) throws Exception { + // Set the main parts of the email + mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); + mimeMessage.setFrom(new InternetAddress(from)); + mimeMessage.setSubject(subject); + mimeMessage.setText(body); + + // Attach the postcard + Resource resource = new ClassPathResource("static/Postcards/" + fileToAttach); + byte[] fileContent = org.apache.commons.io.IOUtils.toByteArray(resource.getInputStream()); + MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); + helper.addAttachment(fileToAttach, new ByteArrayResource(fileContent)); + } + }; + + try { + mailsender.send(preparator); + System.out.println("[GALAXY][POST] Email send successfully to: " + to); + return true; + } catch(MailException e) { + System.out.println("[GALAXY][POST][ERROR] Unable to send email to: " + to); + e.printStackTrace(); + return false; + } + } +}