Reputation: 313
I am trying to create a dynamic link in an email which am sending out using Spring Jpa. I created a variable in my application.properties file so that whenever I am pushing to server I can easily change that variable from localhost to the domain name for live deployment. However, when I try to pass the domain name value, the link becomes inactive. I would appreciate if you could point me in the right direction.
Here is the application.properties snippet below:
server.port=8008
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://41.207.248.189:xxxx/xxx
spring.datasource.username=****
spring.datasource.password=****
##GMAIL PROPERTIES
senderEmail = xxxxx
spring.mail.host=xxxx
spring.mail.port=xxx
spring.mail.username=xxxxxx
spring.mail.password=xxxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.trust=xxxxxxx
domain-name=localhost:4200
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
Here is the snippet for the a href I need to pass the domain-name into:
"\n" +
"<a href='{domain-name}/payments/"+hashlink+"' target='_blank'>Click Here</a>"+
"\n" +
This approach above is not working but when I pass the link manually, the "click here" link works:
"<a href='http://127.0.0.1:4200/payments/"+hashlink+"' target='_blank'>Click Here</a>"+
I would appreciate your help. Thanks
UPDATE
Below is the html for the email body where I am setting the domain-name
try {
emailService.sendEmail("PAYMENT NOTIFICATION",
"<p>Dear sir/ma,</p>\n" +
"\n" +
"\n" +
"<h4>LETTER OF NOTICE </h4>\n" +
"\n" +
"\n" +
"<p>This is to notify you of the status of your organisation ..... </p>\n" +
"<p>You are hereby put on notice to remit the outstanding amount on or before ....</p>\n" +
"<p>Kindly follow the link below to make....\n</p>\n" +
"\n" +
"<a href='{domain-name}/payments/"+hashlink+"' target='_blank'>Click Here</a>"+
"\n" +
"\n" +
"<p>Thank you.</p> \n" +
"\n"
, org.getEmail());
} catch (
MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Email Service error");
e.printStackTrace();
}
}
I hope this helps... I look forward to getting a solution. Thanks.
Upvotes: 0
Views: 968
Reputation: 313
Based on answers by @gtiwari333... I made some corrections to my code. I decided to detail the changes I made for future readers.
First of all, I added http:// to the parameter in my application.properties, as below:
domain-name=http://localhost:4200
next, I added @Value annotation to the top of my class as below:
@Value("${domain-name}")
String domainName;
Then I was able to pass it into the a href link with ease as below:
"<p>Kindly follow the link below to make....\n</p>\n" +
"\n" +
"<a href='" +domainName+ "/payments/payment-make" + link + "' target='_blank'>Click Here</a>" +
"\n" +
"\n" +
"<p>Thank you.</p> \n" +
"\n"
Thanks. I give credit to @gtiwari333 for helping me with this.
Upvotes: 1
Reputation: 25146
Based on the comments/discussion here's what you need to do:
Read/Inject the property to a String variable using @Value
@Value("${domain-name}") String domainName;
Use the variable to construct your href
Full code:
application.properties
domain-name=www.abc.com
DomainNameApp:
package domainname;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
public class DomainNameApp {
public static void main(String[] args) {
SpringApplication.run(DomainNameApp.class, args);
}
}
@RestController
class Ctrl {
@Value("${domain-name}")
String domainName; // or use constructor injection
@GetMapping("a")
void a() {
String hashlink = "jsdklfsdklflsdf";
String html = "<p>" +
"<a href='" + domainName + "/payments/" + hashlink + "' target='_blank'>Click Here</a>" +
"</p>";
System.out.println(html);
}
}
Upvotes: 1