This discussion is archived
7 Replies Latest reply: Nov 23, 2008 12:52 PM by 843785 RSS

javamail could not connect to SMTP Server

843785 Newbie
Currently Being Moderated
Hi, I was bored and decided to create a small email application.
I tryed to connect to my ISP's SMTP Server but I get the following MessagingException
I don't know but I think my problem lies within my Authenticator class.
Please tell me if I am correct and how I would fix it.
create email -      true
send   email -      false
javax.mail.MessagingException: Could not connect to SMTP host: smtp.telkomsa.net, port: 25, response: -1
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1379)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
        at javax.mail.Service.connect(Service.java:288)
        at javax.mail.Service.connect(Service.java:169)
        at javax.mail.Service.connect(Service.java:118)
        at javax.mail.Transport.send0(Transport.java:188)
        at javax.mail.Transport.send(Transport.java:118)
        at EMAIL.Email.send(Email.java:101)
        at ENTRY.Start.main(Start.java:15)
here is part of my code:
    public boolean createEmail(){
        try{
            props.setProperty( "mail.smtp.host", AddressBook.getHost() );
            session = Session.getDefaultInstance( props,(Authenticator)new MyAuthenticator());
            message = new MimeMessage( session );

            message.setText( text );
            message.setSubject( subject );
            if( AddressBook.containsEntry( from ) ){
                message.setFrom( new InternetAddress( AddressBook.getAddress( from ) ) );
            } else {
                if( AddressBook.isValidEmailAddress( from ) ){
                    message.setFrom( new InternetAddress( from ) );
                } else {
                    throw new Exception( "\"From\" address not valid" );
                }
            }
            for( String recipient : to ){
                message.addRecipient( Message.RecipientType.TO, new InternetAddress( recipient ) );
            }
            for( String recipient : cc ){
                message.addRecipient( Message.RecipientType.CC, new InternetAddress( recipient ) );
            }
            for( String recipient : bcc ){
                message.addRecipient( Message.RecipientType.BCC, new InternetAddress( recipient ) );
            }
            return true;
        }catch( Exception ex ){
            ex.printStackTrace();
            return false;
        }
        return true;
    }
    public boolean send(){
        try{
            Transport.send( message );
            return true;
        }catch( Exception e ){
            e.printStackTrace();
            return false;
        }
    }
