import java.util.Properties;
import javax.mail.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class SendMail {
Context ic = null;
public SendMail() {
}
private Properties props;
public void send() throws Exception {
// 使用JNDI查询Mail Session
try {
/*
* Hashtable ht = new Hashtable();
* ht.put(Context.INITIAL_CONTEXT_FACTORY,
* "weblogic.jndi.WLInitialContextFactory");
* ht.put(Context.PROVIDER_URL, "t3://localhost:7001/slide/"); ic =
* new InitialContext(ht);
*/
ic = getInitialContext();
Session session = (Session) ic.lookup("mail/MailSession");
} catch (NamingException e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception {
SendMail sendMail = new SendMail();
sendMail.send();
System.out.println("send ok");
}
public Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = "weblogic";
String password = "weblogic";
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
}
return new InitialContext(properties);
} catch (Exception e) {
throw e;
}
}
}