?
Solved

Why wont this send email?

Posted on 2010-08-16
12
Medium Priority
?
634 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 6

Accepted Solution

by:
ajarvey earned 500 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 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 500 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
 
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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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