黑马旅游网编写练习(2)–邮箱激活功能
当用户进行注册时,一般会需要使用邮箱进行激活;所以在这里也模拟一下注册后邮箱的激活功能。
首先导入发送邮箱的jar包mail.jar;或者配置maven地址为:
<!--javaMail-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.3</version>
</dependency>
然后编写Java发送邮箱的代码,如下:
/**
* 发邮件工具类
*/
public final class MailUtils {
private static final String USER = "1330495503@qq.com"; // 发件人称号,同邮箱地址
private static final String PASSWORD = "xxxxxxxxxxxx"; // 如果是qq邮箱可以使户端授权码,或者登录密码
/**
*
* @param to 收件人邮箱
* @param text 邮件正文
* @param title 标题
*/
/* 发送验证信息的邮件 */
public static boolean sendMail(String to, String text, String title){
try {
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
// 发件人的账号
props.put("mail.user", USER);
//发件人的密码
props.put("mail.password", PASSWORD);
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthenticat