Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 639
  • Last Modified:

Why wont this send email?

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
richardsimnett
Asked:
richardsimnett
  • 5
  • 3
  • 2
  • +2
4 Solutions
 
ajarveyCommented:
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
 
SushiFritoCommented:
Check if your anti-virus is blocking port 25 for this process.
0
 
richardsimnettAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
richardsimnettAuthor Commented:
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
 
CEHJCommented:
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
 
richardsimnettAuthor Commented:
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
 
ajarveyCommented:
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
 
richardsimnettAuthor Commented:
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
 
CEHJCommented:
>>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
 
CEHJCommented:
>>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
 
objectsCommented:
> 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
 
richardsimnettAuthor Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now