public static voidsendMail(String email,String emailMsg)
throwsAddressException,MessagingException {
// 1.创建一个程序与邮件服务器会话对象Session
Properties props =newProperties();
props.setProperty("mail.host","smtp.qq.com");
props.setProperty("mail.transport.protocol","smtp");
props.setProperty("mail.smtp.auth","true");
props.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.port","465");
props.setProperty("mail.smtp.socketFactory.port","465");
//创建验证器
Authenticator auth =newAuthenticator() {
publicPasswordAuthenticationgetPasswordAuthentication() {
return newPasswordAuthentication("[email protected]","jgqjugzwkxkkbeii");
//输入的qq邮箱的用户名和授权码才行。
}
};
Session session = Session.getInstance(props,auth);
// 2.创建一个Message,它相当于是邮件内容
Message message =newMimeMessage(session);
message.setFrom(newInternetAddress("[email protected]"));//设置发送者
message.setRecipient(RecipientType.TO, newInternetAddress(email));//设置发送方式与接收者
message.setSubject("用户激活");
// message.setText("这是一封激活邮件,请点击");
message.setContent(emailMsg,"text/html;charset=utf-8");
// 3.创建Transport用于将邮件发送
Transport.send(message);
}
}