mail.properties
#邮箱服务器 mail.host.name=smtp.163.com #发送方用户名 mail.username=**@163.com #用户名 mail.password=** #接收方 mail.to=**@qq.com #发送方 mail.from=**@163.com
代码如下:
/** * 发送邮件 */ public void sendMail(String email, Phonefee phonefee) throws Exception { // 读取mail.properties ResourceBundle bundle = PropertyResourceBundle.getBundle("mail"); String host = bundle.getString("mail.host.name"); String username = bundle.getString("mail.username"); String password = bundle.getString("mail.password"); String from = bundle.getString("mail.from");
//接收方 Collection collection = new ArrayList<InternetAddress>(); collection.add(new InternetAddress(email)); HtmlEmail htmlEmail = new HtmlEmail(); htmlEmail.setCharset("utf-8"); htmlEmail.setSubject("中国移动手机话费"); htmlEmail.setFrom(from, "中国移动", "gbk"); htmlEmail.setTo(collection); htmlEmail.setSentDate(Calendar.getInstance().getTime());
//接收邮件内容 htmlEmail.setHtmlMsg(phonefee.getDatacontent());
htmlEmail.setDebug(false); htmlEmail.setHostName(host); htmlEmail.setAuthentication(username, password); htmlEmail.send(); }