Skip to content

Commit

Permalink
update email sending template
Browse files Browse the repository at this point in the history
  • Loading branch information
solomonfrank committed Jul 21, 2024
1 parent 844d7eb commit 104c2d9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<artifactId>flyway-core</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
Expand Down Expand Up @@ -139,6 +140,12 @@
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>4.1.0</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.2.2</version>
</dependency>
</dependencies>

<build>
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/example/springOAuth/service/EmailService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.example.springOAuth.service;

import java.io.File;
import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
Expand All @@ -11,6 +13,7 @@
import org.thymeleaf.context.Context;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;

Expand All @@ -23,6 +26,9 @@ public class EmailService {
@Autowired
private TemplateEngine templateEngine;

@Value("${spring.application.name}")
private String appName;

public void sendMail(String to, String subject, String body) throws MessagingException {

MimeMessage message = javaMailSender.createMimeMessage();
Expand All @@ -35,13 +41,20 @@ public void sendMail(String to, String subject, String body) throws MessagingExc
javaMailSender.send(message);
}

public void sendEmailVerificationLink(String to, String body) throws MessagingException {
public void sendEmailVerificationLink(String to, String body)
throws MessagingException, UnsupportedEncodingException {

MimeMessage message = javaMailSender.createMimeMessage();
// MimeMessage message = new MimeMessage(session);
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject("Email Verification");

// helper.setText(body);
helper.setFrom("noreply@springaouth.com");
// helper.setFrom("noreply@springaouth.com");
helper.setFrom(new InternetAddress("no_reply@springbooking.com", appName));

helper.setReplyTo(new InternetAddress("no_reply@springbooking.com"));

Context context = new Context();
context.setVariable("name", to);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.springOAuth.service;

import java.io.UnsupportedEncodingException;
import java.time.ZonedDateTime;
import java.util.UUID;

Expand Down Expand Up @@ -27,7 +28,8 @@ public class VerificationTokenService {
private UserRepository userRepository;

@Async
public void sendEmailVerification(String email, String targetUrl) throws MessagingException {
public void sendEmailVerification(String email, String targetUrl)
throws MessagingException, UnsupportedEncodingException {
var token = UUID.randomUUID().toString();
var verificationToken = VerificationToken.builder().identifier(email).token(token)
.expires(ZonedDateTime.now().plusDays(1))
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.dbcp2.driver-class-name=org.postgresql.Driver
spring.application.name=spring-oauth
spring.application.name=spring-booking
spring.security.oauth2.client.registration.google.scope=profile, email
spring.security.oauth2.client.registration.google.client-id=
spring.security.oauth2.client.registration.google.client-secret=
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=false
spring.datasource.dbcp2.driver-class-name=org.postgresql.Driver
spring.application.name=spring-oauth
spring.application.name=spring-booking
spring.security.oauth2.client.registration.google.client-id=${GOOGLE_CLIENT_ID}
spring.security.oauth2.client.registration.google.client-secret=${GOOGLE_CLIENT_SECRET}
spring.security.oauth2.client.registration.google.scope=profile, email
Expand Down
13 changes: 6 additions & 7 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@

# spring.profiles.active=dev

spring.application.name=spring-oauth
# spring.security.oauth2.client.registration.google.client-id=${GOOGLE_CLIENT_ID}
# spring.security.oauth2.client.registration.google.client-secret=${GOOGLE_CLIENT_SECRET}
# spring.security.oauth2.client.registration.google.scope=profile, email
# spring.security.oauth2.client.registration.google.redirect-uri=http://localhost:5173/oauth2/home

spring.flyway.baseline-on-migrate=true

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=
spring.mail.password=
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
# spring.mail.host=smtp.gmail.com
# spring.mail.port=587
# spring.mail.username=
# spring.mail.password=
# spring.mail.properties.mail.smtp.auth=true
# spring.mail.properties.mail.smtp.starttls.enable=true

app.oauth2.authorizedUri=http://localhost:5173/oauth2/redirect,myandroidapp://oauth2/redirect
app.oauth2.clientRedirectUri=http://localhost:5173/oauth2/redirect
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/templates/email-template.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- <!DOCTYPE html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
Expand All @@ -10,4 +10,4 @@ <h1>Hello, [[${name}]]!</h1>
<p>Regards,</p>
<p>Your Application Team</p>
</body>
</html> -->
</html>

0 comments on commit 104c2d9

Please sign in to comment.