java实现邮件发送功能工具类

本文介绍了一种使用Java实现邮件发送的方法,包括发送纯文本邮件和HTML格式邮件。通过加载配置文件并设置邮件参数,利用SimpleMailSender完成邮件的发送过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用mail-user.jar, mail.jar实现java发送邮件的功能。

发送TEXT方式和发送Html方式。




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) {

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值