java发送邮件

工具类:

package org.plugbase.base.util;

import java.nio.charset.Charset;
import java.util.Properties;

import org.apache.commons.mail.HtmlEmail;
import org.plugbase.common.util.ResourcesUtil;

public class Mail {
   
    public static String getEmailProperties(String property)
    {
        Properties properties;
        String s="";
        try {
            ResourcesUtil.setCharset(Charset.forName("UTF-8"));
            properties = ResourcesUtil.getResourceAsProperties("mailsender.properties");
            s =  properties.getProperty(property).trim();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Waring: Load publisher properties error:" + property);
        }
        return s;
    }
   
     public static boolean send(String sendMail,String title,String content)   
     {  
        String host=getEmailProperties("mail.sender.host");
        String user=getEmailProperties("mail.sender.user");
        String pass=getEmailProperties("mail.sender.pass");
        String mail=getEmailProperties("mail.sender.mail");
       
         boolean f=true;
         HtmlEmail email = new HtmlEmail();      
         email.setTLS(false);  //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验         
         email.setHostName(host);         
         email.setAuthentication(user, pass);
         try    
         {   
             email.addTo(sendMail);   
             email.setFrom(mail);
             email.setSubject(title);
             email.setCharset("utf-8");
             email.setHtmlMsg(content);
             email.send();    
                
         } catch (Exception e) {   
             e.printStackTrace();
             f=false;
         } 
         return f;
     }
     
}

配置文件:

mail.sender.host=192.168.12.48
[email protected]
mail.sender.pass=xxxx
[email protected]
mail.sender.name=商机通

调用:

String html = "您好,"+u.getUsername()+"&nbsp;您的随机密码为:"+password+"&nbsp;请及时<a        href=\"http://im.3jia5.com/webim/login/login.shtml\">登录商机通后台</a>更改新密码。";
 Mail.send(u.getEmail(), "商机通密码找回", html);

你可能感兴趣的:(java发送邮件)