From 9223a375301fb49076459c4201be11a9b59cce9c Mon Sep 17 00:00:00 2001 From: Chinmay Chaudhari <33475051+chinmay9194@users.noreply.github.com> Date: Sat, 3 Oct 2020 22:04:21 +0530 Subject: [PATCH] Changes for STMP Connection in application.properties (#15) * Changes for STMP Connection in application.properties * Removed EMailConfig File and Removed COmments from EmailService * Changed Readme file for SMPT Configuration --- Readme.md | 2 +- .../java/me/anant/PMS/config/EmailConfig.java | 29 ----------- .../me/anant/PMS/service/EmailService.java | 52 ++++++++----------- src/main/resources/application.properties | 17 ++++++ 4 files changed, 40 insertions(+), 60 deletions(-) delete mode 100644 src/main/java/me/anant/PMS/config/EmailConfig.java 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 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 c2399cc..334c216 100644 --- a/src/main/java/me/anant/PMS/service/EmailService.java +++ b/src/main/java/me/anant/PMS/service/EmailService.java @@ -8,40 +8,32 @@ import org.springframework.mail.javamail.MimeMessagePreparator; import org.springframework.stereotype.Service; -import me.anant.PMS.config.EmailConfig; - @Service public class EmailService { + @Autowired - EmailConfig emailConfig; - + 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" - + "" - + ""; - 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); + + "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); } } 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