码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现发邮件功能

时间:2019-10-10 15:33:17      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:auth   hostname   内容   hot   发件人   string   sub   eth   contain   

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.mail.HtmlEmail;

public class SendMailUtil {
    
    private static final String from = "发件人地址";
     
    private static final String fromName = "邮件的标题";
 
    private static final String charSet = "utf-8";
 
    private static final String username = "发件人用户名";
 
    private static final String password = "发件人授权码";
 
    private static final String subject = "邮件的主题";
 
    private static Map<String, String> hostMap = new HashMap<String, String>();
    
    static {

        // 126
        hostMap.put("smtp.126", "smtp.126.com");
        
        // qq
        hostMap.put("smtp.qq", "smtp.qq.com");
 
        // 163
        hostMap.put("smtp.163", "smtp.163.com");
 
        // sina
        hostMap.put("smtp.sina", "smtp.sina.com");
 
        // tom
        hostMap.put("smtp.tom", "smtp.tom.com");
 
        // 263
        hostMap.put("smtp.263", "smtp.263.net");
 
        // yahoo
        hostMap.put("smtp.yahoo", "smtp.mail.yahoo.com");
 
        // hotmail
        hostMap.put("smtp.hotmail", "smtp.live.com");
 
        // gmail
        hostMap.put("smtp.gmail", "smtp.gmail.com");
        hostMap.put("smtp.port.gmail", "465");
 
        // sygongzhi.com
        hostMap.put("smtp.sygongzhi", "smtp.mxhichina.com");
 
    }
 
    private static boolean validate = true;// 是否需要身份验证
 
    public static Properties getProperties() throws Exception {
        Properties p = new Properties();
        p.put("mail.smtp.host", getHost(from));
        p.put("mail.smtp.port", getSmtpPort(from));
        p.put("mail.smtp.auth", validate ? "true" : "false");
        return p;
    }
 
    public static String getHost(String email) throws Exception {
        Pattern pattern = Pattern.compile("\\w+@(\\w+)(\\.\\w+){1,2}");
        Matcher matcher = pattern.matcher(email);
        String key = "unSupportEmail";
        if (matcher.find()) {
            key = "smtp." + matcher.group(1);
        }
        if (hostMap.containsKey(key)) {
            return hostMap.get(key);
        } else {
            throw new Exception("unSupportEmail");
        }
    }
 
    public static int getSmtpPort(String email) throws Exception {
        Pattern pattern = Pattern.compile("\\w+@(\\w+)(\\.\\w+){1,2}");
        Matcher matcher = pattern.matcher(email);
        String key = "unSupportEmail";
        if (matcher.find()) {
            key = "smtp.port." + matcher.group(1);
        }
        if (hostMap.containsKey(key)) {
            return Integer.parseInt(hostMap.get(key));
        } else {
            return 25;
        }
    }
 
    /**
     * 发送普通邮件
     * 
     * @param toMailAddr
     *            收信人地址
     * @param subject
     *            email主题
     * @param message
     *            发送email信息
     */
    public static void sendCommonMail(String toMailAddr, String subject, String message) {
        HtmlEmail hemail = new HtmlEmail();
        try {
            hemail.setHostName(getHost(from));
            hemail.setSmtpPort(getSmtpPort(from));
            hemail.setCharset(charSet);
            //多个收件人
            String[] toMailAddrs = toMailAddr.split(",");
            for(int i = 0; i < toMailAddrs.length; i++){
                hemail.addTo(toMailAddrs[i]);
            }
            //一个收件人
            //hemail.addTo(toMailAddr);
            hemail.setFrom(from, fromName);
            hemail.setAuthentication(username, password);
            hemail.setSubject(subject);
            hemail.setMsg(message);
            hemail.send();
            System.out.println("邮件发送成功!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("邮件发送失败!");
        }
 
    }
    
    public static void main(String[] args) {
        
        sendCommonMail("收件人地址,收件人地址","邮件主题","邮件内容");
//      sendCommonMail("收件人地址","邮件主题","邮件内容");
        
    }
 
}    

 

Java实现发邮件功能

标签:auth   hostname   内容   hot   发件人   string   sub   eth   contain   

原文地址:https://www.cnblogs.com/3b2414/p/11647931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!