package com.zhouyong.util;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MyJMail {
public static void sendMail(String mailname,String uname,String upass) {
String host = "smtp.163.com"; // 指定的smtp服务器
String from = "zhouyongkongda@163.com"; // 邮件发送人的邮件地址
String to = mailname;// kriss1984@yeah.net// 邮件接收人的邮件地址
final String username = "zhouyongkongda"; //发件人的邮件帐户
final String password = "1986825"; //发件人的邮件密码
// 创建Properties 对象
Properties props = System.getProperties();
// 添加smtp服务器属性
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
// 创建邮件会话
Session session = Session.getDefaultInstance(props, new Authenticator(){
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// 定义邮件信息
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject("来自zy的邮件");
message.setText("http://127.0.0.1:8080/javamail/servlet/LoginServlet?flag=1&uname="+uname+"&upass="+upass);
Transport.send(message);
System.out.println("发送成功");
} catch (MessagingException e) {
System.out.println("发送失败");
e.printStackTrace();
}
}
}