Guest User

Untitled

a guest
Jun 19th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. <dependency>
  2. <groupId>it.ozimov</groupId>
  3. <artifactId>spring-boot-email-core</artifactId>
  4. <version>0.5.0</version>
  5. </dependency>
  6.  
  7. spring.mail.host: smtp.gmail.com
  8. spring.mail.port: 587
  9. spring.mail.username: hari.seldon@gmail.com
  10. spring.mail.password: Th3MuleWh0
  11. spring.mail.properties.mail.smtp.auth: true
  12. spring.mail.properties.mail.smtp.starttls.enable: true
  13. spring.mail.properties.mail.smtp.starttls.required: true
  14.  
  15. package com.test;
  16.  
  17. import com.google.common.collect.Lists;
  18. import it.ozimov.springboot.mail.model.Email;
  19. import it.ozimov.springboot.mail.model.defaultimpl.DefaultEmail;
  20. import it.ozimov.springboot.mail.service.EmailService;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23.  
  24. import javax.mail.internet.InternetAddress;
  25. import java.io.UnsupportedEncodingException;
  26.  
  27. import static com.google.common.collect.Lists.newArrayList;
  28.  
  29. @Service
  30. public class TestService {
  31.  
  32. @Autowired
  33. private EmailService emailService;
  34.  
  35. public void sendEmail() throws UnsupportedEncodingException {
  36. final Email email = DefaultEmail.builder()
  37. .from(new InternetAddress("hari.seldon@the-foundation.gal",
  38. "Hari Seldon"))
  39. .to(newArrayList(
  40. new InternetAddress("the-real-cleon@trantor.gov",
  41. "Cleon I")))
  42. .subject("You shall die! It's not me, it's Psychohistory")
  43. .body("Hello Planet!")
  44. .encoding("UTF-8").build();
  45.  
  46. emailService.send(email);
  47. }
  48.  
  49. }
  50.  
  51. Let's do it in the main application, where we'll send the email just after starting and initialising the Spring context.
  52.  
  53. package com.test;
  54.  
  55. import it.ozimov.springboot.mail.configuration.EnableEmailTools;
  56.  
  57. import org.springframework.beans.factory.annotation.Autowired;
  58. import org.springframework.boot.SpringApplication;
  59. import org.springframework.boot.autoconfigure.SpringBootApplication;
  60. import org.springframework.boot.autoconfigure.batch.JobExecutionExitCodeGenerator;
  61. import org.springframework.context.ApplicationContext;
  62. import org.springframework.context.annotation.ComponentScan;
  63.  
  64. import javax.annotation.PostConstruct;
  65. import java.io.UnsupportedEncodingException;
  66. import java.util.Timer;
  67. import java.util.TimerTask;
  68. import java.util.concurrent.TimeUnit;
  69.  
  70. @SpringBootApplication
  71. @EnableEmailTools
  72. public class PlainTextApplication {
  73.  
  74. @Autowired
  75. private TestService testService;
  76.  
  77. public static void main(String[] args) {
  78. SpringApplication.run(PlainTextApplication.class, args);
  79. }
  80.  
  81. @PostConstruct
  82. public void sendEmail() throws UnsupportedEncodingException, InterruptedException {
  83. testService.sendEmail();
  84. }
  85.  
  86. }
  87.  
  88. @EnableEmailTools
Add Comment
Please, Sign In to add comment