0
我是相當新的Java和春季啓動。我已經開始嘗試並繪製出各種功能和參數。Java的春季啓動 - 忘記密碼 - 與速度的電子郵件服務?
忘記密碼 - 電子郵件發送。
//__ send email notification SimpleEmailService.generateEmail("forgotPasswordTemplate", data);
這是舊的電子郵件服務像 - 什麼是與Java春天啓動的equivlant? - 什麼庫導入?
package controller;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import com.mongodb.BasicDBObject;
import javax.mail.internet.MimeMessage;
import java.io.StringWriter;
public class SimpleEmailService {
private static JavaMailSender mailSender;
private static VelocityEngine velocityEngine;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
public static void generateEmail(final String templateType, final BasicDBObject user) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
String velocityTemplateName = null;
String subject = null;
if(templateType.equals("forgotPasswordTemplate")){
velocityTemplateName = "forgot_password";
subject = "Forgot Password";
user.put("imgPath", "http://livesite.com/images/emails/forgot_password/");
}
if(templateType.equals("registerTemplate")){
velocityTemplateName = "registration_confirmation";
subject = "Registration";
user.put("imgPath", "http://livesite.com/images/emails/registration_confirmation/");
}
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo("[email protected]");
message.setFrom("[email protected]"); // could be parameterized...
message.setSubject(subject);
VelocityContext model = new VelocityContext();
model.put("user", user);
String text = applyTemplate(model, velocityTemplateName+".vm");
message.setText(text, true);
}
};
mailSender.send(preparator);
}
public static String applyTemplate(VelocityContext data, String templateName){
StringWriter sw= new StringWriter();
Template t = velocityEngine.getTemplate("net/velocity/templates/"+templateName);
t.merge(data, sw);
return sw.toString();
}
}
這是我當前的代碼庫的樣子 - 我基本上是想一個電源建設reactjs與API - 所以在架構方面 - 的/ API將是一個GET服務 - - 另一個是來自表格的帖子。
-
package controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.tomcat.util.log.SystemLogHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import domain.Account;
import repo.AccountRepository;
@RestController
public class MyService {
@Autowired
private AccountRepository accountRepository;
@RequestMapping(value = "/api", method = RequestMethod.GET)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> index(@RequestParam(value="name", required=false, defaultValue="admin1") String name) throws Exception {
Account acc = accountRepository.findByUsername(name);
return new ResponseEntity<>(acc, HttpStatus.OK);
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> login(
@RequestParam(value="email", required=false, defaultValue="email") String email,
@RequestParam(value="password", required=false, defaultValue="password") String password,
HttpServletRequest request
) throws Exception {
List<Account> acc = accountRepository.findByEmail(email);
HttpSession session = request.getSession(true);
session.setAttribute("user", acc);
System.out.println("logged>>>"+acc);
System.out.println("session>>>"+session);
return new ResponseEntity<>(acc, HttpStatus.OK);
}
@SuppressWarnings("unchecked")
@RequestMapping(value = "/logout", method = RequestMethod.GET)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> logout(
HttpServletRequest request
) throws Exception {
HttpSession session = request.getSession(true);
List<Account> acc = (List<Account>) session.getAttribute("user");
session.removeAttribute("user");
System.out.println("logout>>>"+acc);
System.out.println("session>>>"+session);
return new ResponseEntity<>(acc, HttpStatus.OK);
}
@RequestMapping(value = "/forgot", method = RequestMethod.GET)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> forgot(
@RequestParam(value="email", required=false, defaultValue="email") String email
) throws Exception {
List<Account> acc = accountRepository.findByEmail(email);
return new ResponseEntity<>(acc, HttpStatus.OK);
}
@RequestMapping(value = "/edit", method = RequestMethod.GET)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> edit(
@RequestParam(value="username", required=false, defaultValue="username") String username,
@RequestParam(value="password", required=false, defaultValue="password") String password,
@RequestParam(value="email", required=false, defaultValue="email") String email
) throws Exception {
Account acc = accountRepository.findByUsername(username);
return new ResponseEntity<>(acc, HttpStatus.OK);
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> register(
@RequestParam(value="username", required=false, defaultValue="username") String username,
@RequestParam(value="password", required=false, defaultValue="password") String password,
@RequestParam(value="email", required=false, defaultValue="email") String email
) throws Exception {
Account checkAccount = accountRepository.findByUsername(username);
System.out.println("checkAccount>>>"+checkAccount);
if (checkAccount == null) {
try {
// if not create some
Account newAcc = accountRepository.saveAndFlush(new Account(
username,
password,
email
));
System.out.println("newAcc>>>"+checkAccount);
return new ResponseEntity<>(newAcc, HttpStatus.OK);
}
catch (Exception e) {
e.printStackTrace();
System.out.println("error adding user>>>"+checkAccount);
return new ResponseEntity<>(e, HttpStatus.OK);
}
}else{
// String e = "user exists";
System.out.println("user already exists>>>"+checkAccount);
return new ResponseEntity<>(checkAccount, HttpStatus.OK);
}
}
}
我覺得這個教程可以幫助https://www.quickprogrammingtips.com/spring-boot/how-to-send-email-from-spring-boot-applications.html你也可以閱讀參考https://開頭docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/htmlsingle/#mail https://docs.spring.io/spring-boot/docs/current/reference/html/boot-功能 - email.html –
好了,所以它是類似的 - 一個例子使用「的freemarker模板」和其他的「速度」 - 任何偏好嗎?我有一個使用Velocity模板 –
看到這個職位,也既查看模板引擎的文檔,它會幫助你選擇能滿足你需求的一個https://github.com/spring-projects/spring-boot/commit舊代碼/ 86d87f6b8ad6816b1d98bad7ab68bcdad1bf5cf0 –