javamail的问题
java.lang.SecurityException: Access to default session denied
出现这个的异常是什么问题:
public void setAccount(
String user,String password)
{
this.user=user;
this.password=password;
}
public void send(String from,String to, String subject,String content)
{
Properties props = new Properties();
props.put("mail.smtp.host", host);
//指定SMTP服务器
props.put("mail.smtp.auth", "true");
//指定是否需要SMTP验证
try
{
SMTPAuthenticator auth = new SMTPAuthenticator(user,password);
PasswordAuthentication auths = auth.getPasswordAuthentication();
Session mailSession = Session.getDefaultInstance(props,null);
mailSession.setDebug(true);
//是否在控制台显示debug信息
Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
// //发件人
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// //收件人
message.setSubject(subject);
//邮件主题
message.setText(content);
//邮件内容
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch(Exception e)
{
System.out.println(e);
}
}
发送邮件的主要源码,如果将Session.getDefaultInstance()
改用创建新的session也就是Session.getInstance();就会出现另外的异常
:javax.mail.MessagingException: Unknown SMTP host: smtp.163.com;
nested exception is:
java.net.UnknownHostException: smtp.163.com
如果将Session.getDefaultInstance()
调整为:Session mailSession = Session.getDefaultInstance(props,auth);
就会出现异常:java.lang.SecurityException: Access to default session denied;
我做了好多次测试.问题是这样的:同样的代码.同样的jar,在tomcat下确是完全正确的.无论上面的Session如何设置.
但是我将它放置到websphere中时就出现了上面我说的,各个异常.
希望有高手经过.帮忙解决一下.小弟在这里谢过了,