SendMailUitls:
package com.duocy.util;
import java.net.InetAddress;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendMailUitls {
public static void senMail(String toMail, String code) throws Exception {
InetAddress addrs = InetAddress.getLocalHost();
String addr=addrs.getHostAddress();
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp"); // 连接协议
properties.put("mail.smtp.host", "smtp.163.com"); // 主机名
properties.put("mail.smtp.port", 25); // 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.socketFactory", "true"); // 设置是否使用ssl安全连接 (一般都使用)
properties.put("mail.debug", "false"); // 设置是否显示debug信息 true 会在控制台显示相关信息
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress("15823161396@163.com", "10086abc"));
// 设置收件人地址
message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress(toMail) });
// 设置邮件标题
message.setSubject("由JavaMail发出的测试邮件");
// 设置邮件内容
message.setContent("<h2>请点击以下链接址激活注册:</br>(若无法点击,请复制到浏览器中打开)</h2><h4><a herf='http://"+addr+":8080/duocy/SelectClientByCode?code="+code+"'>http://"+addr+":8080/duocy/SelectClientByCode?code="+code+"</a></h4>","text/html;Charset=UTF-8");
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
transport.connect("15823161396@163.com", "10086abc"); //密码为刚才得到的授权码
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
System.out.println("用户激活码:"+code);
System.out.println("主机地址:"+addr);
}
/*public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()){
System.out.println(e.nextElement());
}
}*/
}