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("收件人地址","邮件主题","邮件内容");
}
}
maven添加jar包
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
如果出现:
Sending the email to the following server failed : smtp.163.com:25 或者
Couldn't connect to host, port: smtp.163.com, 25; timeout -1 等一些错误,
解决方案:
1,向阿里云提交申请解封25端口;
2,放弃25端口,使用ssl加密并改用465端口;
在apache commons-email中的使用如下:
//启用ssl加密
hemail.setSSLOnConnect(true);
//使用465端口(不设置也可,ssl默认为465)
hemail.setSslSmtpPort("465");