I have a Java Program that was installed on an old Ubuntu machine and sent mail using javax.mail. However, that machine went down and I am now running the same Java app in a new CentOS machine.
However, I get an error "MessagingException: 501 Syntax: HELO hostname" when trying to send an email using mail.smtp.host = 127.0.0.1.
My guess is that the mailserver is not yet activated in this CentOS.
How would I go about configuring a mail server that javax.mail can use?
Thank you
Your machine host name must be mapped in /etc/hosts file.
Example:
Console shows: linux#
and cat /etc/hostname shows
linux.mydomain.com
Then edit your hosts file running as root . vi /etc/hosts
127.0.0.1 localhost linux linux.mydomain.com
Good detailed info can be found here: https://confluence.atlassian.com/display/CONFKB/Sending+Email+Fails+Due+to+501+Syntax%3A+HELO+Hostname+Error
I encountered the same issue "MessagingException: 501 Syntax: HELO hostname" when sending out email with Spring MailSender. What works for me is to add in extra property "mail.smtp.localhost" under javaMailProperties like below:
<!-- JAVA MAIL -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="" />
<property name="port" value="25" />
<property name="protocol" value="smtp" />
<property name="username" value="" />
<property name="password" value="" />
<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>
<prop key="mail.smtp.localhost">localhost</prop>
</props>
</property>
</bean>
The problem is that the naming service on the new machine is not properly configured and Java can't find the host name of the machine. The SMTP HELO command needs to include the host name. The server is complaining because it's missing. Turn on JavaMail Session debugging and you can see the actual command this is sent. You can work around this host configuration problem by setting the JavaMail property mail.smtp.localhost to be the host name you want to use in the HELO command.
You need to run sendmail. See here for more info. Configuring sendmail can be a chore and you may want to take the configuration sendmail.cf from the old machine if possible.
I suspect (also) that you should have some central MTA (mail transport agent) set up, such that all machines in your enterprise use this, rather than running one per host. i.e. not using localhost.
In my case etc/hostname was susetest(and not linux.company.com)
Modified etc/hosts from 127.0.0.1 localhost to 127.0.0.1 localhost susetest(also after localhost make sure to use tab, when modifying the file)
Make sure to save changes, postfix stop, postfix start to restart SMTP server.
Should work fine.
(adding properties.setProperty("mail.smtp.localhost", "ourcompany.com"); to the properties also solved the problem, but workaround should not be the fix, when you can find the root cause, even if it takes days in my case)
Related
I have problems with connecting to my Ignite cluster. I have 2 nodes on AWS EC2 and I use AWS S3 based discovery. Ignites nodes start without errors and I see how new objects appear at S3 bucket after launching Ignite.
In AWS Securuty groups I opened all TCP ports so that it is expected that anyone can connect to the cluster.
But I get org.apache.ignite.client.ClientConnectionException: Ignite cluster is unavailable exception when I try to connect from my computer (I try to run simple JUnit tests from IDE).
I used the following configuration:
<property name="addressResolver">
<bean class="org.apache.ignite.configuration.BasicAddressResolver">
<constructor-arg>
<map>
<entry key="EC2 internal IP" value="EC2 public ip"/>
</map>
</constructor-arg>
</bean>
</property>
<property name="discoverySpi">
<bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
<property name="ipFinder">
<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.s3.TcpDiscoveryS3IpFinder">
<property name="awsCredentials" ref="reference to another bean with aws creds"/>
<property name="bucketName" value="MY_BUCKET_NAME"/>
</bean>
</property>
</bean>
</property>
The problem here is that server rejects any attempts to connect, because when I start Ignite client (not Thin client) I see at logs the following picture:
As you can see, the number of clients become equal to 0 then 1, 0 and 1 and etc.
So I suppose, clients can reach the cluster, but why are they being rejected?
I solved this problem. It seems there is a rule: If you use AWS S3 based discovery in your Ignite cluster, your Ignite client nodes should use the same approach as well. It wasn't obvious for me.
According to the official documentation:
TcpDiscoverySpi spi = new TcpDiscoverySpi();
BasicAWSCredentials creds = new BasicAWSCredentials("yourAccessKey", "yourSecreteKey");
TcpDiscoveryS3IpFinder ipFinder = new TcpDiscoveryS3IpFinder();
ipFinder.setAwsCredentials(creds);
ipFinder.setBucketName("yourBucketName");
When can successfully send email using Gmail but when I use Godaddy webmail I get the following:
Failed message 1: javax.mail.MessagingException: Could not connect to SMTP
host: smtpout.asia.secureserver.net, port: 465, response: -1] with root
cause
javax.mail.MessagingException: Could not connect to SMTP host:
smtpout.asia.secureserver.net, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2106) ~
[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:712) ~
[javax.mail-1.5.6.jar:1.5.6]
at javax.mail.Service.connect(Service.java:366) ~[javax.mail-
1.5.6.jar:1.5.6]
Here is my properties file
spring.mail.host = smtpout.asia.secureserver.net
spring.mail.username= xyz#domian.com
spring.mail.password= ******
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.port=465
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.timeout=60000
spring.mail.properties.mail.imap.ssl.enable=true
spring.mail.properties.mail.imap.ssl.trust=*
Here is My code
//SendMail using java Mail API
#Autowired
JavaMailSender mailSender;
public String send(String to, String Subject, String Password) throws
MessagingException{
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
helper = new MimeMessageHelper(message, true);
helper.setSubject(Subject);
helper.setTo(to);
helper.setText("Your password is:"+Password);
mailSender.send(message);
return "false";
}
This issue is not related to JAVA code, its seems to be issue on your server settings with SMTP protocol.
However I suspect you might missed some thing in your SMTP configuration in JAVA, I will suggest below pointers about it.
Configure a local SMTP server as a relay on your locally, just to
check you are on right track.
You can even configure dummy server to trace the email request.
Check your firewall settings as well, possible it might be restricted
for other SMTP server.
I had this very same problem - and was about to get crazy - when I found a working solution here in Stackoverflow, though I've lost the link, I'm sorry.
In this - legacy - project, I was using Spring 3.x. Mail was sent with org.springframework.mail.javamail.JavaMailSenderImpl and configured like this:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.server.host}" />
<property name="port" value="${mail.server.port}" />
<property name="protocol" value="${mail.server.protocol}" />
<property name="username" value="${mail.server.username}" />
<property name="password" value="${mail.server.password}" />
<property name="javaMailProperties">
<util:properties location="classpath:javamail.properties" />
</property>
</bean>
Someone suggested to use this configuration:
mail.server.protocol=smtps
mail.smtps.quitwait=false
mail.smtp.auth=true
mail.smtp.starttls.enable=true
And that worked fine.
Beware the protocol is smtps in some properties but in others is smtp.
Thakur!
I was getting the exact same error you are, but now I got a solution based on your code.
Try to add into your properties the following:
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.ssl.trust=*
Your problem is happening because GoDaddy requires SSL in their connection requests, otherwise they won't answer your request! Also, I used https://www.smtper.net to test my smtp connection and see if it worked. You can use it to check if your problem is the missing SSL too.
I'm using Spring Boot 2.2 an works with this configuration:
spring.mail.host=smtpout.secureserver.net
spring.mail.port=465
spring.mail.username=your_email#example.com
spring.mail.password=your_password
# Other properties
spring.mail.properties.mail.smtp.auth=true
# TLS, port 587
#spring.mail.properties.mail.smtp.starttls.enable=true
# SSL, port 465
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
When you use gmail the protocol is TLS generally, but GoDaddy looks like use only SSL, so I comment the TLS config and enable the SSL config.
You must set de field from when you send the email (the same in spring.mail.username) or get an error:
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("your_email#example.com");
...
Hope can help you.
PD: after google a lot I finded the solution in: https://mkyong.com/spring-boot/spring-boot-how-to-send-email-via-smtp/
I'm developing a Java/Spring/Hibernate/CXF/MySQL SOAP webservice and corresponding web Spring MVC client on my laptop. The plan is to eventually move the two resulting war-files over to my remote server hosted at mybiz.com .
Both laptop and server have an instance of MySQL version 5.x.x
Both laptop and server have a root#localhost user (duh)
The server also has three other users:
zzdb_admin#%
zzdb_admin#localhost
zzdb_admin#mybiz.com
all with the same password remotepw and which have all had assorted privileges granted and flushed.
Both instances of MySQL have a database named zzdb.
Both instances of MySQL have ##session.old_passwords, ##global.old_passwords and ##global.secure_auth set to 0; In all cases hashes of passwords are 41 characters wide.
While logged in to the remote server directly I can manually log in to all accounts on both machines
mysql --user=root --password=remoterootpw
mysql --user=zzdb_admin --password=remotepw
mysql --host=localhost --user=zzdb_admin --password=remotepw
On the laptop I can log in to the local mysql with
mysql --user=root --password=localrootpw
mysql --host=mybiz.com --user=zzdb_admin --password=remotepw
So all users and passwords are correct. And their hashes are all 41 characters. Important: note this last proves that connection can be made with the instance on the remote machine.
The webservice' pom has version 5.1.8 of mysql-connector-java.
Now it gets weird. With these lines in the webservice' properties file:
hibernate.connection.url=jdbc:mysql://localhost:3306/zzdb
hibernate.connection.username=root
hibernate.connection.password=localrootpw
the webapp can connect to the local db instance and all is peachy. But changing only these three lines to
hibernate.connection.url=jdbc:mysql://mybiz.com:3306/zzdb
hibernate.connection.username=zzdb_admin
hibernate.connection.password=remotepw
throws the dreaded "Access denied for user 'zzdb_admin'#'localhost' " error
This has got me pulling out what few hairs I have left. Doesn't look like I'm missing anything and everything is spelled correctly. Anybody have an idea of what's going on?
TIA,
Still-learning Steve
Addendum: trying a different, simpler approach WORKED!
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://mybiz.com/zzdb?user=zzdb_admin&password=remoteapw";
Connection conn = DriverManager.getConnection(connectionUrl);
Now how about that? Only change is the method of connecting. Crazy
MySQL remote link have a bind address. You should comment it. You can refer this blog.
Solved the problem, and oh what a subtle bugger it was!
The applicationContext.xml at the base of the web service uses an Atomikos bean for a data source
<bean id="dataSourceServerA" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="XADBMS_A" />
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties"> <!-- properties will be different for different data sources ie MySQL/HSDB/DB2 etc -->
<props>
<prop key="uri">$dbServerA{hibernate.connection.uri}</prop>
<prop key="user">$dbServerA{hibernate.connection.username}</prop>
<prop key="password">$dbServerA{hibernate.connection.password}</prop>
</props>
</property>
<property name="poolSize"><value>20</value></property>
<property name="testQuery" value="SELECT 1" />
which in turn gets used in the EntityManagerFactor bean. This configuration works great as long as I'm pointing to the local instance of MySQL. Changing the configuration to point to the remote instance kept failing with references to zzdb_admin#localhost no matter what I did.
So I created a second, much simpler connectivity tester using only
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://mybiz.com/zzdb?user=zzdb_admin&password=removepw";
theApp.conn = DriverManager.getConnection(connectionUrl);
and EUREKA! It worked to make a remote connection. So that got me thinking the problem had to lie with the Atomikos bean configuration.
Turns out there's one crucial xaProperty I wasn't setting - serverName, which when not explictly set defaults to - you guessed it - localhost. Atomikos isn't "smart" enough to infer the hostname out of the uri passed to it.
So merely switching to
<bean id="dataSourceServerA" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="XADBMS_A" />
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties"> <!-- properties will be different for different data sources ie MySQL/HSDB/DB2 etc -->
<props>
<prop key="serverName">mybiz.com</prop>
<prop key="port">3306</prop>
<prop key="databaseName">zzdb</prop>
<prop key="user">$dbServerA{hibernate.connection.username}</prop>
<prop key="password">$dbServerA{hibernate.connection.password}</prop>
</props>
</property>
<property name="poolSize"><value>20</value></property>
<property name="testQuery" value="SELECT 1" />
did the trick. So the problem was one of configuration. Fiendishly subtle indeed!
Thanks to all who replied!
CASE CLOSED!
Still-learning Steve
I want to export java.rmi on virtual machine,
<bean id="entityRmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="entityService"/>
<property name="service" ref="entityServiceImpl"/>
<property name="serviceInterface" value="IEntityService"/>
<property name="registryPort" value="1099"/>
<property name="registryHost" value="127.0.0.1"/>
i get connection refused to 127.0.0.1
hosts file :
127.0.0.1 localhost.localdomain localhost
10.0.2.15 compname
is this problem with vm ?
Change the networking mode of the virtual machine to "bridged" so it will be in the same network as the host. Java RMI was not designed to work with NAT, so you'll have trouble with the default networking mode.
After that, change registryHost to the IP address of the VM, 127.0.0.1 can only be accessed from within the machine.
(There are other ways to make this work but this is the easiest to explain)
I have a machine running a java app talking to a mysql instance running on the same instance. the app
uses jdbc4 drivers from mysql. I keep getting com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
at random times.
Here is the whole message.
Could not open JDBC Connection for transaction; nested exception is
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was25899 milliseconds ago.The last packet sent successfully to the server was 25899 milliseconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
For mysql, the value of global 'wait_timeout' and 'interactive_timeout' is set to 3600 seconds and 'connect_timeout' is set to 60 secs. the wait timeout value is much higher than the 26 secs(25899 msecs). mentioned in the exception trace.
I use dbcp for connection pooling and here is spring bean config for the datasource.
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx" />
<property name="poolPreparedStatements" value="false" />
<property name="maxActive" value="3" />
<property name="maxIdle" value="3" />
</bean>
Any idea why this could be happening? Will using c3p0 solve the problem ?
Try setting up the Apache Commons DBCP correctly.
You need to set:
validationQuery to SELECT 1+1
testOnBorrow to true
That should fix the problem.
Can you describe how your app is handling connection pooling? I doubt that autoReconnect=true in the JDBC driver would re-pool connections from your app. The app needs to reconnect when it loses a connection.
I'd follow the advice in the exception. You should consider either:
expiring and/or testing connection validity before use in your application,
increasing the server configured values for client timeouts, or
using the Connector/J connection property 'autoReconnect=true' to avoid this problem. Try adding that to your connection URL (consult the docs for the exact syntax) and see if it helps.
I doubt that C3P0 will be that much better than the DBCP that you're already using. The exception is giving you some specific advice. You've tried #3. What about the other two?
I know how to ask WebLogic to check connections before using them. You should find out how to do the same with Tomcat.
I have seen before that Windows machines which have been moved on the network have had trouble with connecting to themselves.
Is there any connectivity problems outside the JVM - i.e. mysql client connecting to the server, and timing out, etc?