• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

the authenticator cannot work?

 
Wai Le
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have wirte a midlet which simply give 2 parameters (user & pwd) to the servlet which is for authenticating the user to retrieve his/her e-mail inbox account.
the coding is below:
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class ServletMailLogin extends HttpServlet{
String user,pwd;
public void doPost(HttpServletRequest req ,HttpServletResponse res) throws IOException, ServletException
{res.setContentType("text/plain");
PrintWriter out = res.getWriter();
user = req.getParameter("user");
pwd = req.getParameter("pwd");

try{
Properties props = new Properties();
//place the authentication info in
props.put("mail.smtp.host","thomaslee1121.no-ip.com");
props.put("mail.smtp.host","true");
// create an empty authenticator object
Authenticator auth = new SMTPAuthenticator();
// Get a session - pass auth object
Session session = Session.getDefaultInstance(props, auth);
Store store = session.getStore("smtp");
// do not pass any arguments to the connect method
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message messages[] = folder.getMessages();
for (int i=0; i<messages.length; i++) {
out.print(i + ": " + messages[i].getFrom()[0] + "\t" + messages[i].getSubject() + "\t" + messages[i].getSentDate() + "\n\n");
}
folder.close(false);
store.close();
}
catch (MessagingException me) {
System.err.print(user);
}


}

private class SMTPAuthenticator extends javax.mail.Authenticator{
public PasswordAuthentication getPasswordAuthentication(){
String username,password;

username = ServletMailLogin.this.user;
password = ServletMailLogin.this.pwd;
return new PasswordAuthentication(username,password);
}
}
}
compliation is not problem but it will just return me a empty(not null!!) content to the emulator.
Can someone give me advice PLS??
 
Isaias C. Barroso
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When you call the servlet directly on Browser, its work fine?
http://localhost:8080/app/ServletMailLogin?usr=xxxx&pwd=yyyy
Best Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic