javax.naming.NoInitialContextException Apache Commons api

时间:2013-05-28 11:27:19

标签: java apache email

我正在使用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@gmail.com", "password");
                    }
                  });
SimpleEmail se = new SimpleEmail();
se.addTo("email@gmail.com");
se.setFrom("email@gmail.com");
se.setSubject("Test email");
se.setMsg("Hi there");
se.send();

    }

}

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@gmail.com", "password"); } }); SimpleEmail se = new SimpleEmail(); se.addTo("email@gmail.com"); se.setFrom("email@gmail.com"); 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)

1 个答案:

答案 0 :(得分:1)

您正在混合javax.mail Apache Commons Email 。此代码应该适合您:

public static void main(String[] args) {
{
    try
    {
        Email se = new SimpleEmail();

        se.setHostName("smtp.googlemail.com");
        se.setSmtpPort(465);
        se.setAuthenticator(new DefaultAuthenticator("email@gmail.com", "password"));
        se.setSSLOnConnect(true);


        se.addTo("email@gmail.com");
        se.setFrom("email@gmail.com");
        se.setSubject("Test email");
        se.setMsg("Hi there");
        se.send();
    }
    catch(EmailException e) {
        System.out.println("Uh oh! It doesn't work!");
    }
}

更多信息:

Apache Commons Email Userguide