Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. package turistinfo;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.util.HashMap;
  8. import java.util.Properties;
  9.  
  10. import javax.mail.Message;
  11. import javax.mail.MessagingException;
  12. import javax.mail.PasswordAuthentication;
  13. import javax.mail.Session;
  14. import javax.mail.Transport;
  15. import javax.mail.internet.InternetAddress;
  16. import javax.mail.internet.MimeMessage;
  17.  
  18. public class MailClient {
  19.  
  20. private static HashMap<String, Boolean> notificationMap = new HashMap<>();
  21.  
  22. static {
  23. notificationMap.put("Info Grad", false);
  24. notificationMap.put("Info Hotel", false);
  25. notificationMap.put("Info Bus", false);
  26. notificationMap.put("Info Event", false);
  27. notificationMap.put("Info Vrijeme", false);
  28. notificationMap.put("Info Admin", false);
  29. }
  30.  
  31. private static String username;
  32. private static String password;
  33.  
  34. public static Properties loadMailConfig() throws FileNotFoundException, IOException {
  35. Properties serverprop = new Properties();
  36. serverprop.load(new FileInputStream(new File("src/mail/server.properties")));
  37. String mailProvider = serverprop.getProperty("mail_provider");
  38.  
  39. System.out.println("Koristi se " + mailProvider);
  40.  
  41. Properties mailProp = new Properties();
  42. mailProp.load(new FileInputStream(new File("src/mail" + File.separator + mailProvider + ".properties")));
  43.  
  44. username = mailProp.getProperty("username");
  45. password = mailProp.getProperty("password");
  46. return mailProp;
  47. }
  48.  
  49. public static boolean sendMail(String to, String title, String body, String service) throws FileNotFoundException, IOException {
  50.  
  51. //check if the mail is already sent for specified service
  52. if ( notificationMap.get(service) ){
  53. return false;
  54. }
  55.  
  56. Properties props = loadMailConfig();
  57.  
  58. Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
  59. protected PasswordAuthentication getPasswordAuthentication() {
  60. return new PasswordAuthentication(username, password);
  61. }
  62. });
  63.  
  64. try {
  65. Message message = new MimeMessage(session);
  66. message.setFrom(new InternetAddress(username));
  67. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  68. message.setSubject(title);
  69. message.setText(body);
  70. Transport.send(message);
  71. return true;
  72. } catch (MessagingException e) {
  73. e.printStackTrace();
  74. return false;
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement