Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem with javamail demo program msgsendsample.java

25 views
Skip to first unread message

vu...@yahoo.com

unread,
Mar 14, 2000, 3:00:00 AM3/14/00
to
Hello,

I am trying to execute the sample program msgsendsample.java from the
JavaMail 1.1 demo directory.

I am using Win98 with a dialup internet connection.

I am able to get the program to compile fine but when I run it I obtain
an error saying it can't connect to my external smtp mail server.

Below I will provide a trace of what happens and also source code for
the msgsendsample.java. Does anyone know what I am doing wrong? How do
I send a msg using an external smtp provider? Is it because the the
program is trying to connect to the wrong default port? I am able to
send messages using the same smpt server using another email client
such as Netscape mail or or Frontpage express.

If anyone can help, I would be so grateful.

thanks

******begin trace****************
BASH.EXE-2.02$ java msgsendsample p...@uvic.ca vu...@yahoo.com
smtp.uvic.ca false


--Exception handling in msgsendsample.java
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Could not connect to SMTP host:
smtp.uvic
.ca, port: 25;
nested exception is:
java.net.SocketException: no further information (code=10051)
at javax.mail.Transport.send0(Compiled Code)
at javax.mail.Transport.send(Transport.java:80)
at msgsendsample.main(Compiled Code)


Exception in thread "main" java.lang.ClassCastException
at msgsendsample.main(Compiled Code)
BASH.EXE-2.02$
******end trace****************

******begin msgsendsample.java****************
/*
* @(#)msgsendsample.java 1.15 99/12/06
*
* Copyright 1996-1999 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the proprietary information of Sun Microsystems,
Inc.
* Use is subject to license terms.
*
*/

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.Address;
import javax.mail.internet.*;
import javax.activation.*;

