[总结]Java发送Email
(参考: http://www.iteye.com/topic/157290)
用到的API(Jar包):
1. 从oracle的网站下载JavaMail API.
地址: www.oracle.com/technetwork/java/javamail/
2. 从oracle的网站下载JavaBeans Activation Framework.
JavaMail需要这个框架. 不过如果JDK是1.6的话就不用下了, 已经集成了.
3. 下载apache的Commons Email API.
地址: commons.apache.org/email/
例子:
import org.apache.commons.mail.DefaultAuthenticator;import org.apache.commons.mail.EmailException;import org.apache.commons.mail.SimpleEmail;/** * 邮件发送工具。 * 本例在jdk1.6下测试. * @author manxisuo */public class EmailTool{public static boolean sendEmail(String address, String subject, String msg){boolean success = true;SimpleEmail email = new SimpleEmail();// smtp邮件服务器email.setHostName("smtp.gmail.com");// 端口email.setSmtpPort(587);// smtp认证:用户名和密码email.setAuthenticator(new DefaultAuthenticator("user","pw"));// Set or disable the TLS encryptionemail.setTLS(true);// 编码格式email.setCharset("UTF-8");// 标题email.setSubject(subject);try{// 发信人email.setFrom("somebody@gmail.com");// 内容email.setMsg(msg);// 收信人email.addTo(address);email.send();}catch (EmailException e){success = false;e.printStackTrace();}return success;}}