java实现邮件发送功能工具类
使用mail-user.jar, mail.jar实现java发送邮件的功能。
发送TEXT方式和发送Html方式。
发送TEXT方式和发送Html方式。
- package com.***.***.core.util;
- import java.io.BufferedInputStream;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import com.java.entity.Email;
- import com.java.mail.SimpleMailSender;
- public class EmailUtil {
- private static Email mailInfo = new Email();
- private static Properties prop = new Properties();
- //获取发送服务器配置
- static {
- InputStream in = null;
- try {
- String filePath = EmailProperties.class.getResource("/").getPath()+"email.properties";
- in = new BufferedInputStream(new FileInputStream(filePath));
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- try {
- prop.load(in);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 根据key获取properties的value
- *
- * @param key
- * @return
- */
- public static String getValue(String key) {
- return prop.getProperty(key).trim();
- }
- /**
- * 封装邮件发送对象
- *
- * @param subject
- * @param content
- * @param toAddress
- */
- private static void getMailInfo(String subject, String content,
- String toAddress) {
- mailInfo.setMailServerHost(getValue("mailServerHost"));//发送服务器地址
- mailInfo.setMailServerPort(getValue("mailServerPort"));//发送服务端口
- mailInfo.setValidate(true);
- mailInfo.setUserName(getValue("userName"));//发送者账号
- mailInfo.setPassword(getValue("password"));// 发送者邮箱密码
- mailInfo.setFromAddress(getValue("fromAddress"));//发送地址
- mailInfo.setToAddress(toAddress.trim());//接收地址,邮箱地址
- if (subject != null && !"".equals(subject)) {
- mailInfo.setSubject(subject);//邮件主题
- } else {
- mailInfo.setSubject(getValue("subject"));
- }
- if (content != null && !"".equals(content)) {
- mailInfo.setContent(content);//邮件内容
- } else {
- mailInfo.setContent(getValue("content"));
- }
- }
- /**
- * 以文本文件的形式发送邮件
- *
- * @param subject
- * 主题
- * @param content
- * 内容
- * @param toAddress
- * 目标地址
- * @return
- */
- public static boolean sendContentEmail(String subject, String content,
- String toAddress) {
- if (toAddress != null && !"".equals(toAddress)) {
- getMailInfo(subject, content, toAddress);
- SimpleMailSender sms = new SimpleMailSender();
- return sms.sendTextMail(mailInfo);
- }
- return false;
- }
- /**
- * 以html格式发送邮件信息
- *
- * @param subject
- * 主题
- * @param content
- * 内容
- * @param toAddress
- * 目标地址
- * @return
- */
- public static boolean sendHtmlEmail(String subject, String content,
- String toAddress) {
- if (toAddress != null && !"".equals(toAddress)) {
- getMailInfo(subject, content, toAddress);
- return SimpleMailSender.sendHtmlMail(mailInfo);
- }
- return false;
- }
- public static String contentToHtml(String url) {
- StringBuffer email = new StringBuffer(
- "<html xmlns='http://www.w3.org/1999/xhtml'>");
- email.append("<head>");
- email.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
- email.append("<title>**订单支付</title>");
- email.append("</head><body>");
- email.append("<p><u><a href=\""+url+"\">"+url+"</a></u> </p>");
- email.append("</body></html>");
- return email.toString();
- }
- public static void main(String[] args) {
- }
- }