Skip to content

Commit

Permalink
EMail
Browse files Browse the repository at this point in the history
  • Loading branch information
anantjain6 committed May 7, 2020
1 parent 68dc844 commit 10eb5db
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 76 deletions.
6 changes: 5 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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: [email protected]

Password: admin

#### Customer
Username: [email protected]

Password: user

## Screenshots
Expand Down
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<!-- Spring Context Support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>

<build>
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/me/anant/PMS/HomeController.java

This file was deleted.

41 changes: 0 additions & 41 deletions src/main/java/me/anant/PMS/ProductController.java

This file was deleted.

29 changes: 29 additions & 0 deletions src/main/java/me/anant/PMS/config/EmailConfig.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
28 changes: 28 additions & 0 deletions src/main/java/me/anant/PMS/controller/OrderController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +37,9 @@ public class OrderController {
@Autowired
OrderService orderService;

@Autowired
EmailService emailService;

@GetMapping("/customer/order_place")
public ModelAndView customerHome() {
List<Product> pList = productService.get();
Expand All @@ -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,<br><br>Your order has been placed successfuly. Following is the detail of your order.<br><br>"
+ "<table>" +
"<tr>" +
"<th>Name</th>" +
"<th>Price</th>" +
"<th>Qty</th>" +
"<th>Amount</th>" +
"</tr>";
float sum = 0;
for (OrderProduct op : opList)
{
sum = sum + op.getProduct().getProductPrice() * op.getBuyqty();
message = message + "<tr>" +
"<td>"+op.getProduct().getProductName()+"</td>" +
"<td>Rs. "+op.getProduct().getProductPrice()+"</td>" +
"<td>"+op.getBuyqty()+"</td>" +
"<td>Rs. "+op.getProduct().getProductPrice() * op.getBuyqty()+"</td>" +
"</tr>";
}
message = message + "<tr><td colspan=\"3\"><center><b>Total Amount</b></center></td><td>Rs. "+sum+"</td></tr>" +
"</table>";
emailService.send(principal.getName(), "Order Placed successfully", message);

ModelAndView modelAndView = new ModelAndView("customer/order_place");
modelAndView.addObject("opList", opList);
return modelAndView;
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/me/anant/PMS/model/Admin.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/me/anant/PMS/model/Customer.java

This file was deleted.

47 changes: 47 additions & 0 deletions src/main/java/me/anant/PMS/service/EmailService.java
Original file line number Diff line number Diff line change
@@ -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 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n" +
"<html>\r\n" +
" <head>\r\n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
+ "<style>"
+ "table {width: 100%;border-collapse: collapse;}"
+ "table, th, td {border: 1px solid black; padding: 5px}"
+ "</style>"
+ "</head>"
+ "<body>"
+ body
+ "<br><br>"
+ "Thanks & Regards,<br>"
+ "Team PMS"
+ "</body>"
+ "</html>";
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("[email protected]");
message.setSubject(subject);
message.setText(template, true);
}
};
mailSender.send(preparator);
}
}

0 comments on commit 10eb5db

Please sign in to comment.