From fc2eabd5204fd3d4f74e6a6ab2e06faf9c85db77 Mon Sep 17 00:00:00 2001 From: Chinmay Chaudhari Date: Fri, 2 Oct 2020 22:38:06 +0530 Subject: [PATCH 1/4] Changes for STMP Connection in application.properties --- .../java/me/anant/PMS/service/EmailService.java | 12 +++++++----- src/main/resources/application.properties | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/anant/PMS/service/EmailService.java b/src/main/java/me/anant/PMS/service/EmailService.java index c2399cc..749e84a 100644 --- a/src/main/java/me/anant/PMS/service/EmailService.java +++ b/src/main/java/me/anant/PMS/service/EmailService.java @@ -8,12 +8,12 @@ import org.springframework.mail.javamail.MimeMessagePreparator; import org.springframework.stereotype.Service; -import me.anant.PMS.config.EmailConfig; - @Service public class EmailService { + + //Added Autowired For Java MAil Sender @Autowired - EmailConfig emailConfig; + private JavaMailSender javaMailSender; public void send(String to, String subject, String body) { String template = "\r\n" + @@ -32,7 +32,7 @@ public void send(String to, String subject, String body) { + "Team PMS" + "" + ""; - JavaMailSender mailSender = emailConfig.getJavaMailSender(); + MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); @@ -42,6 +42,8 @@ public void prepare(MimeMessage mimeMessage) throws Exception { message.setText(template, true); } }; - mailSender.send(preparator); + + //Replaced EmailConfig with JavaMailSender + javaMailSender.send(preparator); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3144dd8..43efe50 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,3 +4,20 @@ spring.mvc.view.suffix=.jsp spring.h2.console.enabled=true spring.datasource.platform=h2 spring.datasource.url=jdbc:h2:mem:pms + +#SMTP Properties for Java Mail Sender +spring.mail.host=smtp.mailtrap.io +spring.mail.port=2525 +spring.mail.username=53b095fe63e8b6 +spring.mail.password=c87dc71a300727 + +# Other properties +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.connectiontimeout=5000 +spring.mail.properties.mail.smtp.timeout=5000 +spring.mail.properties.mail.smtp.writetimeout=5000 + +# TLS +spring.mail.properties.transport.protocol=smtp +spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.properties.mail.debug=true \ No newline at end of file From d68358a995dead6ab6eab68cf6391bb6821167fd Mon Sep 17 00:00:00 2001 From: Chinmay Chaudhari Date: Sat, 3 Oct 2020 15:56:33 +0530 Subject: [PATCH 2/4] Removed EMailConfig File and Removed COmments from EmailService --- .../java/me/anant/PMS/config/EmailConfig.java | 29 ---------- .../me/anant/PMS/service/EmailService.java | 54 ++++++++----------- 2 files changed, 22 insertions(+), 61 deletions(-) delete mode 100644 src/main/java/me/anant/PMS/config/EmailConfig.java diff --git a/src/main/java/me/anant/PMS/config/EmailConfig.java b/src/main/java/me/anant/PMS/config/EmailConfig.java deleted file mode 100644 index 4df224e..0000000 --- a/src/main/java/me/anant/PMS/config/EmailConfig.java +++ /dev/null @@ -1,29 +0,0 @@ -package me.anant.PMS.config; - -import java.util.Properties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.mail.javamail.JavaMailSender; -import org.springframework.mail.javamail.JavaMailSenderImpl; - -@Configuration -public class EmailConfig { - @Bean - public JavaMailSender getJavaMailSender() - { - JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); - mailSender.setHost("smtp.mailtrap.io"); - mailSender.setPort(2525); - - mailSender.setUsername("53b095fe63e8b6"); - mailSender.setPassword("c87dc71a300727"); - - Properties props = mailSender.getJavaMailProperties(); - props.put("mail.transport.protocol", "smtp"); - props.put("mail.smtp.auth", "true"); - props.put("mail.smtp.starttls.enable", "true"); - props.put("mail.debug", "true"); - - return mailSender; - } -} diff --git a/src/main/java/me/anant/PMS/service/EmailService.java b/src/main/java/me/anant/PMS/service/EmailService.java index 749e84a..334c216 100644 --- a/src/main/java/me/anant/PMS/service/EmailService.java +++ b/src/main/java/me/anant/PMS/service/EmailService.java @@ -10,40 +10,30 @@ @Service public class EmailService { - - //Added Autowired For Java MAil Sender + @Autowired - private JavaMailSender javaMailSender; - + private JavaMailSender javaMailSender; + public void send(String to, String subject, String body) { - String template = "\r\n" + - "\r\n" + - " \r\n" + - "" - + "" - + "" - + "" - + body - + "

" - + "Thanks & Regards,
" - + "Team PMS" - + "" - + ""; - - MimeMessagePreparator preparator = new MimeMessagePreparator() { - public void prepare(MimeMessage mimeMessage) throws Exception { - MimeMessageHelper message = new MimeMessageHelper(mimeMessage); - message.setTo(to); - message.setFrom("system@gmail.com"); - message.setSubject(subject); - message.setText(template, true); - } - }; - - //Replaced EmailConfig with JavaMailSender - javaMailSender.send(preparator); + + "table, th, td {border: 1px solid black; padding: 5px}" + "" + "" + "" + body + + "

" + "Thanks & Regards,
" + "Team PMS" + "" + ""; + + MimeMessagePreparator preparator = new MimeMessagePreparator() { + public void prepare(MimeMessage mimeMessage) throws Exception { + MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true); + + message.setTo(to); + message.setFrom("system@gmail.com"); + message.setSubject(subject); + message.setText(template, true); + + } + }; + + javaMailSender.send(preparator); } } From 1a46e7183684c4caad95c5fb8142d3ee8f2220d1 Mon Sep 17 00:00:00 2001 From: Chinmay Chaudhari Date: Sat, 3 Oct 2020 16:13:07 +0530 Subject: [PATCH 3/4] Changed Readme file for SMPT Configuration --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 0e398f1..43de72d 100644 --- a/Readme.md +++ b/Readme.md @@ -20,7 +20,7 @@ It is a marketplace where customer can place order and Admin can manage inventor - Maven ## Configuration -Change the SMTP details in "EmailConfig" +Change the SMTP details in "application.properties" ## Login Details From 8488bbea822a508e078d166237b5ea44ad9bcd23 Mon Sep 17 00:00:00 2001 From: Chinmay Chaudhari Date: Sat, 3 Oct 2020 18:21:41 +0530 Subject: [PATCH 4/4] Changes for PDF Invoice Attachment with Email --- pom.xml | 103 +++--- .../anant/PMS/controller/OrderController.java | 69 ++-- .../me/anant/PMS/service/EmailService.java | 4 + .../me/anant/PMS/util/GenerateInvoice.java | 49 +++ src/main/resources/InvoiceTemplate.html | 335 ++++++++++++++++++ 5 files changed, 481 insertions(+), 79 deletions(-) create mode 100644 src/main/java/me/anant/PMS/util/GenerateInvoice.java create mode 100644 src/main/resources/InvoiceTemplate.html diff --git a/pom.xml b/pom.xml index 4d1f0c3..4f286ea 100644 --- a/pom.xml +++ b/pom.xml @@ -1,12 +1,13 @@ - 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE - + me.anant PMS @@ -34,7 +35,7 @@ h2 runtime - + org.springframework.boot spring-boot-starter-test @@ -46,58 +47,76 @@ - + - org.apache.tomcat - tomcat-jasper - 9.0.33 + org.apache.tomcat + tomcat-jasper + 9.0.33 - + - org.springframework.boot - spring-boot-starter-security + org.springframework.boot + spring-boot-starter-security - + - org.springframework.security - spring-security-taglibs + org.springframework.security + spring-security-taglibs - + org.springframework.boot spring-boot-devtools - + - org.springframework - spring-context-support - - - - javax.mail - mail - 1.4.7 - - - - javax.servlet - jstl - 1.2 - - - - javax.validation - validation-api - 1.1.0.Final - - - - org.hibernate - hibernate-validator - 5.1.0.Final - + org.springframework + spring-context-support + + + + javax.mail + mail + 1.4.7 + + + + javax.servlet + jstl + 1.2 + + + + javax.validation + validation-api + 1.1.0.Final + + + + org.hibernate + hibernate-validator + 5.1.0.Final + + + + org.thymeleaf + thymeleaf + 3.0.11.RELEASE + + + + org.thymeleaf + thymeleaf-spring5 + 3.0.11.RELEASE + + + + org.xhtmlrenderer + flying-saucer-pdf + 9.1.20 + diff --git a/src/main/java/me/anant/PMS/controller/OrderController.java b/src/main/java/me/anant/PMS/controller/OrderController.java index 23fd3df..4cdd1aa 100644 --- a/src/main/java/me/anant/PMS/controller/OrderController.java +++ b/src/main/java/me/anant/PMS/controller/OrderController.java @@ -1,6 +1,5 @@ package me.anant.PMS.controller; -import java.awt.Dialog.ModalExclusionType; import java.security.Principal; import java.util.HashSet; import java.util.List; @@ -8,7 +7,6 @@ import javax.servlet.http.HttpServletRequest; -import org.omg.CORBA.PUBLIC_MEMBER; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @@ -25,34 +23,35 @@ import me.anant.PMS.service.OrderService; import me.anant.PMS.service.ProductService; import me.anant.PMS.service.UserService; +import me.anant.PMS.util.GenerateInvoice; @Controller public class OrderController { @Autowired ProductService productService; - + @Autowired UserService userService; - + @Autowired OrderService orderService; - + @Autowired EmailService emailService; - + @GetMapping("/customer/order_place") public ModelAndView customerHome() { - List pList = productService.get(); + List pList = productService.get(); ModelAndView modelAndView = new ModelAndView("customer/home"); modelAndView.addObject("pList", pList); return modelAndView; } - + @PostMapping("/customer/order_place") public ModelAndView orderPlace(HttpServletRequest request, Principal principal) { String[] pIds = request.getParameterValues("productId"); Set opList = new HashSet<>(); - for(String pId: pIds) { + for (String pId : pIds) { long pid = Long.parseLong(pId); Product product = productService.findById(pid).get(); int buyqty = Integer.parseInt(request.getParameter(pId)); @@ -61,35 +60,29 @@ public ModelAndView orderPlace(HttpServletRequest request, Principal principal) } User user = userService.findByEmail(principal.getName()); orderService.save(new Order(user, "PROCESSING", opList)); - + String message = "Hello,

Your order has been placed successfuly. Following is the detail of your order.

" - + "" + - "" + - "" + - "" + - "" + - "" + - ""; + + "
NamePriceQtyAmount
" + "" + "" + "" + "" + "" + + ""; float sum = 0; - for (OrderProduct op : opList) - { + for (OrderProduct op : opList) { sum = sum + op.getProduct().getProductPrice() * op.getBuyqty(); - message = message + "" + - "" + - "" + - "" + - "" + - ""; + message = message + "" + "" + "" + "" + "" + ""; } - message = message + "" + - "
NamePriceQtyAmount
"+op.getProduct().getProductName()+"Rs. "+op.getProduct().getProductPrice()+""+op.getBuyqty()+"Rs. "+op.getProduct().getProductPrice() * op.getBuyqty()+"
" + op.getProduct().getProductName() + "Rs. " + + op.getProduct().getProductPrice() + "" + op.getBuyqty() + "Rs. " + + op.getProduct().getProductPrice() * op.getBuyqty() + "
Total Amount
Rs. "+sum+"
"; - emailService.send(principal.getName(), "Order Placed successfully", message); + message = message + "
Total Amount
Rs. " + sum + + "" + ""; + + GenerateInvoice.exportToPdfBox(opList, sum); + emailService.send(principal.getName(), "Order Placed successfully", message); + ModelAndView modelAndView = new ModelAndView("customer/order_place"); modelAndView.addObject("opList", opList); return modelAndView; } - + @GetMapping("customer/order/list") public ModelAndView viewMyOrder(Principal principal) { User user = userService.findByEmail(principal.getName()); @@ -97,7 +90,7 @@ public ModelAndView viewMyOrder(Principal principal) { modelAndView.addObject("orderList", user.getOrders()); return modelAndView; } - + @GetMapping("admin/order/list") public ModelAndView viewAllOrder() { List oList = orderService.get(); @@ -105,35 +98,37 @@ public ModelAndView viewAllOrder() { modelAndView.addObject("oList", oList); return modelAndView; } - + @GetMapping("admin/order/detail") public ModelAndView orderDetailAdmin(@RequestParam("id") long id) { ModelAndView modelAndView = new ModelAndView("admin/order/detail"); modelAndView.addObject("order", orderService.findById(id).get()); return modelAndView; } - + @GetMapping("customer/order/detail") public ModelAndView orderDetailCustomer(@RequestParam("id") long id) { ModelAndView modelAndView = new ModelAndView("customer/order/detail"); modelAndView.addObject("order", orderService.findById(id).get()); return modelAndView; } - + @GetMapping("customer/order/cancel") public String orderCancel(@RequestParam("id") long id, final RedirectAttributes redirectAttributes) { - if(orderService.cancelOrder(id)) { + if (orderService.cancelOrder(id)) { redirectAttributes.addFlashAttribute("msg", "Order cancelled successfully"); redirectAttributes.addFlashAttribute("class", "alert-success"); } else { - redirectAttributes.addFlashAttribute("msg", "Order not cancelled, since you can cencel PROCESSING or CONFIRMED order with 24 hours only."); + redirectAttributes.addFlashAttribute("msg", + "Order not cancelled, since you can cencel PROCESSING or CONFIRMED order with 24 hours only."); redirectAttributes.addFlashAttribute("class", "alert-danger"); } return "redirect:/customer/order/list"; } - + @PostMapping("admin/order/status") - public String changeStatus(@RequestParam("id") long id, @RequestParam("status") String status, final RedirectAttributes redirectAttributes) { + public String changeStatus(@RequestParam("id") long id, @RequestParam("status") String status, + final RedirectAttributes redirectAttributes) { orderService.changeStatus(id, status); redirectAttributes.addFlashAttribute("msg", "Status of order changed."); redirectAttributes.addFlashAttribute("class", "alert-success"); diff --git a/src/main/java/me/anant/PMS/service/EmailService.java b/src/main/java/me/anant/PMS/service/EmailService.java index 334c216..8b887b2 100644 --- a/src/main/java/me/anant/PMS/service/EmailService.java +++ b/src/main/java/me/anant/PMS/service/EmailService.java @@ -1,5 +1,7 @@ package me.anant.PMS.service; +import java.io.File; + import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired; @@ -31,6 +33,8 @@ public void prepare(MimeMessage mimeMessage) throws Exception { message.setSubject(subject); message.setText(template, true); + message.addAttachment("invoice.pdf", new File("invoice.pdf")); + } }; diff --git a/src/main/java/me/anant/PMS/util/GenerateInvoice.java b/src/main/java/me/anant/PMS/util/GenerateInvoice.java new file mode 100644 index 0000000..cf6fade --- /dev/null +++ b/src/main/java/me/anant/PMS/util/GenerateInvoice.java @@ -0,0 +1,49 @@ +package me.anant.PMS.util; + +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.Date; +import java.util.Set; + +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; +import org.thymeleaf.templatemode.TemplateMode; +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; +import org.xhtmlrenderer.pdf.ITextRenderer; + +import me.anant.PMS.model.OrderProduct; + +public class GenerateInvoice { + + public static void exportToPdfBox(Set opList,float sum) { + + try { + ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode(TemplateMode.HTML); + + TemplateEngine templateEngine = new TemplateEngine(); + templateEngine.setTemplateResolver(templateResolver); + + Context context = new Context(); + context.setVariable("invoiceDate", new Date()); + context.setVariable("totalAmount", sum); + context.setVariable("orderedProductList", opList); + + String html = templateEngine.process("InvoiceTemplate", context); + + String outputFolder = "invoice.pdf"; + System.out.println(outputFolder); + OutputStream outputStream = new FileOutputStream(outputFolder); + + ITextRenderer renderer = new ITextRenderer(); + renderer.setDocumentFromString(html); + renderer.layout(); + renderer.createPDF(outputStream); + + outputStream.close(); + } catch (Exception e) { + System.out.println(e); + } + } +} \ No newline at end of file diff --git a/src/main/resources/InvoiceTemplate.html b/src/main/resources/InvoiceTemplate.html new file mode 100644 index 0000000..a5058ad --- /dev/null +++ b/src/main/resources/InvoiceTemplate.html @@ -0,0 +1,335 @@ + + + + + Invoice + + + + +
+

Invoice

+
+

Jonathan Neal

+

101 E. Chapman Ave

Orange, CA 92866

+

(800) 555-1234

+
+ +
+
+
+

Some Company

c/o Some Guy

+
+ + + + + + + + + + + + + +
Invoice #101138
Date
Amount Due$
+ + + + + + + + + + + + + + + + + +
NamePriceQuantityAmount
+ + + + + +
Total$
+
+ + + + \ No newline at end of file