我正在用Apache Commons电子邮件API尝试下列代码,并且它在上下文中引发错误。任何帮助?javax.naming.NoInitialContextException Apache Commons api
import java.util.Properties;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
public class email {
/**
* @param args
* @throws EmailException
*/
public static void main(String[] args) throws EmailException {
// TODO Auto-generated method stub
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "password");
}
});
SimpleEmail se = new SimpleEmail();
se.addTo("[email protected]");
se.setFrom("[email protected]");
se.setSubject("Test email");
se.setMsg("Hi there");
se.send();
}
}
它引发以下错误。我应该在哪里设置什么来克服这个问题?在这方面的任何帮助?
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at javax.xml.registry.samples.SimpleClient.doit(Unknown Source)
at javax.xml.registry.samples.SimpleClient.main(Unknown Source)
请问你'doit'方法是什么样子? 'javax.xml.registry.samples.SimpleClient'和你发布的代码是一样的吗? – longhua
我拥有的就是这个代码吗? AM我错过了什么?什么是doit方法?你能帮我吗? – user1411601
我想你正在使用错误的类?你如何运行你的Java类?你应该使用'email'类,而不是'javax.xml.registry.samples.SimpleClient'。请检查您的堆栈跟踪。 – longhua