Advertisement
moreiramota

Untitled

Dec 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. package lapr.project.controller;
  2.  
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.util.*;
  9. import javax.mail.Message.RecipientType;
  10.  
  11. public class SendEmailController {
  12.  
  13. /**
  14. *
  15. * @param props
  16. * @param top
  17. * @param hours
  18. */
  19. public static void send(final Properties props, String top, int hours) {
  20. String from = props.getProperty("mail.user");
  21. String to = top;
  22. String subject = props.getProperty("testmail.subject");
  23. String body = props.getProperty("testmail.body");
  24.  
  25. Session mailSession = Session.getDefaultInstance(props,
  26. new javax.mail.Authenticator() {
  27. @Override
  28. protected PasswordAuthentication getPasswordAuthentication() {
  29. return new PasswordAuthentication(props.getProperty("mail.user"), props.getProperty("mail.password"));
  30. }
  31. });
  32. Message simpleMessage = new MimeMessage(mailSession);
  33. InternetAddress fromAddress = null;
  34. InternetAddress toAddress = null;
  35. try {
  36. fromAddress = new InternetAddress(from);
  37. toAddress = new InternetAddress(to);
  38. } catch (AddressException e) {
  39. System.err.println("Error sending mail 1");
  40. }
  41. try {
  42. simpleMessage.setFrom(fromAddress);
  43. System.out.println("ola 1");
  44. simpleMessage.setRecipient(RecipientType.TO, toAddress);
  45. System.out.println("ola 2");
  46. simpleMessage.setSubject(subject);
  47. System.out.println("ola 3");
  48. simpleMessage.setText(body);
  49. System.out.println("ola 4");
  50. } catch (MessagingException e) {
  51. System.err.println("Error");
  52. }
  53. try {
  54. Transport.send(simpleMessage);
  55. System.out.println("ola 5");
  56. } catch (MessagingException e) {
  57. System.err.println("Error sending mail");
  58. }
  59.  
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement