File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes JavaMail - Port 25 blocked? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "JavaMail - Port 25 blocked?" Watch "JavaMail - Port 25 blocked?" New topic

JavaMail - Port 25 blocked?

Venkata Avasarala

Joined: Sep 26, 2005
Posts: 2
I am getting the following error when I try to send a mail using JAVAMAIL API.

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

Following is the code snippet I am using:

Properties p = new Properties();
p.put("mail.smtp.user", mailuser);
p.put("", mailhost);
p.put("mail.smtp.port", "558");
p.put("mail.smtp.debug", "true");

try {
session = Session.getDefaultInstance(p);
MimeMessage msg = new MimeMessage(session);
Address fromAddr = new InternetAddress(mailuser);
Address toAddr = new InternetAddress(_to);
msg.addRecipient(Message.RecipientType.TO, toAddr);
System.out.println("Message: " + msg.getContent());
catch (Exception mex) {// Prints all nested exceptions as well
System.out.println("I am here??? ");
// Note: will use results of getLocalHost() to fill in EHLO domain

1. I am suspecting that my DSL service (VZavenue) has bloced port 25 and hence it is throwing that error. is that correct?

2. In my code I tried to change the port and the hostname yet the error is saying localhost and port 25. Why?

3. Is there a work-around to this in case my VZAvenue is in fact blocking port 25? if not, do they unblock this port if I contact them?

Appreciate your help in resolving this problem.

Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Are you sure that a mail service is running on port 25 for your machine?

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Arun Prasath
Ranch Hand

Joined: Sep 17, 2003
Posts: 192
If you run a mail service on your "localhost", and still you get the error, you can check that by connecting to localhost using telnet on port 25.

SCJP 1.4, SCDJWS , SCJA<br />I can do ALL things through CHRIST who strengthens me.
Venkata Avasarala

Joined: Sep 26, 2005
Posts: 2
Hi there,

Not sure what you mean by whether a mail service is running on that port. I don't think it is. I ran the port trace from and it did show that port 25 is blocked.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

> Not sure what you mean by whether a mail service is running on that port.

Your program needs to connect to a mail server. The way you're doing it now, your program is trying to connect to "localhost" (your local machine). If you don't have a mail server running on your computer, then it can't connect (there is no mail server listening on port 25 on your computer).

Change the hostname to something else than "localhost" or setup a mail server on your local computer.

If you're only doing things on your local computer, your ISP has nothing to do with it.
[ September 29, 2005: Message edited by: Jesper de Jong ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: JavaMail - Port 25 blocked?
It's not a secret anymore!