Problem about JavaMail

From: mrby (bianying_at_msn.com)
Date: 04/29/04


Date: Fri, 30 Apr 2004 00:47:29 +0800

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 = "email@def.com";
        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;
    }
}



Relevant Pages

  • Re: IIS hosting remote object
    ... Host a Remote Object in Microsoft Internet Information Services ... IClock ... > catch (Exception e) ... I replaced my actual web server name with "WebServer" in the url. ...
    (microsoft.public.dotnet.framework.remoting)
  • Authorization Problem
    ... performed a recent upgrade and have a strange problem. ... An unhandled exception occurred during compilation using the ... access rights to the resource to the ASP.NET process identity. ... CodeCompileUnit e) +90 ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Debugging under VMWare
    ... but when debugging in the vm I can browse to several pages then for one page boom "Project UNKNOWN faulted with message: 'access violation at 0x75bb2443: read of address 0x75bb2443'. ... same exception and I have to end the process. ... problems running under the host OS though, ... machine support DES perhaps? ...
    (borland.public.delphi.non-technical)
  • System.OutOfMemory exception: error creating window handle
    ... I've run into a strange problem. ... working with MS SQL Server) ... under the debugger. ... sometimes the following exception is thrown: ...
    (microsoft.public.dotnet.framework)
  • System.OutOfMemory exception: error creating window handle
    ... I've run into a strange problem. ... working with MS SQL Server) ... under the debugger. ... sometimes the following exception is thrown: ...
    (microsoft.public.dotnet.framework.windowsforms)