Spring - Prevent JavaMailSender from authenticating every time an email is sent - java

I am using JavaMailSender and Spring to send emails.
I instantiate the JavaMailSender load it as a property of the mailer bean and autowire the mailer bean to my controller.
I load it with the following properties:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="email#email.com" />
<property name="password" value="password" />
<property name="javaMailProperties">
<props>
<prop key="mail.debug">true</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.mime.charset">UTF-8</prop>
<prop key="mail.transport.protocol">smtp</prop>
</props>
</property>
</bean>
<bean id="mailer" class="mz.co.crunchtech.vtchill.utils.Mailer">
<property name="mailSender" ref="mailSender" />
<property name="velocityEngine" ref="velocityEngine"/>
</bean>
I pass the mailer object around on my worker threads so every once in a while i get the following error:
Exception in thread "taskExecutor-50"
org.springframework.mail.MailAuthenticationException: Authentication
failed; nested exception is javax.mail.AuthenticationFailedException:
454 4.7.0 Too many login attempts, please try again later.
n8sm185296pgd.31 - gsmtp
I went to my logs and found various LOGIN succeeded entries as well:
[root#thikiti ~]# cat /usr/share/apache-tomcat-7.0.73/logs/catalina.out | grep -i 'LOGIN succ' | wc -l
182
Is there a way to avoid re-authenticating each time an email is sent?

Related

Mail sender fails due to bad credentials

I am trying to send mail using SMTP in Java. Below are the details:
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.mail.com" />
<property name="port" value="465" />
<property name="username" value="${mail.user}" />
<property name="password" value="${mail.pwd}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.starttls.required">true</prop>
<prop key="mail.smtp.ssl.enabled">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
Error I am getting is:
535 Authentication credentials invalid
But the credentials are valid, I use the same for logging in from UI. Also, I have tried 587 and 25 port numbers, changing auth and tls values but to no avail.
SMTP settings for mail.com - https://www.lifewire.com/what-are-mail-com-smtp-settings-1170500
Note - I have tried it in both Java app and Mule 4 app
Mule 4 config:
<email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="0e79558e-d0c8-42e3-b534-6d18439fc1e0" >
<email:smtp-connection host="smtp.mail.com" user="user#mail.com" password="****" connectionTimeout="10" port="587" readTimeout="10" writeTimeout="10">
<email:properties >
<email:property key="mail.smtp.auth" value="true" />
<email:property key="mail.smtp.starttls.enable" value="true" />
<email:property key="mail.smtp.starttls.required" value="true" />
<email:property key="mail.debug" value="true" />
<email:property key="mail.smtp.ssl.enable" value="true" />
</email:properties>
</email:smtp-connection>
</email:smtp-config>
Since I have faced same in both Java and Mule, I am suspecting it's something to do with the SSL or Auth properties.

Cannot send email with Java in Test Server

I'm using Java Email Sender to send emails in Java.. And I'm using VelocityEngine to send HTML emails.
In my local computer everything is good! The emails are sent.
But when I deploy the code to the test server (which don't have a domain related, just the IP) The connection with gmail fails.
I have my email setting in spring-config.xml
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="mail.domain.com" />
<property name="port" value="25" />
<property name="username" value="mymail#domain.com" />
<property name="password" value="*xxxxxx" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">false</prop>
<prop key="mail.smtps.ssl.checkserveridentity">true</prop>
<prop key="mail.smtps.ssl.trust">*</prop>
</props>
</property>
</bean>
When I try to send the email from the server the error I get said:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: mail.domain.com, port: 25;
And then it said, that it is a timeout.. But my question is if is there any security that I need to disabled from the email server or if it's something with tomcat
Finally I was able to fix this issue using TLS security in the spring-config
So I just have to add
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtps.ssl.checkserveridentity">true</prop>
<prop key="mail.smtps.ssl.trust">*</prop>
and changed the port to 587.
Regards

Unable to access TransactionManager or UserTransaction after updating to Hibernate 5

I just updated from Hibernate 4.2.19 to Hibernate 5.1.2. Of course, little it is to say that things are not going as planned. After solving several issues (among which JOIN FETCH had to be replaced by JOIN), I now run into the next issue:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'idolConfig': Invocation of init method failed; nested exception is org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException: Unable to access TransactionManager or UserTransaction to make physical transaction delegate
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1575)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
[...]
Caused by: org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException: Unable to access TransactionManager or UserTransaction to make physical transaction delegate
at org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl.makePhysicalTransactionDelegate(JtaTransactionCoordinatorImpl.java:229)
at org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl.getTransactionDriverControl(JtaTransactionCoordinatorImpl.java:203)
at org.hibernate.engine.transaction.internal.TransactionImpl.<init>(TransactionImpl.java:36)
at org.hibernate.internal.AbstractSessionImpl.getTransaction(AbstractSessionImpl.java:313)
at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:281)
at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1326)
at org.hibernate.jpa.internal.EntityManagerImpl.internalGetSession(EntityManagerImpl.java:133)
[...]
The logs show that the JtaPlatform could not be loaded:
2016-11-14 15:34:22,853 DEBUG .o.j.EntityManagerFactoryUtils - Line {272} Opening JPA EntityManager
2016-11-14 15:34:22,973 DEBUG e.t.j.p.i.JtaPlatformInitiator - Line {42} No JtaPlatform was specified, checking resolver
2016-11-14 15:34:22,973 DEBUG e.t.j.p.i.JtaPlatformInitiator - Line {42} No JtaPlatform was specified, checking resolver
2016-11-14 15:34:22,974 DEBUG i.JtaPlatformResolverInitiator - Line {33} No JtaPlatformResolver was specified, using default [org.hibernate.engine.transaction.jta.platform.internal.StandardJtaPlatformResolver]
2016-11-14 15:34:22,974 DEBUG i.JtaPlatformResolverInitiator - Line {33} No JtaPlatformResolver was specified, using default [org.hibernate.engine.transaction.jta.platform.internal.StandardJtaPlatformResolver]
2016-11-14 15:34:22,992 DEBUG .i.StandardJtaPlatformResolver - Line {101} Could not resolve JtaPlatform, using default [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2016-11-14 15:34:22,992 DEBUG .i.StandardJtaPlatformResolver - Line {101} Could not resolve JtaPlatform, using default [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2016-11-14 15:34:22,994 DEBUG .JtaTransactionCoordinatorImpl - Line {258} JtaPlatform#retrieveTransactionManager returned null
2016-11-14 15:34:22,994 DEBUG .JtaTransactionCoordinatorImpl - Line {258} JtaPlatform#retrieveTransactionManager returned null
2016-11-14 15:34:22,995 DEBUG .JtaTransactionCoordinatorImpl - Line {223} Unable to access TransactionManager, attempting to use UserTransaction instead
2016-11-14 15:34:22,995 DEBUG .JtaTransactionCoordinatorImpl - Line {223} Unable to access TransactionManager, attempting to use UserTransaction instead
2016-11-14 15:34:22,995 DEBUG .JtaTransactionCoordinatorImpl - Line {241} JtaPlatform#retrieveUserTransaction returned null
2016-11-14 15:34:22,995 DEBUG .JtaTransactionCoordinatorImpl - Line {241} JtaPlatform#retrieveUserTransaction returned null
I do not have a persistence.xml file. Here is my spring.xml:
<bean id="entityManagerFactoryEcli"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="commonUnit" />
<property name="dataSource" ref="dataSourceEcli" />
<property name="packagesToScan"
value="org.my.common.portal.domain,eu.cec.justice.common.domain.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="javax.persistence.transactionType">jta</prop>
<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</prop>
<prop key="hibernate.current_session_context_class">org.hibernate.context.internal.JTASessionContext</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.my.common.util.ECLICommonEhCacheRegionFactory</prop>
</props>
</property>
</bean>
<bean id="entityManagerFactoryCommon"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="portalUnit" />
<property name="dataSource" ref="dataSourceCommon" />
<property name="packagesToScan"
value="eu.cec.justice.common.domain.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="javax.persistence.transactionType">jta</prop>
<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</prop>
<prop key="hibernate.current_session_context_class">org.hibernate.context.internal.JTASessionContext</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.my.common.util.ECLICommonEhCacheRegionFactory</prop>
<prop key="hibernate.ejb.cfgfile">hibernate.cfg.xml</prop>
</props>
</property>
</bean>
<bean id="dataSourceCommon" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="$${portal.common.database.jndi}" />
<property name="resourceRef" value="true" />
</bean>
<bean id="dataSourceEcli" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="$${ecli.database.jndi}" />
<property name="resourceRef" value="true" />
</bean>
<tx:jta-transaction-manager />
<tx:annotation-driven/>
EDIT:
Adding the following to the bean properties did not solve my issue (I also downgraded to Hibernate 5.0.0 but still the problem persists):
<prop key="hibernate.transaction.coordinator_class">org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl</prop>
This also didn't work:
<prop key="hibernate.transaction.coordinator_class">jta</prop>
This also deleting
<prop key="javax.persistence.transactionType">jta</prop>
but this failed as well.
As explained in the User Guide:
Hibernate tries to discover the JtaPlatform it should use through the
use of another service named
org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformResolver.
If that resolution does not work, or if you wish to provide a custom
implementation you will need to specify the
hibernate.transaction.jta.platform setting.
Try providing the underlying JTA platform via the hibernate.transaction.jta.platform configuration property.
Three days of work to find out the solution. Added the following line to spring.xml:
<prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform</prop>
Hibernate 4.x was somehow able to identify that automatically, but now you have to tell it manually which JtaPlatform you are using.

Accessing a remote JNDI in Spring

I was wondering how one would go about getting an object from a remote JNDI in Spring 3. Where do you specify the URL, how do you set it all up in a context file, etc. I have found some disperate information suggesting this is possible, but no singular source for how to do it for a JNDi that is on a different server.
You could use, for example, the JndiObjectFactoryBean class within a basic configuration like this one:
<bean id="someId" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="yourLookupNameGoesHere" />
<property name="jndiEnvironment">
<props>
<prop key="java.naming.provider.url">yourRemoteServerGoesHere:PortGoesHere</prop>
<prop key="java.naming.factory.initial">yourNamingContextFactoryGoesHere</prop>
<prop key="java.naming.factory.url.pkgs">yourPackagePrefixesGoHere</prop>
<!-- other key=values here -->
</props>
</property>
<!-- other properties here-->
</bean>
You can then specify other environment properties as needed and you can also simplify your configuration by using the Spring jee schema.
expanding on the above with an example for connecting to a remote activeMQ server in JBoss EAP7 using CAMEL Jms component.
You will need these 3 beans in your Spring XML application context:
<bean id="remoteQCF" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${factoryJndiName}" />
<property name="jndiEnvironment">
<props>
<prop key="java.naming.provider.url">http-remoting://${remoteHost}:${remotePort}</prop>
<prop key="java.naming.factory.initial">org.jboss.naming.remote.client.InitialContextFactory</prop>
<!-- other key=values here <prop key="java.naming.factory.url.pkgs">yourPackagePrefixesGoHere</prop> -->
</props>
</property>
</bean>
<bean id="remoteQCFproxy"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="remoteQCF" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<bean id="jmsremote" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory" ref="remoteQCFproxy" />
</bean>
where each ${xxx} denotes a value that you shall supply in place or with a property placeholder in your application context.
If you do not need a user and password to open a JMS queue connection, you may omit the second bean and reference directly the first bean as connectionFactory in the Camel JmsComponent bean.
The 'jmsremote' bean can then be used in CAML URI's like "jmsremote:queue:myQueue1"

Sending Mail using MailSender

I am using SimpleMailMessage and MailSender in Spring to send mail. I have configured .xml file as
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="protocol" value="smtps"></property>
<property name="username" value="userId#gmail.com" />
<property name="password" value="passward" />
<property name="endoding" value="UTF-8"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtps.auth">true</prop>
</props>
</property>
<bean id="userRegistrationService" class="UserRegistrationService">
<property name="mailSender" ref="mailSender" />
<property name="userEmailIds">
<set>
<value>abc#gmail.com</value>
<value>abc#yahoo.co.in</value>
</set>
</property>
</bean>
</bean>
but I get error :
Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required.
Learn more at
530 5.5.1 http://mail.google.com/support/bin/answer.py?answer=14257 i7sm235670pbj.90
What could be the reason?
Did you check out the configuration guide on Google's site? You could for example try to set the mail port as 587 if 465 causes problems.
protocol is written smtps it should be smtp

Categories

Resources