Socket s = new Socket("centech-ops.com", 25); 
       out = new PrintWriter(s.getOutputStream());
       in = new BufferedReader(new
            InputStreamReader(s.getInputStream()));
    send(null);
    send("HELO " + "OPSSERVER");
    send("MAIL FROM: " + fadd);
    send("RCPT TO: " + sadd);
    send("DATA");
    out.println("Subject: "+sname+"您好"+ "\n");
    out.flush();
    out.println("From: "+fname+" <"+fadd+">"+ "\n");
    out.flush();
......假如以Socket方式发送
Subject可以这样发,那么用户名和密码该写在哪里?

解决方案 »

  1.   

    用户名和密码对应的SMTP命令是AUTH,你可以通过俘获数据包来观察认证过程。
      

  2.   

    验证bean:import javax.mail.PasswordAuthentication;
    import javax.mail.Authenticator;/**
     * <p>Title: javamail api</p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: halitech</p>
     * @author david guo
     * @version 1.0
     */  public class MyAuthenticator extends Authenticator {
    public MyAuthenticator() {
    super();
    }
    public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("username","password");
    }
    }
    调用:
        // Get system properties
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth","true");
        // Setup mail server
        Authenticator auth = new MyAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);
        ......................记得给分啊
      

  3.   

    大哥,有没有scoket访是的写法,谢谢(:
      

  4.   

    Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth","true");
    Session session = Session.getDefaultInstance(props, null);
    就可以,但是不是所有的服务器都要求发信验证,163.com就不许要,
    sina就需要。不用严整类也可以。我试过了。当然用的话,有什么好处就不止到了。