怎么解决javax.mail中,smtp的认证问题 我已经找了很多资料。。但是还是java.lang.SecurityException: Access to default session denied。ft.... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好了,我已经解决了public static void mysendMail(String host,String from,String to ,String subject ,String context) throws RemoteException { java.util.Properties property=new java.util.Properties (); property.put("mail.smtp.host",host); property.put("mail.smtp.auth","true"); try { Authenticator auth = new SmtpAuth("zhouhaojie","lovewindy"); javax.mail.Session session = javax.mail.Session.getDefaultInstance(property,auth ) ;// javax.mail.internet.InternetAddress fromadress = new javax.mail.internet.InternetAddress (from); InternetAddress fromadress = new InternetAddress(from); javax.mail.internet.InternetAddress toadress= new javax.mail.internet.InternetAddress (to); javax.mail.internet.MimeMessage mymessage = new javax.mail.internet.MimeMessage (session); mymessage.setFrom(fromadress); mymessage.setRecipient(Message.RecipientType.TO ,toadress);// mymessage.setRecipient(Message.RecipientType.CC,new InternetAddress("[email protected]")); mymessage.setSentDate(new java.util.Date()); mymessage.addHeader("zhj","test") ; mymessage.setSubject(subject) ; mymessage.setText(context) ; Transport tt=session.getTransport("smtp");// tt.connect(host,"zhouhaojie","lovewindy") ; tt.send(mymessage) ;// javax.mail.Transport.send(mymessage) ; } catch (MessagingException e) { throw new RemoteException("error"+e.getMessage() ); }catch (Exception e) { throw new RemoteException("err"+e.getMessage() ); } }其中的SmtpAuth继承之javax.mail.Authenticator static class SmtpAuth extends javax.mail.Authenticator { private String user; private String password; public SmtpAuth(String newuser,String userpassword) { user=newuser; password=userpassword; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication (user,password); } } class Authenticator1 extends Authenticator{ public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication("user","password"); }}....Session smtpSession = Session.getInstance(props,new Authenticator1());具体的情况自己可以改Authenticator1类 比赛题目求解 MyEclipse6.0 万份火急..求救!!! web项目,文件上传到服务器上,怎么删除啊。 请教:类的继承和合成 Swing是线程安全的吗?如何在Swing中使用多线程? 请问这里有谁懂SNMP啊? 经典问题,希望高手能给解决 不同类中的main是否有用? 关于连接池的问题? 如何将Tomcat安装为NT的一个服务 有南京的朋友吗?进来把你伊妹妹留下。 方法内可以使用final 不??
public static void mysendMail(String host,String from,String to ,String subject ,String context)
throws RemoteException
{
java.util.Properties property=new java.util.Properties ();
property.put("mail.smtp.host",host);
property.put("mail.smtp.auth","true");
try
{
Authenticator auth = new SmtpAuth("zhouhaojie","lovewindy");
javax.mail.Session session = javax.mail.Session.getDefaultInstance(property,auth ) ;
// javax.mail.internet.InternetAddress fromadress = new javax.mail.internet.InternetAddress (from);
InternetAddress fromadress = new InternetAddress(from);
javax.mail.internet.InternetAddress toadress= new javax.mail.internet.InternetAddress (to);
javax.mail.internet.MimeMessage mymessage = new javax.mail.internet.MimeMessage (session);
mymessage.setFrom(fromadress);
mymessage.setRecipient(Message.RecipientType.TO ,toadress);
// mymessage.setRecipient(Message.RecipientType.CC,new InternetAddress("[email protected]"));
mymessage.setSentDate(new java.util.Date());
mymessage.addHeader("zhj","test") ;
mymessage.setSubject(subject) ;
mymessage.setText(context) ;
Transport tt=session.getTransport("smtp");
// tt.connect(host,"zhouhaojie","lovewindy") ;
tt.send(mymessage) ;
// javax.mail.Transport.send(mymessage) ; }
catch (MessagingException e)
{
throw new RemoteException("error"+e.getMessage() );
}catch (Exception e)
{
throw new RemoteException("err"+e.getMessage() );
} }
其中的SmtpAuth继承之javax.mail.Authenticator
static class SmtpAuth extends javax.mail.Authenticator {
private String user;
private String password;
public SmtpAuth(String newuser,String userpassword)
{
user=newuser;
password=userpassword;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication (user,password);
}
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("user","password");
}
}
....
Session smtpSession = Session.getInstance(props,new Authenticator1());
具体的情况自己可以改Authenticator1类