发送邮件以及获取发件方ip的方法

需导入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", "标题", "文本内容");
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值