博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

java邮件发送小实例

Posted on 2012-11-12 10:14  feng1825  阅读(280)  评论(0编辑  收藏  举报

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(); }