Skip to content

Instantly share code, notes, and snippets.

@mevsungur
Created February 21, 2020 22:41
Show Gist options
  • Save mevsungur/d248678f291d13d66d76e720151d43f5 to your computer and use it in GitHub Desktop.
Save mevsungur/d248678f291d13d66d76e720151d43f5 to your computer and use it in GitHub Desktop.
Spring Boot Mail with Tymeleaf
@Autowired
SpringTemplateEngine templateEngine;
@Async
public void htmlEpostaGonder(String kime, String konu) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
// Context Tymeleaf'ten import edilmelidir.
Context context = new Context();
context.setVariable("metin", "Bu metin Thymeleaf ile oluşturulmuştur. ");
context.setVariable("list", Arrays.asList("Thymeleaf", "Liste", "İçinde", "Dönebilir"));
String icerik = templateEngine.process("MerhabaDunya.html", context);
try {
helper.setFrom(from);
helper.setTo(kime);
helper.setSubject(konu);
helper.setText(icerik,true);
//Epostaya ek yüklemek istiyorsanız aşağıdaki alandan dosya eklemesi yapabilirsiniz.
//helper.addAttachment("name", new File(""));
} catch (MessagingException e) {
log.error("E-posta gönderimi yapılırken hata oluştu. " + e.getMessage());
e.printStackTrace();
}
mailSender.send(message);
log.info(kime + " adresine başarılı e-posta gönderimi yapıldı.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment