props.put还有两个属性没设
 //设置邮件传输协议为:smtp
props.put("mail.transpost.protocol","smtp");
//设置邮件服务器端口
props.put("mail.smtp.port","25");

解决方案 »

  1.   

    现在已经把mail.jar,activation-1.1.jar这2个包放进了项目中。
    代码
    Properties props = new Properties();
    Authenticator auth = new Email_Autherticator();
    props.put("mail.smtp.host", "smtp.163.com");
    props.put("mail.transpost.protocol", "smtp");
    props.put("mail.smtp.port", "25");
    props.put("mail.smtp.auth", "true");
    Session ssn = Session.getDefaultInstance(props, auth);
    MimeMessage message = new MimeMessage(ssn);
    message.setSubject("test subject");
    message.setText("test Body");
    message.setSentDate(new Date());
    Address fromAddress = new InternetAddress("[email protected]");
    message.setFrom(fromAddress);
    Address toAddress = new InternetAddress("[email protected]");
    message.addRecipient(Message.RecipientType.TO, toAddress);
    Transport.send(message); public class Email_Autherticator extends Authenticator {
    public Email_Autherticator() {
    super();
    } public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("test", "test");
    }
    }
    -------------------------------------------------------------
    报错信息
    Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
    at javax.mail.Session.loadProvidersFromStream(Session.java:928)
    at javax.mail.Session.access$000(Session.java:174)
    at javax.mail.Session$1.load(Session.java:870)
    at javax.mail.Session.loadResource(Session.java:1084)
    at javax.mail.Session.loadProviders(Session.java:889)
    at javax.mail.Session.<init>(Session.java:210)
    at javax.mail.Session.getDefaultInstance(Session.java:299)
    at com.xxx.util.mail.Test.send(Test.java:38)
    at com.xxx.util.mail.Test.main(Test.java:27)“Session ssn = Session.getDefaultInstance(props, auth);”这行代码报错
      

  2.   

    你放的mail.jar包是全的还是单独的???
    javamail的包有两种方式,一种是核心包和provider包分离的,可以自由组合,
    一种是包括所有的包。看你的出错信息,应该只是一个核心包,其它的provider包没有安装进去。
      

  3.   

    mail.jar是我在官方网站下的javamail-1_4.zip解压后根目录里的文件。现在我把lib目录下的所有.jar也都放进了项目里 。但问题依旧。大家帮帮忙呀。我在线等。
      

  4.   

    你自己要写一个验证类,继承Authenticator,用来把用户名和密码传进去,然后把你的得到seesion也有问题,可以把Session ssn = Session.getDefaultInstance(props, auth);换成Session ssn = Session.getInstance(props, auth);,auth这个类一定自己写啊
    可写成:
    public class CheckSendMail extends Authenticator {
    /**
     * Email发送帐号密码验证类
     */   private String m_username = null;
      private String m_userpass = null;
      
      public CheckSendMail(String username, String userpass)
      {
      super();
          setUsername(username);
          setUserpass(userpass);
      }
      
      
      public void setUsername(String username)
     {
          m_username = username;
      }
      public void setUserpass(String userpass)
      {
          m_userpass = userpass;
      }
         public PasswordAuthentication getPasswordAuthentication()
      {
          return new PasswordAuthentication(m_username,m_userpass);
      }}