I have a scenario where I am to send email using our own SMTP server using Java. I have an application deployed on Tomcat 6.
- The functionality to send email works when we use GMail SMTP.
- The functionality does not work when I try using our own servers.
I get the following error
MailException occured while sending emailMail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: 172.16.16.1, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: 172.16.16.1, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out
The problem is that everything works fine using the same credentials and configuration parameters from a Perl Script.
I am on a Debain system.
I doubt there is some kind of access rights issue. I am new to Linux and have very limited knowledge.
Can anybody please help?
The Perl SCRIPT
Pasting the Perl script below for reference
#!/usr/bin/env perl
# System Modules
use strict;
use warnings;
use Mail::Sender;
my $subject = q/Testing Mail Server./;
my $smtp = q/localhost/;
my $from = 'Test <no-reply@xxxx.com>';
my $to = 'shardul@xxxx.com';
my $msg = q/Hi Testing.../;
my $auth = q{};
my $authid = q{};
my $authpwd = q{};
eval {
sendmail( $smtp, $from, $to, q{}, $subject, $msg, $auth, $authid, $authpwd );
};
if ( $@ ) {
print STDERR "Confirmation Mail Sending to $to Failed.\n";
print STDERR 'ERROR: ' . $@;
}
sub sendmail {
my ( $smtp, $from, $to, $bcc, $subject, $msg, $auth, $authid, $authpwd ) = @_;
my $sender = new Mail::Sender { smtp => $smtp, from => $from };
$sender->Body( 0, 0, 'text/html' );
my $result = $sender->MailMsg(
{
replyto => 'test@xxxxxx.com',
to => $to,
bcc => $bcc,
subject => $subject,
msg => $msg,
auth => $auth,
authid => $from,
authpwd => $authpwd,
}
);
if ( $result < 0 ) {
die "$Mail::Sender::Error\n";
}
}
You’re getting a
java.net.ConnectExceptionso the Java VM can’t connect to the mail server. It’s not really a mail problem since Java can’t see the mail server.You say:
Is the Perl script running on the same system as Java? If not it looks like network connectivity problem between the Java host and Mail server.
If the Perl script is running on the same system double check the script is doing what you think it is, perhaps using something like the
netstatcommand. If the server has multiple network interfaces check the Java VM isn’t bound to one that can’t contact the mail server.Update – Your Perl script is not using your mail server at
172.16.16.1to send mail. You can see from this line:that the script is sending mail via the mail server running on the same host as the script.
It does look like a network problem. Try running the following command:
If you have network connectivity to the mail server on port 25 you’ll see something like:
If you don’t have network connectivity you’ll see something like this:
If you do have connectivity then there must be something on the local system preventing the JVM from connecting, possibly an
iptablesrules. For example, network connectivity might be restricted by user id. Are you running the Perl script as the same user as Tomcat?