java smtp 发送邮件的例子

QQ邮箱测试不行,用163的可以

package net.e_lian.bpm.util.ueum;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.mail.MessagingException;
import net.e_lian.bpm.core.freeflow.notification.IMSystemMsg;
import net.e_lian.bpm.util.ldap.LdapUtil;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;

public class MailUtil {
	// 定义日志对象
	private static Logger logger = Logger
				.getLogger(MailUtil.class.getName());
    //protected static final Logger logger = Logger.getLogger(getClass()); 
    private static Properties props = null;
    public static final String ENCODEING = "UTF-8"; 
    private  static String host; // 服务器地址  
    
    private static String sender; // 发件人的邮箱  
  
    private static String name; // 发件人昵称  
  
    private static String username; // 账号  
  
    private static String password; // 密码  
  
    private static  String subject; // 主题  
    static {
		if (null == props) {
			props = new Properties();
			URL propsUrl = LdapUtil.class.getClassLoader().getResource(
					"a.properties");
			if (propsUrl == null) {
				throw new IllegalStateException("a.properties missing");
			}
			try {
				props.load(propsUrl.openStream());
				host = (String) props.get("SMTP_EMAIL_HOST");
				sender = (String) props.get("SMTP_EMAIL_SENDER");
				name = (String) props.get("SMTP_EMAIL_NAME");
				username = (String) props.get("SMTP_EMAIL_USERNAME");
				password = (String) props.get("SMTP_EMAIL_PASSWORD");
				subject = (String) props.get("SMTP_EMAIL_SUBJECT");
			} catch (IOException e) {
				/*if (logger.isLoggable(Level.WARNING)) {
					logger.log(Level.WARNING, "", e);
				}*/
			}
		}
	}
    /**
     * 发送邮件
     * @param emailMap
     * 发送邮件参数
     * @return
     * @throws MalformedURLException
     * @throws MessagingException
     */
    public boolean send(Map<String,String> emailMap) throws MalformedURLException, MessagingException {      
    	// 发送email  
        HtmlEmail email = new HtmlEmail();//创建能加附件内容为HTML文本的邮件、HTML直接内联图片但必须用setHtmlMsg()传邮件内容 
     
        
        try {  
            // 这里是SMTP发送服务器的名字:163的如下:"smtp.163.com"  
            email.setHostName(host); 
            // 字符编码集的设置  
            email.setCharset(ENCODEING);  
            // 收件人的邮箱  
            email.addTo(emailMap.get("receiver"));
            // 发送人的邮箱  
            email.setFrom(sender, name);  
            // 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码  
            email.setAuthentication(username, password);  
            // 要发送的邮件主题  
            email.setSubject(subject);   
            email.setHtmlMsg(emailMap.get("htmlMsg"));
            // 发送  
            email.send();  
            if (logger.isDebugEnabled()) {  
                logger.debug(sender + " 发送邮件到 " + emailMap.get("receiver"));  
            }  
            return true;  
        } catch (EmailException e) {  
            e.printStackTrace();  
            logger.info(sender + " 发送邮件到 " + emailMap.get("receiver") 
                    + " 失败");  
            return false;  
        }  
    }  
    
    public  void sendEmail(String receiverEmails,IMSystemMsg imMsgs) throws MalformedURLException, MessagingException{
    	Map<String,String> emailMap=new HashMap<String,String>();
        emailMap.put("receiver", receiverEmails);
        emailMap.put("htmlMsg", "<h1>Hello world</h1>"+"<a href=''>连接</a>");
        send(emailMap); 
    }
    
    
    public static void main(String[] args) throws MalformedURLException, MessagingException {  
        Map<String,String> emailMap=new HashMap<String,String>();
        emailMap.put("receiver", "350769850@qq.com");
        emailMap.put("htmlMsg", "<h1>Hello world</h1>"+"<a href=''>连接</a>");
        new MailUtil().send(emailMap);  
    }  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值