package cn.pyd.itcaststore.utils; import java.net.Authenticator; import java.net.PasswordAuthentication; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class MailUtils { public static void sendMail(String email,String emailMsg) throws AddressException,MessagingException{ //1.创建Properties对象,并设置邮件服务器的基本信息 Properties props=new Properties(); //1.1设计邮件传输协议为SMIP props.put("mail.smtp.host", "smtp.163.com");//存储发送邮件服务器的信息 props.put("mail.smtp.auth", "true");//同时通过验证 props.setProperty("mail.transport.protocol", "smtp"); //1.4创建验证器 javax.mail.Authenticator auth=new javax.mail.Authenticator() { public javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication("用户名", "密码"); } }; //实例化邮件会话Session Session session=Session.getInstance(props, auth); //2.创建一个Message,该对象相当于邮件内容 Message message=new MimeMessage(session); //2.1设置发送者 message.setFrom(new InternetAddress("用户名@163.com")); //2.2设置发送方式与接受者,邮件接受者在调用sendMail()方法时通过参数传递过来 message.setRecipient(RecipientType.TO,new InternetAddress(email)); //2.3设置邮件发送主题 message.setSubject("用户激活"); message.setContent(emailMsg, "text/html;charset=utf-8"); //3.发送邮件 Transport.send(message); } }

庞玉栋个人博客、人生苦短-我用Python