From 10eb5db3a48ce0bbd478bf5c5412211db1e7752a Mon Sep 17 00:00:00 2001 From: Anant Jain Date: Thu, 7 May 2020 22:17:22 +0530 Subject: [PATCH] EMail --- Readme.md | 6 ++- pom.xml | 12 +++++ .../java/me/anant/PMS/HomeController.java | 14 ------ .../java/me/anant/PMS/ProductController.java | 41 ---------------- .../java/me/anant/PMS/config/EmailConfig.java | 29 ++++++++++++ .../anant/PMS/controller/OrderController.java | 28 +++++++++++ src/main/java/me/anant/PMS/model/Admin.java | 10 ---- .../java/me/anant/PMS/model/Customer.java | 10 ---- .../me/anant/PMS/service/EmailService.java | 47 +++++++++++++++++++ 9 files changed, 121 insertions(+), 76 deletions(-) delete mode 100644 src/main/java/me/anant/PMS/HomeController.java delete mode 100644 src/main/java/me/anant/PMS/ProductController.java create mode 100644 src/main/java/me/anant/PMS/config/EmailConfig.java delete mode 100644 src/main/java/me/anant/PMS/model/Admin.java delete mode 100644 src/main/java/me/anant/PMS/model/Customer.java create mode 100644 src/main/java/me/anant/PMS/service/EmailService.java diff --git a/Readme.md b/Readme.md index f17df64..6956b9e 100644 --- a/Readme.md +++ b/Readme.md @@ -7,7 +7,6 @@ It is a marketplace where customer can place order and Admin can manage inventor - Spring Data JPA for Creating Custom Repository - Spring Boot for Autoconfiguration and Dependency Management - Spring Security for Authentication & Authorisation -- Hibernate for Object Relation Mapping - H2 In-memory Database for Storing data - Java Mail API to send HTML E-Mail over SMTP @@ -16,14 +15,19 @@ It is a marketplace where customer can place order and Admin can manage inventor - Oracle - Apache Tomcat +## Configuration +Change the SMTP details in "EmailConfig" + ## Login Details #### Admin Username: admin@gmail.com + Password: admin #### Customer Username: customer@gmail.com + Password: user ## Screenshots diff --git a/pom.xml b/pom.xml index f6f8ebd..445c843 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,18 @@ org.springframework.boot spring-boot-devtools + + + + org.springframework + spring-context-support + + + + javax.mail + mail + 1.4.7 + diff --git a/src/main/java/me/anant/PMS/HomeController.java b/src/main/java/me/anant/PMS/HomeController.java deleted file mode 100644 index 7f312e8..0000000 --- a/src/main/java/me/anant/PMS/HomeController.java +++ /dev/null @@ -1,14 +0,0 @@ -package me.anant.PMS; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; - -@Controller -public class HomeController { - @RequestMapping("/hoho") - public String home() { - System.out.println("hi"); - return "index"; - } -} diff --git a/src/main/java/me/anant/PMS/ProductController.java b/src/main/java/me/anant/PMS/ProductController.java deleted file mode 100644 index 4e866f3..0000000 --- a/src/main/java/me/anant/PMS/ProductController.java +++ /dev/null @@ -1,41 +0,0 @@ -package me.anant.PMS; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.servlet.ModelAndView; - -import me.anant.PMS.dao.ProductRepo; -import me.anant.PMS.model.Product; - -@Controller -public class ProductController { - @Autowired - ProductRepo productRepo; - - @PostMapping("/admin/product/add") - public String add(Product product) { - productRepo.save(product); - return "admin/product/add"; - } - - @GetMapping("/admin/product/list") - public ModelAndView list() { - List pList = (List) productRepo.findAll(); - ModelAndView modelAndView = new ModelAndView(); - modelAndView.setViewName("/admin/product/list"); - modelAndView.addObject("pList", pList); - return modelAndView; - } - -// @GetMapping("/admin/product/list") -// public String list(Model model) { -// List pList = (List) productRepo.findAll(); -// model.addAttribute("pList", pList) -// return "/admin/product/list"; -// } -} diff --git a/src/main/java/me/anant/PMS/config/EmailConfig.java b/src/main/java/me/anant/PMS/config/EmailConfig.java new file mode 100644 index 0000000..4df224e --- /dev/null +++ b/src/main/java/me/anant/PMS/config/EmailConfig.java @@ -0,0 +1,29 @@ +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/controller/OrderController.java b/src/main/java/me/anant/PMS/controller/OrderController.java index d58451f..23fd3df 100644 --- a/src/main/java/me/anant/PMS/controller/OrderController.java +++ b/src/main/java/me/anant/PMS/controller/OrderController.java @@ -21,6 +21,7 @@ import me.anant.PMS.model.OrderProduct; import me.anant.PMS.model.Product; import me.anant.PMS.model.User; +import me.anant.PMS.service.EmailService; import me.anant.PMS.service.OrderService; import me.anant.PMS.service.ProductService; import me.anant.PMS.service.UserService; @@ -36,6 +37,9 @@ public class OrderController { @Autowired OrderService orderService; + @Autowired + EmailService emailService; + @GetMapping("/customer/order_place") public ModelAndView customerHome() { List pList = productService.get(); @@ -57,6 +61,30 @@ 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.

" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + float sum = 0; + for (OrderProduct op : opList) + { + sum = sum + op.getProduct().getProductPrice() * op.getBuyqty(); + message = message + "" + + "" + + "" + + "" + + "" + + ""; + } + message = message + "" + + "
NamePriceQtyAmount
"+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); + ModelAndView modelAndView = new ModelAndView("customer/order_place"); modelAndView.addObject("opList", opList); return modelAndView; diff --git a/src/main/java/me/anant/PMS/model/Admin.java b/src/main/java/me/anant/PMS/model/Admin.java deleted file mode 100644 index d597763..0000000 --- a/src/main/java/me/anant/PMS/model/Admin.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.anant.PMS.model; - -import javax.persistence.DiscriminatorValue; -import javax.persistence.Entity; - -@Entity -@DiscriminatorValue("A") -public class Admin extends User { - -} diff --git a/src/main/java/me/anant/PMS/model/Customer.java b/src/main/java/me/anant/PMS/model/Customer.java deleted file mode 100644 index 2140afe..0000000 --- a/src/main/java/me/anant/PMS/model/Customer.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.anant.PMS.model; - -import javax.persistence.DiscriminatorValue; -import javax.persistence.Entity; - -@Entity -@DiscriminatorValue("C") -public class Customer extends User { - -} diff --git a/src/main/java/me/anant/PMS/service/EmailService.java b/src/main/java/me/anant/PMS/service/EmailService.java new file mode 100644 index 0000000..c2399cc --- /dev/null +++ b/src/main/java/me/anant/PMS/service/EmailService.java @@ -0,0 +1,47 @@ +package me.anant.PMS.service; + +import javax.mail.internet.MimeMessage; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.mail.javamail.MimeMessagePreparator; +import org.springframework.stereotype.Service; + +import me.anant.PMS.config.EmailConfig; + +@Service +public class EmailService { + @Autowired + EmailConfig emailConfig; + + public void send(String to, String subject, String body) { + String template = "\r\n" + + "\r\n" + + " \r\n" + + "" + + "" + + "" + + "" + + body + + "

" + + "Thanks & Regards,
" + + "Team PMS" + + "" + + ""; + JavaMailSender mailSender = emailConfig.getJavaMailSender(); + 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); + } + }; + mailSender.send(preparator); + } +}