2010-05-09 49 views
2

任何人都可以告诉我如何在Eclipse中使用James服务器作为我的服务器和Java?在Eclipse中使用James Server使用JavaMail

我想测试下面发布的两个类,但我得到以下错误: 线程“主”的异常javax.mail.AuthenticationFailedException:身份验证失败。

public class JamesConfigTest 
{ 
public static void main(String[] args) 
throws Exception 
{ 
// CREATE CLIENT INSTANCES 
MailClient redClient = new MailClient("red", "localhost"); 
MailClient greenClient = new MailClient("green", "localhost"); 
MailClient blueClient = new MailClient("blue", "localhost"); 

// CLEAR EVERYBODY'S INBOX 
redClient.checkInbox(MailClient.CLEAR_MESSAGES); 
greenClient.checkInbox(MailClient.CLEAR_MESSAGES); 
blueClient.checkInbox(MailClient.CLEAR_MESSAGES); 
Thread.sleep(500); // Let the server catch up 

// SEND A COUPLE OF MESSAGES TO BLUE (FROM RED AND GREEN) 
redClient.sendMessage(
    "[email protected]", 
    "Testing blue from red", 
    "This is a test message"); 
greenClient.sendMessage(
    "[email protected]", 
    "Testing blue from green", 
    "This is a test message"); 
Thread.sleep(500); // Let the server catch up 

// LIST MESSAGES FOR BLUE (EXPECT MESSAGES FROM RED AND GREEN) 
blueClient.checkInbox(MailClient.SHOW_AND_CLEAR); 
} 
} 


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

public class MailClient 
extends Authenticator 
{ 
public static final int SHOW_MESSAGES = 1; 
public static final int CLEAR_MESSAGES = 2; 
public static final int SHOW_AND_CLEAR = 
SHOW_MESSAGES + CLEAR_MESSAGES; 

protected String from; 
protected Session session; 
protected PasswordAuthentication authentication; 

public MailClient(String user, String host) 
{ 
this(user, host, false); 
} 

public MailClient(String user, String host, boolean debug) 
{ 
from = user + '@' + host; 
authentication = new PasswordAuthentication(user, user); 
Properties props = new Properties(); 
props.put("mail.user", user); 
props.put("mail.host", host); 
props.put("mail.debug", debug ? "true" : "false"); 
props.put("mail.store.protocol", "pop3"); 
props.put("mail.transport.protocol", "smtp"); 
session = Session.getInstance(props, this); 
} 

public PasswordAuthentication getPasswordAuthentication() 
{ 
return authentication; 
} 

public void sendMessage(
String to, String subject, String content) 
    throws MessagingException 
{ 
System.out.println("SENDING message from " + from + " to " + to); 
System.out.println(); 
MimeMessage msg = new MimeMessage(session); 
msg.addRecipients(Message.RecipientType.TO, to); 
msg.setSubject(subject); 
msg.setText(content); 
Transport.send(msg); 
} 

public void checkInbox(int mode) 
    throws MessagingException, IOException 
{ 
if (mode == 0) return; 
boolean show = (mode & SHOW_MESSAGES) > 0; 
boolean clear = (mode & CLEAR_MESSAGES) > 0; 
String action = 
    (show ? "Show" : "") + 
    (show && clear ? " and " : "") + 
    (clear ? "Clear" : ""); 
System.out.println(action + " INBOX for " + from); 
Store store = session.getStore(); 
store.connect(); 
Folder root = store.getDefaultFolder(); 
Folder inbox = root.getFolder("inbox"); 
inbox.open(Folder.READ_WRITE); 
Message[] msgs = inbox.getMessages(); 
if (msgs.length == 0 && show) 
{ 
    System.out.println("No messages in inbox"); 
} 
for (int i = 0; i < msgs.length; i++) 
{ 
    MimeMessage msg = (MimeMessage)msgs[i]; 
    if (show) 
    { 
    System.out.println(" From: " + msg.getFrom()[0]); 
    System.out.println(" Subject: " + msg.getSubject()); 
    System.out.println(" Content: " + msg.getContent()); 
    } 
    if (clear) 
    { 
    msg.setFlag(Flags.Flag.DELETED, true); 
    } 
    } 
    inbox.close(true); 
    store.close(); 
    System.out.println(); 
    } 
    } 

回答

1

我有相同的javax.mail.AuthenticationFailedException问题。基本上我通过使用命令“deluser blue”删除用户蓝色,然后使用命令“adduser blue blue”再次添加用户来修复它。之后,它的工作。