我想在用户完成注册后向用户发送电子邮件。
@PostMapping("registration")
public String registration(Model model,
@Valid @ModelAttribute("cool") User user,
BindingResult bindingresult) {
if (bindingresult.hasErrors()) {
model.addAttribute("cool", user);
return "registration";
}
model.addAttribute("user", user);
data.addUser(user);
//------------------------Email----------------//
//------------------------Email----------------//
return "success";
}
我的电子邮件地址是:[email protected]
我想从我的电子邮件(或从本地主机)向用户的电子邮件发送电子邮件。
String host = "smtp.gmail.com";
int port = 587;
final String username = "[email protected]";
final String password = "your password";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", port);
Session session = Session.getInstance(props,new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}} );
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,\n\n No spam to my email, please!");
Transport transport = session.getTransport("smtp");
transport.connect(host, port, username, password);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
您可以使用通过TLS的Gmail SMTP
private static String USER_NAME = "Gmail Username"; // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "gbxxfgfhujdfqndd"; // GMail password
public static boolean sendEmail(String RECIPIENT, String sub, String title, String body, String under_line_text,
String end_text) {
String from = USER_NAME;
String pass = PASSWORD;
String[] to = {
RECIPIENT
}; // list of recipient email addresses
String subject = sub;
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<div style=\" background-color: white;width: 25vw;height:auto;border: 20px solid grey;padding: 50px;margin:100 auto;\">\n" +
"<h1 style=\"text-align: center;font-size:1.5vw\">" + title + "</h1>\n" + "<div align=\"center\">" +
"<h2 style=\"text-align: center;font-size:1.0vw\">" + body + "</h2>" +
"<h3 style=\"text-align: center;text-decoration: underline;text-decoration-color: red;font-size:0.9vw\">" +
under_line_text + "</h3><br><h4 style=\"text-align: center;font-size:0.7vw\">" + end_text +
" </h4></div>";
messageBodyPart.setContent(htmlText, "text/html");
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (AddressException ae) {
ae.printStackTrace();
} catch (MessagingException me) {
me.printStackTrace();
}
return true;
}
或通过SSL发送的Gmail,应在其中添加
//change port number
prop.put("mail.smtp.port", "465");
prop.put("mail.smtp.socketFactory.port", "465");
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
输出:
也可以相应地更改BodyPart
,因为我已经包含了html
响应模板
我建议您访问您的Google帐户并生成一个新的应用程序密码,这可以对您的密码字段进行编码,而不用作纯文本
否则,您将提出此异常
javax.mail.AuthenticationFailedException:534-5.7.9需要特定于应用程序的密码。
希望它有所帮助!