需导入mail.jar包,删除j2ee自带的mail文件
package ndy.quantgroup.util;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author nidayu
* @date 2014-12-16
* @Description xxx
* @version V1.0
*/
public class SendEmailUtil {
/**
* xxx
*
* @author nidayu
* @param
* @return
*/
public static void send(String emailName, String emailPwd, String[] to, String smtpHost, String title, String content) {
try {
SmtpAuth sa = new SmtpAuth();
sa.getuserinfo(emailName, emailPwd);
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpHost);
InternetAddress fromAddress = new InternetAddress(emailName);
InternetAddress[] toAddresss = new InternetAddress[to.length];
for (int len = 0; len < to.length; len++) {
if (to[len] != null && to[len].length() > 1) {
toAddresss[len] = new InternetAddress(to[len]);
}
}
int i = 0;
while (i < toAddresss.length) {
if (toAddresss[i] != null) {
Session mailSession = Session.getDefaultInstance(props, sa);
MimeMessage testMessage = new MimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddresss[i]);
testMessage.setSentDate(new java.util.Date());
testMessage.setSubject(title);
testMessage.setText(content + "\r\tip:" + getHostIp());
Transport.send(testMessage);
System.out.println("A mail have been sent to " + to[i]);
}
i++;
}
} catch (Exception e) {
System.out.println("邮件发送失败:");
e.printStackTrace();
}
}
public static String getHostIp() {
String sIP = "";
InetAddress ip = null;
try {
// 如果是Windows操作系统
if (isWindowsOS()) {
ip = InetAddress.getLocalHost();
}
// 如果是Linux操作系统
else {
boolean bFindIP = false;
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
// ----------特定情况,可以考虑用ni.getName判断
// 遍历所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() // 127.开头的都是lookback地址
&& ip.getHostAddress().indexOf(":") == -1) {
bFindIP = true;
break;
}
}
}
}
} catch (Exception e) {
}
if (null != ip) {
sIP = ip.getHostAddress();
}
return sIP;
}
public static boolean isWindowsOS() {
boolean isWindowsOS = false;
String osName = System.getProperty("os.name");
if (osName.toLowerCase().indexOf("windows") > -1) {
isWindowsOS = true;
}
return isWindowsOS;
}
public static class SmtpAuth extends javax.mail.Authenticator {
private String user, password;
public void getuserinfo(String getuser, String getpassword) {
user = getuser;
password = getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user, password);
}
}
}
最后测试
public static void main(String[] args) {
SendEmailUtil.send("邮箱号@163.com", "邮箱密码", new String[] { "对方邮箱" }, "smtp.163.com", "标题", "文本内容");
}