/**
* msgsendsample creates a very simple text/plain message and sends it.
* <p>
* usage: <code>java msgsendsample <i>to from smtphost
true|false</i></code>
* where <i>to</i> and <i>from</i> are the destination and
* origin email addresses, respectively, and <i>smtphost</i>
* is the hostname of the machine that has the smtp server
* running. The last parameter either turns on or turns off
* debugging during sending.
*
* @author Max Spivak
*/
public class msgsendsample {
static String msgText = "This is a message body.\nHere's the second
line.";

public static void main(String[] args) {
if (args.length != 4) {
usage();
System.exit(1);
}

System.out.println();

String to = args[0];
String from = args[1];
String host = args[2];
boolean debug = Boolean.valueOf(args[3]).booleanValue();

// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", host);
if (debug) props.put("mail.debug", args[3]);

Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);

try {
// create a message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(args[0])};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("JavaMail APIs Test");
msg.setSentDate(new Date());
// If the desired charset is known, you can use
// setText(text, charset)
msg.setText(msgText);

Transport.send(msg);
} catch (MessagingException mex) {
System.out.println("\n--Exception handling in
msgsendsample.java");

mex.printStackTrace();
System.out.println();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
System.out.println(" ** Invalid Addresses");
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
System.out.println(" " + invalid
[i]);
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses
();
if (validUnsent != null) {
System.out.println(" ** ValidUnsent
Addresses");
if (validUnsent != null) {
for (int i = 0; i < validUnsent.length;
i++)
System.out.println
(" "+validUnsent[i]);
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
System.out.println(" ** ValidSent
Addresses");
if (validSent != null) {
for (int i = 0; i < validSent.length; i++)
System.out.println(" "+validSent
[i]);
}
}
}
System.out.println();
} while ((ex = ((MessagingException)ex).getNextException())
!= null);
}
}

private static void usage() {
System.out.println("usage: java msgsendsample <to> <from>
<smtp> true|false");
}
}
******end msgsendsample.java*****************

Sent via Deja.com http://www.deja.com/
Before you buy.

Jon Skeet

unread,
Mar 15, 2000, 3:00:00 AM3/15/00
to
vu...@yahoo.com wrote:
> Hello,
>
> I am trying to execute the sample program msgsendsample.java from the
> JavaMail 1.1 demo directory.
>
> I am using Win98 with a dialup internet connection.
>
> I am able to get the program to compile fine but when I run it I obtain
> an error saying it can't connect to my external smtp mail server.
>
> Below I will provide a trace of what happens and also source code for
> the msgsendsample.java. Does anyone know what I am doing wrong? How do
> I send a msg using an external smtp provider? Is it because the the
> program is trying to connect to the wrong default port? I am able to
> send messages using the same smpt server using another email client
> such as Netscape mail or or Frontpage express.

Try turning JavaMail debug on (session.setDebug (true)); and then you'll
be able to see any traffic that gets through. Sounds like it just can't
connect though. Did you already have your dialup connection up and
running? What happens if you just telnet to port 25 of the smtp server?

--
Jon Skeet - sk...@pobox.com
http://www.pobox.com/~skeet/

vu...@yahoo.com

unread,
Mar 15, 2000, 3:00:00 AM3/15/00
to
Hi,
thanks for replying. I still don't know why it does not work for me.
I tried your suggestion of turning on the debug info and it looks like
what is implied is true, it can't connect to the server. I was a little
unsure of how to connect to port 25 of the smtp server using telnet. I
tried typing telnet smtp.mail.yahoo.com 25 on the command line and it
says I can't connect to the server.

Also using a suggestion from a previous post, I checked to make sure
the smtp server is actually listening by creating a simple program that
creats a socket to the smtp server. I get an error
java.net.SocketException and thus I am sure I can't connect to the
server on port 25. But when I do a simple ping of the smtp server, it
replies fine. And so is it that the default port of 25 is wrong? Can
anyone help me figure out what is going wrong? Below I will show the
actual traces and program listing.

If you have a suggestion why my this simple program does not work for
me, pls let me know.

/*****error msg obtained when trying to send msg*******/
BASH.EXE-2.02$ java msgsendsample vu...@yahoo.com vu...@yahoo.com
smtp.mail.yahoo
.com true

DEBUG: not loading system providers in <java.home>/lib
DEBUG: not loading optional custom providers file: /META-
INF/javamail.providers
DEBUG: successfully loaded default providers

DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name:
{com.sun.mail.smtp.SMTPTransport=javax.ma
il.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun
Microsystems, Inc
], com.sun.mail.imap.IMAPStore=javax.mail.Provider
[STORE,imap,com.sun.mail.imap.
IMAPStore,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider
[STORE,imap,com.su
n.mail.imap.IMAPStore,Sun Microsystems, Inc], smtp=javax.mail.Provider
[TRANSPORT
,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: not loading optional address map file: /META-
INF/javamail.address.map

DEBUG: getProvider() returning javax.mail.Provider
[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false

DEBUG: SMTPTransport trying to connect to host "smtp.mail.yahoo.com",
port 25


--Exception handling in msgsendsample.java
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Could not connect to SMTP host:

smtp.mail
.yahoo.com, port: 25;


nested exception is:
java.net.SocketException: no further information (code=10051)
at javax.mail.Transport.send0(Compiled Code)
at javax.mail.Transport.send(Transport.java:80)
at msgsendsample.main(Compiled Code)


Exception in thread "main" java.lang.ClassCastException
at msgsendsample.main(Compiled Code)

BASH.EXE-2.02$/*****end error msg*******/

/*****begin socket program listing*****/

try {
System.out.println("\nMhi.");
Socket aTestSocket = new Socket("smtp.mail.yahoo.com", 25);
System.out.println("\nconnection established.");
} catch (Exception e) {
e.printStackTrace();

}/****end socket program listing******/


/*****output of socket program*****/
BASH.EXE-2.02$ java test


java.net.SocketException: no further information (code=10051)

at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Compiled Code)
at java.net.PlainSocketImpl.connectToAddress
(PlainSocketImpl.java:125)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:112)
at java.net.Socket.<init>(Socket.java:269)
at java.net.Socket.<init>(Socket.java:98)
at test.<init>(test.java:15)
at test.main(test.java:7)

/****end output of socket program******/

> Try turning JavaMail debug on (session.setDebug (true)); and then
you'll
> be able to see any traffic that gets through. Sounds like it just
can't
> connect though. Did you already have your dialup connection up and
> running? What happens if you just telnet to port 25 of the smtp
server?
>
> --
> Jon Skeet - sk...@pobox.com
> http://www.pobox.com/~skeet/
>

vu...@yahoo.com

unread,
Mar 15, 2000, 3:00:00 AM3/15/00
to
Hi,

I have since confirmed that there is nothing wrong with the smtp server
that I am using. I tried running my simple test program that tries to
create a socket to the smtp server on a different machine and it is
able to create a socket fine.

Does anyone know why I can't create a socket to the smtp server on my
win98 machine. I have a dialup connection. everything else seems to be
ok. I can connect to the internet, use my browser, use ftp, telnet. But
why can't my machine establish a socket to a known working smtp server?

here is the simple socket program that I used:
import java.net.*;

public class test {

public static void main(String[] argv) {
new test(argv);
}


public test(String[] argv) {

try {
System.out.println("\nMhi.");
Socket aTestSocket = new Socket("smtp.uvic.ca", 25);


System.out.println("\nconnection established.");
} catch (Exception e) {
e.printStackTrace();
}
}

0 new messages