You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.0 KiB
58 lines
2.0 KiB
package mathlearning.service;
|
|
|
|
import javax.mail.*;
|
|
import javax.mail.internet.*;
|
|
import java.util.Properties;
|
|
|
|
public class EmailService {
|
|
private final String host;
|
|
private final String port;
|
|
private final String username;
|
|
private final String password;
|
|
private final boolean auth;
|
|
|
|
public EmailService(String host, String port, String username, String password, boolean auth) {
|
|
this.host = host;
|
|
this.port = port;
|
|
this.username = username;
|
|
this.password = password;
|
|
this.auth = auth;
|
|
}
|
|
|
|
public boolean sendVerificationCode(String toEmail, String verificationCode) {
|
|
try {
|
|
Properties props = new Properties();
|
|
props.put("mail.smtp.host", host);
|
|
props.put("mail.smtp.port", port);
|
|
props.put("mail.smtp.auth", auth);
|
|
props.put("mail.smtp.starttls.enable", "true");
|
|
|
|
Session session = Session.getInstance(props, new Authenticator() {
|
|
@Override
|
|
protected PasswordAuthentication getPasswordAuthentication() {
|
|
return new PasswordAuthentication(username, password);
|
|
}
|
|
});
|
|
|
|
Message message = new MimeMessage(session);
|
|
message.setFrom(new InternetAddress(username));
|
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
|
|
message.setSubject("数学学习软件 - 注册验证码");
|
|
|
|
String emailContent = "尊敬的用户:\n\n" +
|
|
"您的注册验证码是:" + verificationCode + "\n\n" +
|
|
"该验证码有效期为10分钟。\n\n" +
|
|
"如果您没有注册本软件,请忽略此邮件。\n\n" +
|
|
"数学学习软件团队";
|
|
|
|
message.setText(emailContent);
|
|
|
|
Transport.send(message);
|
|
return true;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
}
|