163.net需要smtp认证,你要自己继承一个 Authenticator 类,比如:
public class MailAuth extends Authenticator
{
String userName,password;
public MailAuth(String user,String pass)
{
super();
userName=new String(user);
password=new String(pass);
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName,password);
}
}
然后:Properties props = new Properties();
//初始化参数
props.put("mail.smtp.host",smtpHost);
props.put("mail.transport.protocol","smtp");
props.put("mail.transport.port",smtpPort);
props.put("mail.smtp.auth","true"); //这行一定要有

//创建Session
MailAuth ma=new MailAuth(smtpUser,smtpPass);
Session mailSession=Session.getInstance(props,ma);这样就行了

解决方案 »

  1.   

    我改成这样了可是还是不行,高手们帮忙在看一下,那里错了或者需要设置什么东西呢??
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    import java.util.*;
    public class mymail extends Authenticator {    String userName,password;  public mymail(String user,String pass) {
                        super();
    userName=new String(user);
    password=new String(pass);
    }
    protected PasswordAuthentication getPasswordAuthentication()
    {
    return new PasswordAuthentication(userName,password);
    }
      public static void main(String[] args){
      try{
      Properties props=new Properties();
      props.put("mail.host","163.net");
      props.put("mail.transport.protocol","smtp");
    //props.put("mail.transport.port",smtpPort);
    props.put("mail.smtp.auth","true"); //这行一定要有
    mymail mm=new mymail("zmrljl","fcyljl");
      Session mailConnection=Session.getInstance(props,mm);
      Message msg=new MimeMessage(mailConnection);
      Address bill=new InternetAddress("[email protected]");
      Address elliotte=new InternetAddress("[email protected]");
      msg.setContent("resistansce is futile ,tyou wiil be assistant!","text/plain");
      msg.setFrom(bill);
      msg.setRecipient(Message.RecipientType.TO,elliotte);
      msg.setSubject("you must ok");
      Transport.send(msg);  }
    catch(Exception e){
    e.printStackTrace() ;
    }
    }
    }
    /////////////////
    javax.mail.SendFailedException: Sending failed;
      nested exception is:
    class javax.mail.AuthenticationFailedException at javax.mail.Transport.send0(Transport.java:218) at javax.mail.Transport.send(Transport.java:80) at webmail.mymail.main(mymail.java:46)
    这个类需要抛出异常吗??
      

  2.   

    163.net必须用原来的用户作为邮件发出者的,也就是说你的from必须是
    [email protected]你把debug设置成true可以看到交互内容,另外你耶应该可以看到Exception的内容啊:javax.mail.MessagingException: 553 You are not authorized to send mail as <MAIL FROM: < ....>>, authentication is required