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

What is wrong with this javamail code?

670 views
Skip to first unread message

Mike N. Christoff

unread,
Mar 12, 2001, 10:39:48 PM3/12/01
to

I can't figure out why this code throws an exception.

=============================================
import javax.mail.*;
import java.util.*;

public class MailTest
{
public static void main(String[] args)
{
Properties mailProps = new Properties();
mailProps.put("mail.transport.protocol", "smtp");
mailProps.put("mail.smtp.host", "myHost");

try {
Session session = Session.getDefaultInstance(mailProps, new LogIn());

/* also tried this - with the same result
Session session = Session.getDefaultInstance(mailProps, null); */

Store store = session.getStore();
store.connect();
Folder folder = store.getFolder("Inbox");
folder.open(Folder.READ_WRITE);
int numMsgs = folder.getMessageCount();
Message[] msgs = folder.getMessages();

for(int i = 0; i < numMsgs; ++i)
System.out.println(msgs[i].getSubject());
} catch(Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
=========================================
import javax.mail.*;

public class LogIn extends Authenticator
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("username", "password");
}
}
===========================================

Here is the exception that is thrown
(NOTE: I have j2ee.jar in my class path which includes mail.jar and
activation.jar)

javax.mail.NoSuchProviderException: Invalid protocol: null
at javax.mail.Session.getProvider(Session.java:265)
at javax.mail.Session.getStore(Session.java:363)
at javax.mail.Session.getStore(Session.java:343)
at javax.mail.Session.getStore(Session.java:329)
at MailTest.main(MailTest.java:15)
Invalid protocol: null

=============================================
I thought SMTP was supposed to come with javamail, but it apparently can't
find the SMTP provider classes. Thanks for any help.
---

l8r, mike

Robert Lynch

unread,
Mar 13, 2001, 12:43:20 AM3/13/01
to

IMO, you get the exception because you are not giving the "store"
provider, e.g.:

Properties mailProps = new Properties();

mailProps.put("mail.transport.protocol", "smtp"); // you give
transport...
mailProps.put("mail.store.protocol", "imap"); // but not store

for which there is a provider:

$ cat META-INF/javamail.default.providers
# JavaMail IMAP provider Sun Microsystems, Inc
protocol=imap; type=store; class=com.sun.mail.imap.IMAPStore;
vendor=Sun Microsystems, Inc;
# JavaMail SMTP provider Sun Microsystems, Inc
protocol=smtp; type=transport;
class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems,
Inc;
# JavaMail POP3 provider Sun Microsystems, Inc
protocol=pop3; type=store; class=com.sun.mail.pop3.POP3Store;
vendor=Sun Microsystems, Inc;

(Does this make sense?) Bob L.
--
Robert Lynch Berkeley CA USA rml...@pacbell.net

Mike N. Christoff

unread,
Mar 13, 2001, 1:13:27 AM3/13/01
to
Thanks Robert. I no longer get the NoSuchProviderException, however I now
get the following:

javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:265)
at javax.mail.Service.connect(Service.java:135)
at javax.mail.Service.connect(Service.java:87)
at MailTest.main(MailTest.java:17)

I know for a fact the username and password I'm using are correct. The
outgoing server uses SMTP and the incoming server uses POP3. I also know
the server names I provided are correct. I'm a bit confused, as I thought
to retrieve email I would use POP3, so don't I have to specify the POP3
server somewhere?

Mike N. Christoff

unread,
Mar 13, 2001, 1:34:09 AM3/13/01
to
By the way, I tried using both:
mailProps.put("mail.store.protocol", "smtp");
mailProps.put("mail.store.protocol", "pop3");

and got the same exception.
---


l8r, mike


Mike N. Christoff

unread,
Mar 13, 2001, 2:21:41 AM3/13/01
to
Thanks again for the help Robert. I got everything to work. I used:

mailProps.put("mail.store.protocol", "pop3");

mailProps.put("mail.pop3.host", "my.pop3.server");

Also, I forgot I had commented out the line that passed an authenticator to
Session.getInstance().
---

l8r, mike


Mike N. Christoff <killallspamme...@sympatico.ca> wrote in message
news:Bbjr6.379285$f36.12...@news20.bellglobal.com...

0 new messages