cancel
Showing results for 
Search instead for 
Did you mean: 

How to bind email config from Destination service in SCP Neo to Spring Boot application?

boudhayan-dev
Employee
Employee
0 Kudos

I want to deploy a spring boot application in SAP Cloud Platform Neo environment. It has a endpoint /sendmail which sends a mail to a particular user when called. For now, I have hard coded the credentials in application.properties file and it works. The file looks as follows -

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=...
spring.mail.password=...
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
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

Now, I want to make use of the destinations service of the SCP platform where I'll define the credentials of the mail account. I want to fetch the credentials from the destination service. So, what needs to go in application.properties file ??

I have performed similar operation in SCP - Cloud Foundry but for a different service ( Database ). CF has the VCAP environment variables which can be used to fetch the credentials as follows -

spring.datasource.url=${vcap.services.${vcap.services.name}.credentials.url}

Is it possible to do the same here in Neo ?

Regards

Accepted Solutions (0)

Answers (5)

Answers (5)

karstenvoigt
Participant

Hi,

I've used Spring Boot Mail according to https://www.baeldung.com/spring-email and configured a destination using the following parameter:

#mail.password=<< Existing password/certificate removed on export >>
#
#Thu Mar 07 14:19:07 UTC 2019 
Type=MAIL 
mail.user=***@gmail.com
mail.transport.protocol=smtp 
mail.smtp.host=smtp.gmail.com 
Name=mymail 
mail.smtp.starttls.enable=true 
mail.smtp.port=587 
mail.smtp.auth=true 

The JavaMailSender uses the properties read from the Destination. In that case it is not necessary to define a resource-ref. I've tested that approach in Neo Rot and Neo Amsterdam.

Regards

Karsten

(The approach defined in https://help.sap.com/viewer/cca91383641e40ffbe03bdc78f00f681/Cloud/en-US/e70a574cbb57101494a781920e3... did not work for me.)

Read destination information (in short):

web.xml

<!-- Declare the JNDI lookup of destination -->
<resource-ref>
<res-ref-name>connectivityConfiguration</res-ref-name>
<res-type>com.sap.core.connectivity.api.configuration.ConnectivityConfiguration</res-type>
</resource-ref>

Java (Component class in Spring '@Component')

JndiTemplate jndiTemplate = new JndiTemplate();
ConnectivityConfiguration configuration = 
     (ConnectivityConfiguration) jndiTemplate
.lookup("java:comp/env/connectivityConfiguration");
DestinationConfiguration destConfiguration = configuration.getConfiguration("mymail");
boudhayan-dev
Employee
Employee
0 Kudos

Where does the JndiTemplate code go ? I do not have a Component class as of now. Its just, Controller, Services, Repositories and Entities.

Also, in Spring boot i couldnt find the web.xml file

karstenvoigt
Participant
0 Kudos

boudhayan_dev you can but the "JndiTemplate" - Stuff to any class you like, as long as Spring boot finds the class during component scan. So I use something like this:

@Component
public class DestinationContext {
   @PostConstruct
private void initialize() {
JndiTemplate jndiTemplate = ... // see above
} ... }

The web.xml is placed in the folder /src/main/webapp/WEB-INF

Hope that helps

boudhayan-dev
Employee
Employee
0 Kudos

Hi,

The following error results -

APPLICATION FAILED TO START
***************************
Description:
Field sender in io.javabrains.Email.SendMail required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration.

The application.properties is empty. I was expecting Java to pick up the JavaMailSender properties from Destinations service during runtime. The above error is because no sprinf.mail.username.. is not defined.

karstenvoigt
Participant
0 Kudos

You should read: https://www.baeldung.com/spring-email 🙂

Have your created the MailSender like:

@Configuration 
public class JavaMailConfig {
@Bean public JavaMailSender getJavaMailSender() {
final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(..);
mailSender.setPort(...);
... return mailSender;
}
}
boudhayan-dev
Employee
Employee
0 Kudos

1. I asked about Spring boot not Spring.

2. Only if you scroll down a bit from the example you cited, you'll notice the example for spring boot.

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<login user to smtp server>
spring.mail.password=<login password to smtp server>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Here, they are hard-coding the config values. I don't think i have missed anything.

karstenvoigt
Participant
0 Kudos

So maybe you have completely different application setup?

We also used Spring Boot (and only have dependencies to org.springframework.boot in our gradle-build-scrip). To run the Spring Boot app in Neo you deployed it using a war - File? A Tomcat - web application can use web.xml. Any configuration provided by Neo - Destinations can only be retrieved using JNDI, so you have to configure JNDI resources. Alternative approach: don't use destinations (but I wouldn't recommend that) or use CloudFoundry. So why is using a web.xml not possible for you?

boudhayan-dev
Employee
Employee
0 Kudos

I have the web.xml in WEB-INF folder. Infact added all the neo sdk jar as well. But for some reason, the application fails to start. The JNDI lookup isnt able to assign the email config from destinations service to spring boot mail class.

Do you have a reference application that uses this web.xml/JNDI lookup for mail service ? Maybe I can take a look and see what my application is missing.

Also, Cloud foundry is not an option for now due to some platform restriction.

mirco_roth
Employee
Employee
0 Kudos

Hello everyone,
Is there a possibility to retrieve the destination of type: MAIL from inside the Spring Boot code (without web.xml) running on Cloud Foundry?

Would appreciate your help a lot! 🙂

Edwibalr
Explorer
0 Kudos

In addition to enabling Allow less secure apps, you might also need to navigate to https://accounts.google.com/DisplayUnlockCaptcha and click continue

0 Kudos

Hi Boudhayan ,

Yes , you should have one web.xml file under your WEB-INF folder which should have resource reference configuration something like -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"; id="WebApp_ID" version="3.0">
  <display-name>Demo</display-name>
 
  <resource-ref>
    <res-ref-name>mail/Session</res-ref-name>
    <res-type>javax.mail.session</res-type>
  </resource-ref>
</web-app>

0 Kudos

Hi Boudhayan ,

There is no concept of VCAP EV in SCP Neo like we have in CF but yes you can get declared properties of destination with ConnectivityConfiguration interface using SCP SDK for neo. But if you want to leverage real essence of Destinations in SCP then there is no need to get declared properties rather you can directly use destination of type 'mail' to send email as per your requirement. Here are two main steps that might help you to succeed -

1- Click on your Java app URL deployed on SCP Neo for email then at left hand side go to Destinations tab and create your destination of type 'mail' and provide all required config.

2- Use the destination created in your Java code using @Resource(name = "mail/<name of destination created>") and then write code to send email using javax.mail.session.

Thanks,

Manu

boudhayan-dev
Employee
Employee
0 Kudos

Hi Manu,

I created a Destination called Session ( type: Mail) and uploaded it under the JAVA application. It contains the config needed for mail. Then created a new Controller class that triggeres the sending of the mail . However , I am running into the following error when the application is deployed on Neo -

**************************
APPLICATION FAILED TO START
***************************

<Description:

A component required a bean named 'mail/Session' that could not be found.

Action:

Consider defining a bean named 'mail/Session' in your configuration.

I was following the example given here Tutorial to send Mail

In there, they mention a certain web.xml file. I couldn't find this in in my project.

Regards