Added an Email Service to send postcards

- Credentials not working properly
- Needs testing
This commit is contained in:
2022-07-07 12:16:58 -05:00
committed by Julia Butenhoff
parent 78e8940e37
commit 1479835340
4 changed files with 94 additions and 6 deletions

View File

@@ -33,6 +33,10 @@
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -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<String> 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 = "<postcard result=\"0\" reason=\"Postcard Sent!\" cost=\"5\" />";
} else {
response = "<postcard result=\"1\" reason=\"Unable to send postcard at this time!\" cost=\"0\" />";
}
return new ResponseEntity<>(response, HttpStatus.OK);
}
}

View File

@@ -0,0 +1,7 @@
package com.icedberries.UBFunkeysServer.service;
public interface EmailService {
Boolean sendMailWithAttachment(String to, String subject, String body, String fileToAttach);
}

View File

@@ -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;
}
}
}