Sending Mail using MailSender - java

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

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.

Hazelcast-client configuration on AWS

My question regards hazelcast-client configuration.
From what I understand, when configured properly the client is supposed to fetch hazelcast server nodes IPs automatically from the AWS api , the problem is doesn't even try connect.
Here's some log I found.
Caused by: java.lang.IllegalStateException: Unable to connect to any address in the config! The following addresses were tried: []
at com.hazelcast.client.spi.impl.ClusterListenerSupport.connectToCluster(ClusterListenerSupport.java:178)
at com.hazelcast.client.spi.impl.ClientClusterServiceImpl.start(ClientClusterServiceImpl.java:189)
also here's client conifig
<bean id="hazelcastInstance" class="com.hazelcast.client.HazelcastClient" factory-method="newHazelcastClient">
<constructor-arg>
<bean class="com.hazelcast.client.config.ClientConfig">
<property name="groupConfig">
<bean class="com.hazelcast.config.GroupConfig">
<property name="name" value="dev"/>
</bean>
</property>
<property name="properties">
<props>
<prop key="hazelcast.icmp.enabled">true</prop>
</props>
</property>
<property name="networkConfig">
<bean class="com.hazelcast.client.config.ClientNetworkConfig">
<property name="awsConfig">
<bean class="com.hazelcast.client.config.ClientAwsConfig">
<property name="insideAws" value="true" />
<property name="enabled" value="${hazelcast.aws.enabled:false}" />
<property name="region" value="${hazelcast.aws.region:set-me}" />
<property name="accessKey"value="key" />
<property name="secretKey" value="secret"/>
<property name="hostHeader" value="ec2.amazonaws.com"/>
<property name="iamRole" value="${hazelcast.aws.iam.role:#{null}}"/>
<!-- <property name="securityGroupName" value="${hazelcast.aws.securityGroupName:#{null}}" /> -->
<property name="tagKey" value="${hazelcast.aws.tagKey:hazelcast-cluster}" />
<property name="tagValue" value="${hazelcast.aws.tagValue:#{null}}" />
<property name="connectionTimeoutSeconds" value="${hazelcast.aws.connectionTimeout:15}" />
</bean>
</property>
</bean>
</property>
</bean>
</constructor-arg>
</bean>
also tried to force server ip, that works. I need the client to discover server ips automatically. any clue ?
You need to use the discovery plugin mechanism if you want the client to discover clusters on AWS (or any other cloud). The old AWS discovery was for members only.
Please see https://github.com/hazelcast/hazelcast-aws

How to disable JMS spring listener in test evnvironment

I am using spring JMSTemplate to connect to a queue on a different weblogic domain.
I wired all the components like jndiTemplate, connectionfactory, destination etc in the Spring config.
Messages are handled by a simple implementation of MessageListener.
It all works fine. But when I try to deploy this application where the destination is not available, deployment fails. this is very important for me as we dont have JMS infrastructure in some of our environments.
I tried with lookupOnStartup=false on all the components and tried injecting them into DMLC based on a environment variable. But the issue is, the listener doesnt seem to be starting up. All the messages are left in the queue.
Any help is highly appreciated.
Spring-config:
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">t3://wp2128.xyz.int:7001</prop>
<prop key="java.naming.security.authentication">none</prop>
<prop key="java.naming.security.principal"></prop>
</props>
</property>
</bean>
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean" >
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="jms/connectionFactory" />
</bean>
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="jms/testQueue" />
</bean>
<bean id="simpleConsumer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="concurrentConsumers" value="5" />
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="simpleMessageListener" />
</bean>
You can set autoStartup to false. This property value can be externalized in a properties files.
<bean id="simpleConsumer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="concurrentConsumers" value="5" />
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="simpleMessageListener" />
<property name="autoStartup" value="false"/>
</bean>

SwiftMQ URL parser cannot parse correct URL

When I try to send a message, I get the following exception. The URL seems correct and I used similar configuration before. I don't have a clue what might cause this problem...
Caused by: javax.naming.NamingException: invalid URL: protocol != smqp, URL==smqp://activemq:3001/timeout=10000
at com.swiftmq.jndi.v400.URLParser.parseURL(Unknown Source)
at com.swiftmq.jndi.InitialContextFactoryImpl.getInitialContext(Unknown Source)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.<init>(InitialContext.java:197)
at org.springframework.jndi.JndiTemplate.createInitialContext(JndiTemplate.java:136)
at org.springframework.jndi.JndiTemplate.getContext(JndiTemplate.java:103)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:85)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
at org.springframework.jndi.JndiObjectTargetSource.getTarget(JndiObjectTargetSource.java:127)
... 50 more
Configuration
<!-- JMS Connection Factory -->
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.provider.url">smqp://activemq:3001/timeout=10000</prop>
<prop key="java.naming.factory.initial">com.swiftmq.jndi.InitialContextFactoryImpl</prop>
</props>
</property>
</bean>
<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="QueueConnectionFactory" />
<property name="lookupOnStartup" value="false" />
<property name="cache" value="false" />
<property name="proxyInterface" value="javax.jms.QueueConnectionFactory" />
</bean>
<bean id="sendConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory" destroy-method="destroy">
<property name="targetConnectionFactory" ref="jmsConnectionFactory" />
<property name="reconnectOnException" value="true" />
<property name="sessionCacheSize" value="100"/>
</bean>
<!-- Template used only for sending messages -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="sendConnectionFactory" />
<property name="defaultDestinationName" value="request#z4smq_4001" />
</bean>
Usage
MessageCreator messageCreator = new MessageCreatorImpl(payload);
jmsTemplate.send(messageCreator); // setter injection
Is there a blank or any other invisible character in front of "smqp://"? In that case I get the same exception otherwise it works for me.
You should post your questions to SwiftMQ's user forum to reach the proper audience.

spring - config spring email

I used the springmail to send email from my smtp server with the following config:
<bean id="springEmailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="defaultEncoding" value="UTF-8"/>
<property name="host" value="mail.myserver.com"/>
<property name="port" value="25"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
<property name="javaMailProperties">
<value>
mail.debug=true
mail.smtp.auth=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
</value>
</property></bean>
But it throw "javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?"
I've tested this config with gmail on port 465 and it worked.
Please tell me what i've done wrong. Thank you
It looks like your SMTP server requires SSL (secure) connection. See example below of how to configure it for Gmail, which is also requires SSL. Note smtps protocol and extra properties.
<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 name="username" value="user"/>
<property name="password" value="password"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtps.auth">true</prop>
<prop key="mail.smtps.starttls.enable">true</prop>
<prop key="mail.smtps.debug">true</prop>
</props>
</property>
</bean>
Maybe you shouldn't be using SSL. Is the mail server configured to accept an encrypted message? Looks like it wants plain text.
I'd go back to the reference docs and see if that will work.
I would try removing all javaMailProperties, as well as the username and password properties.
As duffymo points out, you probably shouldn't use SSL (port 25 is the non-SSL SMTP port). Most SMTP servers also don't require authentication (unless you've explicitly configured it).

Categories

Resources