我已经找了很多资料。。但是还是
java.lang.SecurityException: Access to default session denied
。ft....

解决方案 »

  1.   

    好了,我已经解决了
    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);
       }
      
      }
      

  2.   

    class Authenticator1 extends Authenticator{
         public PasswordAuthentication getPasswordAuthentication(){
               return new PasswordAuthentication("user","password");
         }
    }
    ....
    Session smtpSession = Session.getInstance(props,new Authenticator1());
    具体的情况自己可以改Authenticator1类