http://www.csdn.net/Develop/read_article.asp?id=20621
我写的,希望有帮助

解决方案 »

  1.   

    比如:163.com的smtp服务器的写法是:  smtp.163.com
      

  2.   

    找个easymail用,建个用户名账号,不就有了smtp账号了吗
      

  3.   

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.host", "smtp.163.com");
    Session sess = Session.getDefaultInstance(props);
      

  4.   

    建一个mail.properties文件:
    hostname=smtp.mail.yahoo.com.cn
    username=baosenbj
    password=team36253个参数按照有效的内容设置。
      

  5.   

    package metacat;import java.io.*;
    import java.util.*;
    import javax.mail.*;
    import javax.mail.event.*;
    import javax.mail.internet.*;
    import javax.activation.*;public final class Mailer extends Object implements Serializable {  /* Bean Properties */
      private String to = null;
      private String from = null;
      private String subject = null;
      private String message = null;
      public static Properties props = null;
      public static Session session = null;
      private static String username;
      private static String password;
      private static String smtphost = "";
      private PrintWriter log;  static {
        /* Setting Properties for STMP host */
        props = System.getProperties();
        //props.put("mail.smtp.host", smtphost);
        //props.put("mail.smtp.auth","true");
        //username = "";
        //password = "";
      }  public void Mailer(){
        this.setConfig();
        /* Setting Properties for STMP host */
        //props = System.getProperties();
        props.put("mail.smtp.host", smtphost);
        props.put("mail.smtp.auth","true");
      }
      /* Setter Methods */
      public void setTo(String to) {
        this.to = to.trim();
      }  public void setFrom(String from) {
        this.from = from.trim();
      }  public void setSubject(String subject) {
        this.subject = subject;
      }  public void setMessage(String message) {
        this.message = message;
      }
      /* Sends Email */
      public void sendMail() throws Exception {
        this.setConfig();
        if(!this.everythingIsSet())
          throw new Exception("Could not send email.");
        try {
          subAuthenticator subauth = new subAuthenticator(username,password);//smtp authenticator
          props.put("mail.smtp.host", smtphost);
          props.put("mail.smtp.auth","true");
          session = Session.getInstance(props,subauth);
          MimeMessage message = new MimeMessage(session);
          message.setRecipient(Message.RecipientType.TO,new InternetAddress(this.to));
          message.setFrom(new InternetAddress(this.from));
          message.setSubject(this.subject);
          message.setText(this.message);
          Transport.send(message);
        }
        catch(AuthenticationFailedException e1){
          System.out.println("SMTP认证出错!");
        }
        catch (MessagingException e) {
          throw new Exception(e.getMessage());
        }
      }  /* Checks whether all properties have been set or not */
      private boolean everythingIsSet() {
        if((this.to == null)||(this.from == null)||(this.subject == null)||(this.message == null))
          return false;    if((this.to.indexOf("@") == -1) ||(this.to.indexOf(".") == -1))
          return false;    if((this.from.indexOf("@") == -1) ||(this.from.indexOf(".") == -1))
          return false;    return true;
      }  /**
       * 读取属性,完成参数设置
       *
       */
      public void setConfig(){
        InputStream is = getClass().getResourceAsStream("/mail.properties");
        Properties props = new Properties();
        try {
          props.load(is);
        }
        catch (Exception e) {
          System.err.println("不能读取属性文件. " + "请确保mail.properties在CLASSPATH指定的路径中");
          return;
        }
        Enumeration propNames = props.propertyNames();
        while (propNames.hasMoreElements()) {
          String name = (String) propNames.nextElement();
          smtphost = props.getProperty("hostname");
          System.out.println(smtphost);
          if(smtphost == null){
            log("没有指定SMTP服务器");
          }
          username = props.getProperty("username");
          System.out.println(username);
          password = props.getProperty("password");
        }
      }  /**
       * 将文本信息写入日志文件
       * @param msg 日志
       */
      private void log(String msg) {
        log.println(new Date() + ": " + msg);
      }  /**
       * 将文本信息与异常写入日志文件
       * @param e 异常
       * @param msg 日志
       */
      private void log(Throwable e, String msg) {
        log.println(new Date() + ": " + msg);
        e.printStackTrace(log);
      }  /**
       *此内部类定义了smtp认证方法
       */
      public class subAuthenticator extends Authenticator{
        private String userName;
        private String password;
        public subAuthenticator(String user,String pass){
          userName=user;
          password=pass;
        }
        public PasswordAuthentication getPasswordAuthentication(){
          return new PasswordAuthentication(userName,password);
        }
      }
    }