機能:正常に登録は、登録されたメールアカウントによってメールボックスにアクティベーションメールを送信します。これは、アカウントを使用するために有効にメールボックスアカウントをログに記録するようにユーザに要求します。
プロセス:テーブルに基本的に新しいユーザ・データは、コードフィールドに保存されているユーザテーブルには、ランダムな文字列を持っている必要があります。コードの値に連結されたユーザ(UUID)、送信メールメッセージは、一意のアカウントを見つけるために使用される場合、生成されたランダムコードを追加し、その後、ユーザは、決定された状態、活性化です。
特定には、以下を達成します。
まず、追加のmavenここで必要なメッセージがあるのjavax.mail送ります
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
二、コンフィギュレーション・メールのアプリケーション
ここでは、ライセンスキーを取得する方法にQQメールに使用される試験は、ここをクリックしてください:認証コードを取得します
三、クラスに対応するMailConfigローディング設定
package com.weavewan.sdwan.user.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author wshanshi
* @version 1.0
* @date 2020/2/12 10:32
*/
@Data
@Component
@ConfigurationProperties(prefix = "mail.config")
public class MailConfig {
// 指定发送邮件的主机
private String host;
// 邮件服务器
private String mailService;
// 认证
private String auth;
// 发件人邮箱账号
private String sender;
// 授权码
private String code;
private String sslEnable;
private String sslSocketFactory;
}
四、メールツール
package com.weavewan.sdwan.user.util;
import com.sun.mail.util.MailSSLSocketFactory;
import com.weavewan.sdwan.user.config.MailConfig;
import org.springframework.context.ApplicationContext;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* 邮件发送类
*
* @author wshanshi
* @version 1.0
* @date 2020/2/8 13:57
*/
public class MailUtil {
private ApplicationContext applicationContext = SpringUtils.getApplicationContext();
private MailConfig mailConfig = applicationContext.getBean(MailConfig.class);
private String email;// 收件人邮箱
private String code;// 激活码
public MailUtil(String email, String code) {
this.email = email;
this.code = code;
}
public void run(String subject, String content) {
// 创建连接对象javax.mail.Session
// 创建邮件对象 javax.mail.Message
// 发送一封激活邮件
String sender = mailConfig.getSender();// 发件人电子邮箱
String host = mailConfig.getHost(); // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
Properties properties = System.getProperties();// 获取系统属性
properties.setProperty(mailConfig.getMailService(), host);// 设置邮件服务器
properties.setProperty(mailConfig.getAuth(), "true");// 打开认证
try {
// QQ邮箱需要下面这段代码,163邮箱不需要
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put(mailConfig.getSslEnable(), "true");
properties.put(mailConfig.getSslSocketFactory(), sf);
// 获取默认session对象
Session session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(sender, mailConfig.getCode()); // 发件人邮箱账号、授权码
}
});
// 创建邮件对象
Message message = new MimeMessage(session);
// 设置发件人
message.setFrom(new InternetAddress(sender));
// 设置接收人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
// 设置邮件主题
message.setSubject(subject);
// 设置邮件内容
message.setContent(content, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
System.out.println("邮件成功发送!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
第五に、メッセージを送信するための呼び出し
=新しい新しいMailUtil mailUtil MailUtil(email.getS()、code.getS());
mailUtilの設定パラメータは2つです:受信者のメールボックス(メールボックスユーザー)、ランダムコード認証コード(UUID)。
次のような効果がある:
付属書:UUIDを生成します
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
String uuidStr = str.replace("-", "");