Results 1 to 16 of 16
  1. #1
    Poyntz's Avatar
    Poyntz is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default Java Mail "SMTP host" Exception

    Using packages: javax.mail, javax.mail.internet
    When the submit button the program lags and my internet lags for about two minutes
    Then I get the following runtime exception from a printStackTrace:

    javax.mail.MessagingException: Could not connect to SMTP host: smtp.unimelb.edu.au, port: 25;
    nested exception is:
    java.net.ConnectException: Connection timed out
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTra nsport.java:1391)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SM TPTransport.java:412)
    at javax.mail.Service.connect(Service.java:310)
    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 MailMngr.sendMail(MailMngr.java:118)
    at MailMngr$SubmitListener.actionPerformed(MailMngr.j ava:128)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6134)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3265)
    at java.awt.Component.processEvent(Component.java:589 9)
    at java.awt.Container.processEvent(Container.java:202 3)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4501)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2081)
    at java.awt.Component.dispatchEvent(Component.java:43 31)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4301)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3965)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3895)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2067)
    at java.awt.Window.dispatchEventImpl(Window.java:2458 )
    at java.awt.Component.dispatchEvent(Component.java:43 31)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Caused by: java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at com.sun.mail.util.SocketFetcher.createSocket(Socke tFetcher.java:233)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFe tcher.java:189)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTra nsport.java:1359)

    I don't understand. The SMTP host I used is correct. Why am I getting this error?

    Would you like me to pastebin the code or does the stack dump provide enough information?

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    If the "smtp.unimelb.edu.au, port: 25;" is correct , paste your code .. or try to run your program from another network ..
    To check your code .. you can even try other server ....
    dont worry newbie, we got you covered.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems you have post the thread in wrong place lol. For me this is more related to networking. I'll move there.

  4. #4
    Poyntz's Avatar
    Poyntz is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by roots View Post
    or try to run your program from another network ..
    To check your code .. you can even try other server ....
    I'm trying with gmail now. Even changed the port to the gmail smtp port. Still not luck.

    Here's my code:

    import javax.swing.*;
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.mail.*;
    import javax.mail.internet.*;

    public class MailMngr extends JPanel
    {
    //Declarations
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_PORT = 995;
    private static final String SMTP_AUTH_USER = "xxxxxxxxxxxxxxxx";
    private static final String SMTP_AUTH_PWD = "xxxxxxx";
    private static final String subject = "";
    private static final String fromAddress = "Chris Poynton <xxxxxx@gmail.com>";
    private static final String toAddress = "MailServ <xxxxxxxxxxxxxxxx>";


    JFrame frame = new JFrame("MUDC Mail Manager");
    JPanel tainer1 = new JPanel();
    JPanel tainer2 = new JPanel();
    JPanel tainer3 = new JPanel();
    JLabel mailListLbl = new JLabel("Which mailing list?");
    JLabel subscribesLbl = new JLabel("Enter subscribes here:");
    JLabel unsubscribesLbl = new JLabel("Enter unsubscribes here:");
    JList mailList = new JList();
    JTextField subscribesTF = new JTextField();
    JTextField unsubscribesTF = new JTextField();
    JButton submit = new JButton("Submit");
    String subscribes = subscribesTF.getText();
    String unsubscribes = unsubscribesTF.getText();
    String listValue = "mudc-members";
    String message = "";

    //Constructor
    public MailMngr()
    {
    setSize(250,500);
    setVisible(true);
    }

    //Display
    public void showGUI()
    {
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    tainer1.setLayout(new BorderLayout());
    tainer2.setLayout(new BorderLayout());
    tainer3.setLayout(new BorderLayout());
    String[] options = {"mudc-com","mudc-members"};
    mailList.setListData(options);
    mailList.addListSelectionListener(new ListListener());
    submit.addActionListener(new SubmitListener());

    //Layout
    frame.add(tainer1, BorderLayout.WEST);
    frame.add(tainer2, BorderLayout.EAST);
    frame.add(tainer3, BorderLayout.SOUTH);
    tainer1.add(mailListLbl, BorderLayout.NORTH);
    tainer1.add(subscribesLbl, BorderLayout.CENTER);
    tainer1.add(unsubscribesLbl, BorderLayout.SOUTH);
    tainer2.add(mailList, BorderLayout.NORTH);
    tainer2.add(subscribesTF, BorderLayout.CENTER);
    tainer2.add(unsubscribesTF, BorderLayout.SOUTH);
    tainer3.add(submit, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }

    private String messageReformat(String message)
    {
    int i = 0;
    this.message = message;
    subscribes.replaceAll(";","/n subscribe "+listValue+" ");
    unsubscribes.replaceAll(";","/n unsubscribe "+listValue+" ");
    message = subscribes+unsubscribes;

    if (i == message.length()-1)
    {
    message += "/n exit";
    }
    return message;
    }

    public static void main(String[] args)
    {
    MailMngr smtpmailer = new MailMngr();
    smtpmailer.showGUI();
    }

    public void sendMail(String t, String s, String m, String f) throws MessagingException
    {
    //Email
    Properties prop = new Properties();
    Authenticator auth = new SMTPAuthenticator();
    Session sesh = Session.getDefaultInstance(prop, auth);
    Message msg = new MimeMessage(sesh);
    InternetAddress addressFrom = new InternetAddress(f);
    InternetAddress addressTo = new InternetAddress(t);

    prop.put("mail.smtp.host", SMTP_HOST_NAME);
    prop.put("mail.smtp.auth", "true");
    prop.put("mail.smtp.port", Integer.toString(SMTP_PORT));
    sesh.setDebug(false);

    msg.setFrom(addressFrom);
    msg.setRecipient(Message.RecipientType.TO, addressTo);
    msg.setSubject(subject);
    msg.setContent(this.messageReformat(message), "text/plain");
    Transport.send(msg);
    }

    class SubmitListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    //System.out.print("This is reading");
    MailMngr smtpmailer = new MailMngr();
    try {
    smtpmailer.sendMail(toAddress, subject, message, fromAddress);
    }
    catch (Exception er)
    {
    er.printStackTrace();
    }
    }
    }

    class ListListener implements ListSelectionListener
    {
    public void valueChanged(ListSelectionEvent e)
    {
    if (e.getValueIsAdjusting()==false)
    {
    if (mailList.getSelectedIndex()==0)
    {
    listValue = "mudc-com";
    }
    }
    }
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator
    {
    public PasswordAuthentication getPasswordAuthentication()
    {
    String username = SMTP_AUTH_USER;
    String password = SMTP_AUTH_PWD;
    return new PasswordAuthentication(username, password);
    }
    }
    }

  5. #5
    Poyntz's Avatar
    Poyntz is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Please ignore logical errors, I should be able to figure these out for myself when I get the code sending emails. I know for example, that in one of my methods I forgot to add a loop, but I'll worry about that.

  6. #6
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Case of gmail is different, you have to deal with SSL as well.

    JavaMail - Javamail using Gmail SMTP 465 port!

    If you cant get SMTP server try installing james in local machine or other computer in the network. As far i can guess, the problem is with your SMTP Server.

    Checked its firewall ? checked with other application like thunderbird, outlook to send an email from that server ?
    dont worry newbie, we got you covered.

  7. #7
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    First, debug it without java. With nearly all O/S, get a shell and enter:

    telnet smtp.unimelb.edu.au 25

    If you get a "connected" message, you can enter "quit" and a carriage return and know that you have the right host.

    If you get any error, fix it before you worry about your Java code

    For example, if I enter that string, from my laptop in Virginia (USA) I get the connection message

  8. #8
    Poyntz's Avatar
    Poyntz is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by fishtoprecords View Post
    If you get a "connected" message, you can enter "quit" and a carriage return and know that you have the right host.
    I get the connected message when I do what you said. But it still won't find the host in java even with the right port. I additionally tried to get SSL integrated because gmail requires SSL but had little luck.

  9. #9
    Poyntz's Avatar
    Poyntz is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Sorry. Forgot to include this. Here's my method with an attempt at getting SSL to work. Please tell me if it's incorrect:

    public void sendMail(String t, String s, String m, String f) throws MessagingException
    {
    //Email
    Properties prop = new Properties();
    Authenticator auth = new SMTPAuthenticator();
    Session sesh = Session.getDefaultInstance(prop, auth);
    Message msg = new MimeMessage(sesh);
    InternetAddress addressFrom = new InternetAddress(f);
    InternetAddress addressTo = new InternetAddress(t);

    prop.put("mail.smtp.host", SMTP_HOST_NAME);
    prop.put("mail.smtp.auth", "true");
    prop.put("mail.smtp.port", Integer.toString(SMTP_PORT));
    prop.put("mail.smtp.socketFactory.port","465");
    prop.put("mail.smtp.class", "javax.net.ssl.SSLSocketFactory");
    prop.put("mail.smtp.socketFactory.fallback","false ");
    sesh.setDebug(true);

    msg.setFrom(addressFrom);
    msg.setRecipient(Message.RecipientType.TO, addressTo);
    msg.setSubject(subject);
    msg.setContent(this.messageReformat(message), "text/plain");
    Transport.send(msg);
    }

  10. #10
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Don't even think about SSL until you get clear communications to work.

  11. #11
    ZZAnthony is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default классная штукенция

    натурально

  12. #12
    Poyntz's Avatar
    Poyntz is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by fishtoprecords View Post
    Don't even think about SSL until you get clear communications to work.
    well they don't. even with telnet reading connected :/

  13. #13
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    then work on getting the clear text version working.

    I suggest writing very simple code to test it, debug each step. And with the javax.mail class, turn on the debugger/logging. It usually is very helpful

  14. #14
    Poyntz's Avatar
    Poyntz is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    thanks for the tip, but i'm 100% certain this issue that i'm dealing with isn't an interface issue. if there are any interface issues later i'll deal with them when I see them.

  15. #15
    Poyntz's Avatar
    Poyntz is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    i didn't mention that the debugger didn't seem to offer me much assistance either. Additionally, I can't seem to get the demo samples included in the java mail package to send emails either

  16. #16
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Poyntz View Post
    i didn't mention that the debugger didn't seem to offer me much assistance either. Additionally, I can't seem to get the demo samples included in the java mail package to send emails either
    Then back up, get the demos to work.

Similar Threads

  1. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  2. Replies: 6
    Last Post: 08-18-2008, 07:44 AM
  3. Replies: 5
    Last Post: 05-14-2008, 02:43 PM
  4. Replies: 1
    Last Post: 07-27-2007, 11:59 PM
  5. Replies: 1
    Last Post: 07-25-2007, 11:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •