Java实现发邮件功能

Posted 抟鹏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现发邮件功能相关的知识,希望对你有一定的参考价值。

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实现发邮件功能的主要内容,如果未能解决你的问题,请参考以下文章

Java实现发邮件功能

如何用java实现发邮件功能

Java实现发邮件功能

Java实战-用Java mail实现Exchange发邮件给你喜欢的人

Java发邮件简单实现

C#使用Windows窗体实现发邮件功能