Solved

Why wont this send email?

Posted on 2010-08-16
12
616 Views
Last Modified: 2012-05-10
Hello,
Im working on a tag for glassfish v2.1 which simply sends a basic email message. Everything appears to be correct, however I keep getting this message when I try to test the code:

javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
      java.net.ConnectException: Connection refused

I have sendmail configured to accept mail from 127.0.0.1, and have tested it using telnet 127.0.0.1 25, it accepts the connections and processes mail perfectly.

Can someone tell me what Im doing wrong? It must be something with my code since the sendmail verification works.

Worth 500 points.

NOTE: I have tried inserting 127.0.0.1 instead of localhost in the mail.smtp.host property, same error occurs. (I tried this to see if it couldnt resolve localhost to 127.0.0.1 for some reason).

Thanks,
Rick

<%-- 

    Document   : sendemail

    Created on : Aug 15, 2010, 11:48:29 PM

    Author     : rick

--%>

<%@tag import="javax.mail.internet.*" %>

<%@tag import="javax.mail.*" %>

<%@tag import="javax.mail.Transport.*" %>

<%@tag import="java.util.*" %>

<%@tag import="java.io.*" %>

<%@tag import="com.sun.mail.smtp.*" %>



<%@tag description="send email" pageEncoding="UTF-8"%>



<%-- The list of normal or fragment attributes can be specified here: --%>

<%@attribute name="message" required="true"%>

<%@attribute name="subject" required="true"%>

<%@attribute name="to" required="true"%>

<%@attribute name="from" required="true"%>



<%-- send emails --%>

<%

	Properties props = System.getProperties();

        props.put("mail.smtp.host","localhost");

		//props.put("mail.smtp.auth", "true");

	//props.put("mail.smtp.starttls.enable", "true");

        Session mailsession = Session.getDefaultInstance(props);

	mailsession.setDebug(false);





		// create a message

	MimeMessage msg = new MimeMessage(mailsession);



	InternetAddress fromAddress = new InternetAddress(from);

	msg.setFrom(fromAddress);



	InternetAddress[] toAddresses = InternetAddress.parse(to);

	msg.setRecipients(Message.RecipientType.TO, toAddresses);



	msg.setSubject(subject);

	msg.setHeader("X-Mailer", "smtpsend");

	msg.setSentDate(new java.util.Date());

	msg.setText(message);



	SMTPTransport t = (SMTPTransport) mailsession.getTransport("smtp");



        try {

                t.connect();

                t.sendMessage(msg, msg.getAllRecipients());

        }

        finally {

                System.out.println("Response: " + t.getLastServerResponse());

                t.close();

        }

%>

Open in new window

0
Comment
Question by:richardsimnett
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 6

Accepted Solution

by:
ajarvey earned 125 total points
Comment Utility
Check your sendmail logs and see what IP address the connection is coming from. Allow relaying for that IP. I know testing a telnet to localhost works, but your script may be going out on a different IP address than the standard telnet.
0
 
LVL 2

Assisted Solution

by:SushiFrito
SushiFrito earned 125 total points
Comment Utility
Check if your anti-virus is blocking port 25 for this process.
0
 

Author Comment

by:richardsimnett
Comment Utility
ajarvey,
Ok I created an access control which allows ALL mail from 192.168.1/24. I have tested this from another machine on the lan, and it accepts connections from anything in that range for relay. However, I STILL get the same error,

javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
      java.net.ConnectException: Connection refused

ITs so weird.

Thanks,
Rick
0
 

Author Comment

by:richardsimnett
Comment Utility
SushiFrito,
I am running on a linux server. There isnt any antivirus installed, there was ufw, and iptables, but those services and apparmor have been removed from the system. Its wide open, and relays mail from other systems, just for some reason glassfish (javamail however you want to look at it) refuses to send mail.

Im baffled at this point.

Thanks,
Rick
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
Comment Utility
Try temporarily removing the following line from the domain.xml file and see if it makes any difference after restarting the server

-Djava.security.policy=${com.sun.aas.instanceRoot}/config/server.policy
0
 

Author Comment

by:richardsimnett
Comment Utility
CEHJ,
Ok I have removed that line from the domain.xml for the application, and then restarted glassfish. I am still getting the same error.

Something that I did just notice after reviewing the mail.log.... java isnt even attempting a connection to sendmail. When I connect from remote, it logs the connection attempt even though mail isnt set, same with localhost, but when I try to run the java code to connect, it logs nothing: indicating java isnt trying to connect to the server specified in the smtp.mail.host.... weird.


Thanks,
Rick
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 6

Expert Comment

by:ajarvey
Comment Utility
Run a netstat and see where Java is trying to connect to.. Maybe you have a global setting in place for a default mail host, and that's where it's trying to relay off of.
0
 

Author Comment

by:richardsimnett
Comment Utility
Ok. I got it to work. Instead of specifying the localhost, or 127.0.0.1, I changed the mail.smtp.host to its public interface (192.168.1.100), and it worked. Go figure.

Anyone have thoughts on this?

Thanks,
Rick
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Ok I have removed that line from the domain.xml for the application, and then restarted glassfish. I am still getting the same error.

OK - make sure you put it back again

Put javamail into debug mode - you'll probably find it's trying to connect to a different host
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>I changed the mail.smtp.host to its public interface (192.168.1.100), and it worked.

That's not a public interface - it's a private address. The socket is bound to that address, not the loopback address. That's why it worked
0
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
Comment Utility
> The socket is bound to that address, not the loopback address. That's why it worked

telnet would not have worked if that was the case
0
 

Author Comment

by:richardsimnett
Comment Utility
Not sure how to award points on this. So IM just gonna split it up between the responders. Hope thats ok with everyone.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now