package com.tarena.netctoss.test; import java.util.Date; 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; public class MailTest { public static boolean send(String to,String subject,String content){ //第一步:创建session Properties pros=new Properties(); //指定邮件的传输协议 pros.put("mail.smtp.host", "smtp.163.com"); pros.put("mail.smtp.auth", "true"); Session session=Session.getDefaultInstance(pros); session.setDebug(true); try{ Transport transport = session.getTransport("smtp"); transport.connect("smtp.163.com","snail_zhou","***************");//服务器,邮箱号(不要后面的@163.com),邮箱的密码 MimeMessage message=new MimeMessage(session);
message.setSubject(subject);//设置主题 message.setContent(content,"text/html;charset=utf-8");//设置内容 message.setSentDate(new Date());//发送日期 message.setFrom(new InternetAddress("snail_zhou@163.com"));//发件人邮箱 transport.sendMessage(message, InternetAddress.parse(to));//发送 return true; }catch(Exception e){ e.printStackTrace(); } return false; } public static void main(String[] args) { MailTest.send("972661036@qq.com", "验证码", "123456"); } }