Solved

Why wont this send email?

Posted on 2010-08-16
12
619 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
ID: 33447739
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
ID: 33448066
Check if your anti-virus is blocking port 25 for this process.
0
 

Author Comment

by:richardsimnett
ID: 33448201
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
ID: 33448215
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
ID: 33448415
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
ID: 33448570
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 6

Expert Comment

by:ajarvey
ID: 33448657
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
ID: 33448682
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
ID: 33448697
>>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
ID: 33449681
>>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
ID: 33450225
> 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
ID: 33450753
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 41
json example 39 132
Eclipse Neon and jdk 1.8.0 11 138
servlet example issue 6 31
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

15 Experts available now in Live!

Get 1:1 Help Now