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
Solved

Why wont this send email?

Posted on 2010-08-16
12
622 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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 last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

860 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