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

Posted on 2011-03-25
Last Modified: 2013-11-24
I am getting an error while sending the mail from my java application

I have attached the error document. Please help soon.
Thanks in advance.

import java.util.Properties;
import javax.activation.*;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.AddressException;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

public class Mail {
	public static void main(String[] args) {
        try {
            new Email().sendSimpleMail("Test from Java", "", "", "Testing...");

        catch(Throwable e) {

    public static class Email {
        public void sendSimpleMail(String subject, String to, String from,String messageText) throws AddressException, MessagingException {

            Properties mailProps = new Properties();
            mailProps.put("mail.transport.protocol", "smtp");
            mailProps.put("mail.smtp.starttls.enable", "true");
            mailProps.put("", "");
            mailProps.put("mail.smtp.auth", "true");
            mailProps.put("mail.smtp.user", "");
            mailProps.put("mail.debug", "true");
            mailProps.put("mail.smtp.port", "25");
            mailProps.put("mail.smtp.debug", "true");
            Authenticator auth = new MailAuthenticator();
            Session session = Session.getDefaultInstance(mailProps, auth);

            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress(from));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));


            message.setContent(messageText, "text/html");


        private class MailAuthenticator extends javax.mail.Authenticator {
            public PasswordAuthentication getPasswordAuthentication() {
                String user = "";
                String pwd = "abcd";
                return new PasswordAuthentication(user, pwd);

Open in new window

Question by:meeramt
  • 7
  • 5
LVL 92

Expert Comment

ID: 35219052
looks like the mail server isn't listening on port 25
try 465, or check what port the mail server is actually using
LVL 92

Expert Comment

ID: 35219130
587 is another to try

Author Comment

ID: 35221596
Mail properites on outlook is showing 25 as port...
Tried with both the ports but getting the same error.. :-(
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.

LVL 92

Expert Comment

ID: 35224018
does you network use a proxy

Author Comment

ID: 35225591
no.. it does not  use any proxy.. the mail server is accessed through internet only.. we do not use any proxy to connect to it..
LVL 92

Expert Comment

ID: 35225596
thats strange. Is outlook running on the same box
and do you have any firewalls

Author Comment

ID: 35225601
outlook is running on same machine n firewall is turned off..

Accepted Solution

meeramt earned 0 total points
ID: 35231016
There was a problem in my mail server so was not able to connect.. thanks a lot for the help...

Author Comment

ID: 35231049
closing d question..

Author Comment

ID: 35231519
closing this question as issue has been resolved.. thanks everyone for their time n opnion..
LVL 92

Expert Comment

ID: 35236288
> but experts confirmed that there is not problem in the code so checked the connectivity...

so award them points for helping. We have no way of knowing you have connectivity issues

Author Closing Comment

ID: 35304190
I was facing the error because of server issue at my end... but experts confirmed that there is not problem in the code so checked the connectivity...

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb on wildfly 5 30
eclipse console opening separately 2 29
Chrome and Firefox Java 5 52
passing enum to a method 4 25
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…
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

820 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