Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java email error

  1. #1
    Member
    Join Date
    May 2013
    Posts
    161
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Java email error

    I am trying to send a mail message though get the following error after the "Transport.send(message)" command.

    com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
      nested exception is:
    	java.net.ConnectException: Connection refused: connect
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1984)
    	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656)
    	at javax.mail.Service.connect(Service.java:345)
    	at javax.mail.Service.connect(Service.java:226)
    	at javax.mail.Service.connect(Service.java:175)
    	at javax.mail.Transport.send0(Transport.java:253)
    	at javax.mail.Transport.send(Transport.java:124)
    	at MainGuiNotes$12.actionPerformed(MainGuiNotes.java:459)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: java.net.ConnectException: Connection refused: connect
    	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    	at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    	at java.net.PlainSocketImpl.connect(Unknown Source)
    	at java.net.SocksSocketImpl.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:301)
    	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
    	... 43 more
    com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
      nested exception is:
    	java.net.ConnectException: Connection refused: connect
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1984)
    	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656)
    	at javax.mail.Service.connect(Service.java:345)
    	at javax.mail.Service.connect(Service.java:226)
    	at javax.mail.Service.connect(Service.java:175)
    	at javax.mail.Transport.send0(Transport.java:253)
    	at javax.mail.Transport.send(Transport.java:124)
    	at MainGuiNotes$12.actionPerformed(MainGuiNotes.java:459)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: java.net.ConnectException: Connection refused: connect
    	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    	at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    	at java.net.PlainSocketImpl.connect(Unknown Source)
    	at java.net.SocksSocketImpl.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:301)
    	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
    	... 43 more

    This is my code for the mail.
                                 String to = "...@gmail.com";
     
            		 String from = "...@gmail.com";
     
            		 String host = "localhost";
     
            		 Properties properties = System.getProperties();
     
            		 properties.setProperty("mail.smtp.host", host);
     
            		 Session session = Session.getDefaultInstance(properties);
     
            		 try{
            			 MimeMessage message = new MimeMessage(session);
            			 message.setFrom(new InternetAddress(from));
            			 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            			 message.setSubject("greeting");
            			 message.setText("hello");
            			 Transport.send(message);
            			 System.out.println("success");
     
            		 }catch(Exception except){
            			 except.printStackTrace();
            		 }
    I have strictly followed the code from a tutorial so not sure where I have gone wrong.


  2. #2
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java email error

    I do believe your problem is you're trying to connect the mail 'hosting' website to your localhost rather than to something like GMAIL in this line;

    String host = "localhost";

    I could be completely wrong but at a glance that is what it looks like, like it says in the error.
    Couldn't connect to host, port: localhost, 25;

Similar Threads

  1. Bulk email sendingbulk email
    By Bra_Zee in forum Java Theory & Questions
    Replies: 0
    Last Post: March 6th, 2013, 08:06 AM
  2. Java Code Send email from email id configured in outlook
    By Hrithik in forum Java Theory & Questions
    Replies: 0
    Last Post: September 21st, 2012, 02:41 AM
  3. java email program
    By ashwidjava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2011, 04:10 AM
  4. Java problem -- sending email via JSP page
    By java_beginner in forum Java Theory & Questions
    Replies: 2
    Last Post: June 28th, 2010, 02:35 AM