Authenticator class
import javax.mail.Authenticator;
public class MyAuthenticator extends Authenticator{
    @Override
    protected PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication( "onlinexxxxxx@dsl.2gbsh.telkomsa.net", "xxxxxxxx" );
    }
}
  • 1. Re: javamail could not connect to SMTP Server
    800308 Newbie
    Currently Being Moderated
    reaper,

    Your problem is
    send email - false
    Right?

    You posted createEmail()

    Ya putz ;-)
  • 2. Re: javamail could not connect to SMTP Server
    843785 Newbie
    Currently Being Moderated
    Haha, love your sense of humour as always.
    Those 2 lines came from the below snippet:
    System.out.println("create email -\t" + email.createEmail());
    System.out.println("send   email -\t" + email.send());
    Look a little lower in my code you will see the send() method. sorry my fault for not checking the indent
  • 3. Re: javamail could not connect to SMTP Server
    800308 Newbie
    Currently Being Moderated
    I was actually referring to Transport.send( message ); Doh!

    ... and I get exactly the same error without making any attempt to authentice.
    // http://www.javacommerce.com/displaypage.jsp?name=javamail.sql&id=18274
    
    package krc.utilz;
    
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;
    
    public class Emailz
    {
    
      private static final Properties properties = new Properties();
      static {
        properties.put("mail.smtp.host", "smtp.googlemail.com");
      }
    
      public static void send(String subject, String message, String from, String... recipients)
        throws MessagingException
      {
        Session session = Session.getDefaultInstance(properties, null);
        session.setDebug(false);
    
        Message msg = new MimeMessage(session);
    
        msg.setFrom(new InternetAddress(from));
    
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i=0; i<recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients);
    }

    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // you can set custom headers in the email if you want
    // msg.addHeader("MyHeaderName", "myHeaderValue");

    msg.setSubject(subject);
    msg.setContent(message, "text/plain");

    Transport.send(msg);
    }

    public static void main(String[] args) {
    try {
    send("Testing", "Testing testing 123", "________@gmail.com", "________@gmail.com");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    }



    produces
    C:\Java\home\src\krc\utilz>"C:\Program Files\Java\jdk1.6.0_07\bin\java.exe" -Xms512m -Xmx1024m -enableassertions -cp C:\Java\home\cl
    asses;C:\Programs\glassfish-v2ur2\lib\mail.jar krc.utilz.Emailz
    javax.mail.MessagingException: Could not connect to SMTP host: smtp.googlemail.com, port: 25, response: -1
            at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1379)
            at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
            at javax.mail.Service.connect(Service.java:288)
            at javax.mail.Service.connect(Service.java:169)
            at javax.mail.Service.connect(Service.java:118)
            at javax.mail.Transport.send0(Transport.java:188)
            at javax.mail.Transport.send(Transport.java:118)
            at krc.utilz.Emailz.send(Emailz.java:38)
            at krc.utilz.Emailz.main(Emailz.java:43)
    Press any key to continue . . .
  • 4. Re: javamail could not connect to SMTP Server
    800308 Newbie
    Currently Being Moderated
    I managed to piece it together from my settings in thunderbird and [this google groups post|http://groups.google.com/group/Gmail-Help-POP-and-IMAP-en/browse_thread/thread/8b93bd247581d556/ee7baf764267576d?q=smtp&pli=1]
      private static final Properties props = new Properties();
      static {
        props.put("mail.smtp.host", "smtp.googlemail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.user", "[account name only]"); <<<< fill this is
        //props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.debug", "true"); <<<< turn this off for production
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false"); 
      }
    Run it with debug turned on to start with... It tells you quite a lot about what it's doing.
  • 5. Re: javamail could not connect to SMTP Server
    843785 Newbie
    Currently Being Moderated
    Hi Keith,
    I found why I ended up getting that connection error:
    I got the SMTP address from this page SMTPServerList
    After doing some digging around I found this out:
     
    smtp.telkomsa.net ;    -> Outgoing for Dial-up & SpaceStream (SMTP) Server 
    smtp.dsl.telkomsa.net -> Outgoing for DSL (SMTP) Server 
    Looks like the whole Putz thing is was relevant, you psychic you.

    Anyway after changing the host address it connects and says that the message was sent although it does not go through.
    I thought it might be a SPAM Filter that is blocking the email (not likely) so I disabled all spam filters.
    Still with no luck, and yes the email addresses are correct (copy-past).
    Any ideas as to why?
    ---------
    O and I found another method of authentication, but you probably already know this:
     
    Transport transport = session.getTransport("smtp"); 
    transport.connect(host, username, password); 
    transport.sendMessage(message, message.getAllRecipients()); 
    transport.close(); 
  • 6. Re: javamail could not connect to SMTP Server
    800308 Newbie
    Currently Being Moderated
    O and I found another method of authentication, but you probably already know this:
    Yeah I've since spotted transport.connect(host, username, password); in the API doco... but I'd never heard of it when I posted... so you tell me who's pshychic.
    Any ideas as to why?
    Sorry I don't know why your mail isn't getting through... mine is... but can I suggest you check for delivery via a webmail interface if you have one (obviating all spam filters)... and if you don't then get a gmail or a yahoo email account.

    As a matter of fact get a gmail acount anyways... if your code works with gmail then at-least you'll know your problem is specific to your telkomsa email service... and an extra email account can be useful besides... even if it is only a spam attractor.

    Cheers. Keith.
  • 7. Re: javamail could not connect to SMTP Server
    843785 Newbie
    Currently Being Moderated
    I have muiltiple for gmail, telkom (My ISP) and work.
    When I try it through gmail it worx but my ISP's smtp host still bounces the message:
    -------------
    Email recieved from MAILER-DAEMON@telkomsa.net
    MAILER-DAEMON@telkomsa.net to me 
    show details 10:31 PM (10 minutes ago) Reply
    
    
    Hi. This is the qmail-send program at telkomsa.net.
    I'm afraid I wasn't able to deliver your message to the following addresses.
    This is a permanent error; I've given up. Sorry it didn't work out.
    Your message was bounced by our server.
           If you have any other queries please contact our support:
    TelkomInternet Support Desk Number: 10215
    TelkomInternet Support Desk Mail: support@telkomsa.net
    
    <xxxx_xxxxxxxx@telkomsa.net>:
    Warning: undefined mail delivery mode: normal (ignored).
    The users mailfolder is over the allowed quota (size). (#5.2.2)
    
    --- Below this line is a copy of the message.
    
    Return-Path: <xxxx.xx.xx.xxxxxxxxx@gmail.com>
    Received: (qmail 18884 invoked from network); 23 Nov 2008 20:31:06 -0000
    Received: from unknown (HELO ironport1.telkomsa.net) ([xxx.xx.xxx.xxx])
             (envelope-sender <xxxx.xx.xx.xxxxxxxxx@gmail.com>)
             by O (qmail-ldap-1.03) with SMTP
             for <xxxx_xxxxxxxx@telkomsa.net>; 23 Nov 2008 20:31:06 -0000
    X-IronPort-Anti-Spam-Filtered: true
    X-IronPort-Anti-Spam-Result: ArwAANpLKUlIDszicWdsb2JhbACSSlQ+AQwKCQkPBa47gQKJfAEDAQOCeQ
    Received: from qb-out-0506.google.com ([72.14.204.226])
     by ironport1.telkomsa.net with ESMTP; 23 Nov 2008 22:31:01 +0200
    Received: by qb-out-0506.google.com with SMTP id f29so1924750qba.33
           for <xxxx_xxxxxxxx@telkomsa.net>; Sun, 23 Nov 2008 12:30:59 -0800 (PST)
    DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
           d=gmail.com; s=gamma;
           h=domainkey-signature:received:received:cc:message-id:subject
            :mime-version:content-type:content-transfer-encoding:from:date;
           bh=ip5PW6igBRoELerpvPl2DF77VNe5JC4vopdDoV6lpjY=;
           b=TU+Y61W432FfMIYvEmhD2r6eBDdrure+Ax+U4FIi7El3vHt2AES+mb3I2qsvvhyiAy
            CJhqO7FzQakkIkRGIWJOf4ghjrk6e6K1XIBvprPRSABVF/TvqpyFKaVc3tXJ7isW13Nh
            9Ad0Zn6/2dYjM+awXmEnzeDut5l9JB7kOApDw=
    DomainKey-Signature: a=rsa-sha1; c=nofws;
           d=gmail.com; s=gamma;
           h=cc:message-id:subject:mime-version:content-type
            :content-transfer-encoding:from:date;
           b=S8/unLzvAR7/+c1KB3PGimhO4StOsIE0r2S39oEaYLZI8jCP5F/vJg5wc/LfK2gsi3
            KypsThXRKgcjREtjmQPltOdqZhgwf+QJXW/Tg6zFaMRswgJFqig8aeY+uFddCaYTrxoc
            EtYglspCmhfJfUleh7ioCoW2w8G71rcLcN1qA=
    Received: by 10.64.184.18 with SMTP id h18mr2648302qbf.27.1227472259580;
           Sun, 23 Nov 2008 12:30:59 -0800 (PST)
    Return-Path: <xxxx.xx.xx.xxxxxxxxx@gmail.com>
    Received: from 5200am2x64bit ([41.246.118.18])
           by mx.google.com with ESMTPS id p27sm3343039qbp.16.2008.11.23.12.30.57
           (version=SSLv3 cipher=RC4-MD5);
           Sun, 23 Nov 2008 12:30:58 -0800 (PST)
    Cc: xxxx_xxxxxxxx@xxxxxxx.com
    Message-ID: <8694729.0.1227472251250.JavaMail.NEIL@5200am2x64bit>
    Subject: My First Email
    MIME-Version: 1.0
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit
    From: xxxx.xx.xx.xxxxxxxxx@gmail.com
    Date: Sun, 23 Nov 2008 12:30:58 -0800 (PST)
    
    - Show quoted text -
    
    Hello this is a email