J2EE项目里有用到JavaMail,研究了下。下面是参考资料和我的JavaMail:

JavaMail

J2EE JavaMail

如何使用JSP发送邮件

java利用JavaMail实现邮件功能

package mail;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
     private Properties props;//系统属性,可以get和put属性名值对。
     private Session session;//根据系统属性设置会话
     private Message msg;//MimeMessage为其子类
     //管理员SMTP服务器信息
     private String host;
     private String username;
     private String password;
     private String from;

     public Mail(String host, String username, String password, String from) {
        props = System.getProperties();
        //props.put("mail.smtp.host", "smtp.jspinsider.com");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        session = Session.getDefaultInstance(props);
        msg = new MimeMessage(session);
        this.host = host;
        this.username = username;
        this.password = password;
        this.from = from;
     }

     public void send(String to) throws MessagingException {
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setSubject("测试");
        msg.setText("测试");
        Transport transport = session.getTransport("smtp");
        transport.connect((String)props.get("mail.smtp.host"), username, password);
        transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
        transport.close();
        //Transport.send(msg);
     }
}