Cannot send email with Java in Test Server - java

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

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.

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

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?

How to configure Spring JavaMailSenderImpl for hotmail

this is my XML configuration file in a maven project, I am trying to send an auto-generated message using a Hotmail id, sometimes it's working fine and sometimes I am getting an error sending mail
<bean id=" email" class=" org.springframework.mail.SimpleMailMessage"></bean>
<bean class="org.springframework.mail.javamail.JavaMailSenderImpl" id="mailSender">
<property value="smtp.live.com" name="host"/>
<property value="587" name="port"/>
<property value="xxxxxx#hotmail.com" name="username"/>
<property value="xxxxxxxxx" name="password"/>
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.port">465</prop>
</props>
</property>
</bean>
after changing port name 587 to 25 it's working fine

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

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