JavaMail Error - AuthenticationFailedException

T

TC

Java 1.4.2
JavaMail API 1.3.2
NetBeans 4.1


I have done quite a bit of research on this issue and cannot find the
problem. I've even checked with one of our local programmers.

I created one program that connects to our smtp server and sends an
email. This works fine. Now, I'm trying to create a program that
connects to the smtp server and reads/gets the email message subjects.
This is failing. I have tried changing the getStore("pop3") to
getStore("smtp") but this gives the error "Invalid provider". If I use
caps (SMTP), I get "No provider for SMTP".

The host I'm using is the same as what I use to send mail. I have
mail.jar and authentication.jar in my Project Compiling Sources dir.
I'm guessing it means it's in my CLASSSPATH.

The META-INF file found in the JavaMail\lib\ dir contains .jar files
for pop3 and smtp. Do I need to add my host to the smtp.jar file?


Here's my code:

*
* ReadMail.java
*
* Created on November 4, 2004, 10:20 AM
*/
/**
*
* @author jmartin
*/
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class ReadMail {
public static void main (String agrs[])
throws Exception {
try {
String host = "server.mydomain.com";
String userName = ""; // Has a valid username
String password = ""; // Has a valid passwd
// Create empty properties
Properties props = new Properties();
// Get session
Session session = Session.getDefaultInstance(props, null);
// Get the store
Store store = session.getStore("pop3");
store.connect(host, userName, password);
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
// Get directory
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++){
System.out.println(i + ": " + message.getFrom()[0]
+ "\t" + message.getSubject());
}
// Close connection
folder.close(false);
store.close();
} // try
catch (Exception e){
System.out.println("There was an error.");
e.printStackTrace();
}
} // main
}
 
S

shakahshakah

You can set Javamail to run in debug mode via Session.setDebug(true) or
setting the "mail.debug" property (props.put("mail.debug", "true")):
http://java.sun.com/products/javamail/FAQ.html#debug

Beyond that, and at the risk of asking the obvious, are you sure you're
specifying a valid server, e-mail account, and password when trying to
connect? Is your POP server different than your SMTP server? Can you
use telnet to connect via POP using the same information you're using
with the Java code?
 
T

Thomas Weidenfeller

TC said:
I created one program that connects to our smtp server and sends an
email. This works fine. Now, I'm trying to create a program that
connects to the smtp server and reads/gets the email message subjects.
This is failing. I have tried changing the getStore("pop3") to
getStore("smtp") but this gives the error "Invalid provider". If I use
caps (SMTP), I get "No provider for SMTP".

In a typical "home user" setup you use SMTP and POP3 (or IMAP) for
different things. Simplified:

SMTP: To send mail from your desktop computer.
POP3: To read mail from the mailbox which your ISP provides.

So, don't be surprised that getStore("smtp") fails, this is not the
protocol for reading data from a mailbox at your ISP, and there is no
point in trying - it can't work in this scenario (that's why a separate
protocol like POP3 was invented).

Often, the SMTP server and the POP3 server of an ISP are not the same,
so this might be a source of your problems. So you better check twice if
you really provide the correct data.
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);

I remember that get-folder part slightly different, but I can't check at
the moment. I think one was first supposed to get the default folder.

// Close connection
folder.close(false);
store.close();
} // try

You should move the close() to a finally block to ensure they are always
called (plus appropriate checks that you don't invoke the close()
methods on null references).

/Thomas
 
T

TC

Thomas said:
In a typical "home user" setup you use SMTP and POP3 (or IMAP) for
different things. Simplified:

SMTP: To send mail from your desktop computer.
POP3: To read mail from the mailbox which your ISP provides.

So, don't be surprised that getStore("smtp") fails, this is not the
protocol for reading data from a mailbox at your ISP, and there is no
point in trying - it can't work in this scenario (that's why a
separate protocol like POP3 was invented).

Often, the SMTP server and the POP3 server of an ISP are not the
same, so this might be a source of your problems. So you better check
twice if you really provide the correct data.

In the process now.
I remember that get-folder part slightly different, but I can't check
at the moment. I think one was first supposed to get the default
folder.

I can use getDefaultFolder but debug gives "The requested mailbox is
not available on this server".
You should move the close() to a finally block to ensure they are
always called (plus appropriate checks that you don't invoke the
close() methods on null references).

Will do.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top