spring - config spring email - java

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).

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

password authenticated email sending in spring

How do i authenticate password for the email id.Currently it is sending mail without authentication,so how do i authenticate password for mail.username given in propert file.
below code is there in spring-servlet :
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}" />
<property name="port" value="${mail.port}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtps.auth">true</prop>
<prop key="mail.smtp.starttls.enable">false</prop>
<!-- <prop key="mail.smtp.debug">true</prop> -->
</props>
</property>
</bean>
**Mail Sending Part :**
SimpleMailMessage email = new SimpleMailMessage();
if(UserEmailId != null){
email.setFrom(userFrom);
email.setTo(UserEmailId);
email.setSubject(subject);
email.setText(MessageBody);
mailSender1.send(email);
}
**mail property file :**
#Email address/account for sending emails from system.
mail.username=<from email id>
#Password for accessing email account
mail.password=<password>
#Mail server hostname or IP
mail.host=<host>
#Mail server port
mail.port=25
#Mail transport protocol
mail.transport.protocol=smtp
#Mail authentication enabled true/false
mail.smtp.auth=true
#smtp TLS enable true/false
mail.smtp.starttls.enable=false
Set following true and try again
<prop key="mail.smtp.starttls.enable">true</prop>
Hope it helps

Sending email by Spring via Google ()

The code below works well
But, my question is how can set the 'javaMailProperties' programaticaly ?
Because I would like to set ssl/tsl from the code. I could not access these properties, I dont know why, thanks for the solution and explanation.
SimpleMailMessage message=(SimpleMailMessage)SpringUtil.getContext().
getBean("templateMessage");
JavaMailSenderImpl mailSender = (JavaMailSenderImpl)SpringUtil.getContext()
.getBean("mailSender");
mailSender.send(message);
--applicationcontext.xml--
<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="your gmail address"/>
<property name="password" value="your password"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage" >
<property name="from" value="from#gmail.com"/>
<property name="to" value="to#gmail.com"/>
<property name="subject" value="subject"/>
<property name="text" value="hello"/>
</bean>
(I dont want to use javax.mail approach, it was asked)
Use:
mailSender.getJavaMailProperties().setProperty("mail.smtp.starttls.enable", "true");
Same for other properties that you want to set programatically.
EDIT:
I checked the source code for the JavaMailSenderImpl class:
/**
* Allow Map access to the JavaMail properties of this sender,
* with the option to add or override specific entries.
* <p>Useful for specifying entries directly, for example via
* "javaMailProperties[mail.smtp.auth]".
*/
public Properties getJavaMailProperties() {
return this.javaMailProperties;
}
As you can see, the getJavaMailProperties is a public method and should be available to you. My Spring framework version is 3.0.5.
A quick google not verified.
Call setProperty(String key, String value) on the mailSender.getJavaMailProperties.setProperty("mail.smtp.auth",true)
http://download.oracle.com/javase/6/docs/api/java/util/Properties.html

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