Problem about JavaMail

Discussion in 'Java' started by mrby, Apr 29, 2004.

  1. mrby

    mrby Guest

    Hi,

    I'm just trying to use JavaMail and have a strange problem.
    (See the code snippet below)

    When running it, the exception error message is:

    errorjavax.mail.SendFailedException: Sending failed;
    nested exception is:
    javax.mail.MessagingException: Could not connect to SMTP host: localhost,
    port: 25;
    nested exception is:
    java.net.ConnectException: Connection refused

    the problem is in the line with //!!!!
    If I comment this line out, it works fine.

    The purpose of this line to invoke check_server is to
    have a check first.

    Who can give me some hints?
    Thanks much!

    ================================================
    import java.sql.*;
    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;

    /** Class handles sending email messages. */

    public class email {

    private static String mail_host = null;

    public static void main (String[] args) {
    mail_host = "mrby.8866.org"; //which is a valid mail server
    if ( check_server(mail_host) ) //!!!!!
    send();
    }

    public static void send ()
    {
    String to = "root@localhost";
    String subject = "hello,world";
    String total_message = "test email";
    String from = "";
    String host = mail_host;

    Properties props = new Properties();
    props.put("mail.smtp.host", host );
    Session session = Session.getDefaultInstance(props, null);
    try {
    // create a message
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setRecipients( Message.RecipientType.TO,
    InternetAddress.parse(to) );
    msg.setSubject(subject);
    msg.setText(total_message);
    Transport.send(msg);
    System.out.println(host);
    }
    catch (MessagingException mex) {
    System.out.println("error"+mex.toString());
    }
    }


    // Method to check if the SMTP service on a host is available
    private static boolean check_server( String host )
    {
    boolean flag;
    Transport tr = null;

    try {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props,null);
    tr = session.getTransport("smtp");
    tr.connect(host,null,null);
    flag = true;
    tr.close();
    }
    catch (MessagingException mx) {
    flag = false;
    }
    finally {
    try {
    if (tr != null) {
    tr.close();
    }
    }
    catch (MessagingException mex) {}
    }
    return flag;
    }
    }
     
    mrby, Apr 29, 2004
    #1
    1. Advertising

  2. mrby

    GaryM Guest

    "mrby" <> wrote in news:c6rbng$2bo6$99.com:

    > Who can give me some hints?
    > Thanks much!
    >
    >


    I think you should not call tr.connect(host, null, null). Put the
    host in the session, then call tr.connect(). Reason? Not 100% on
    this, but I think the connect(String, String, String) requires all 3
    params or 3 nulls (which prompts the underlying code to do a
    different connect). I don't think it can handle the host, null, null
    and is defaulting to the Session which defaults to "localhost" if
    undefined. Note in your example there is no "mail.smtp.host" property
    defined at that point in the code. You do this only in send().

    There are a couple of things with this example you could change that
    would make your life easier. check_server does not need the host as a
    parm, as that parm is a class variable and is visible to all methods.
    Likewise no need for a copy of it to be made in the send() method.

    I would consider making the Session instance an instance variable and
    initializing it in the constructor/initializer accordingly.

    HTH,

    Gary
     
    GaryM, Apr 29, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news
    Replies:
    0
    Views:
    349
  2. Jason

    q javamail compile problem

    Jason, Dec 15, 2003, in forum: Java
    Replies:
    3
    Views:
    408
    Jason
    Dec 19, 2003
  3. Nilambari

    problem sending javamail

    Nilambari, Dec 23, 2003, in forum: Java
    Replies:
    2
    Views:
    8,379
    Ulas Ergin
    Dec 26, 2003
  4. Bobby Martin

    JavaMail sending problem and solution

    Bobby Martin, May 19, 2004, in forum: Java
    Replies:
    4
    Views:
    5,822
    Bobby Martin
    May 21, 2004
  5. webguynow

    javamail pop Store SBC problem

    webguynow, Jan 3, 2005, in forum: Java
    Replies:
    1
    Views:
    592
    webguynow
    Jan 4, 2005
Loading...

Share This Page