Advertisement
Guest User

Mail.class

a guest
Mar 20th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. import java.util.Properties;
  2.  
  3.  
  4. import javax.annotation.Resource;
  5. import javax.ejb.Stateless;
  6. import javax.mail.Message;
  7. import javax.mail.MessagingException;
  8. import javax.mail.PasswordAuthentication;
  9. import javax.mail.Session;
  10. import javax.mail.Transport;
  11. import javax.mail.internet.InternetAddress;
  12. import javax.mail.internet.MimeMessage;
  13.  
  14.  
  15. @Stateless
  16. public class Mail implements IObserver {
  17.  
  18. @Resource(lookup = "java:jboss/mail/gmail")
  19. private Session session;
  20.  
  21. public void send(String adresses, String topic, String textMessage) {
  22.  
  23. final String username = "mestniutrip.maribor@gmail.com";
  24. final String password = "mestniutrip1";
  25.  
  26. Properties props = new Properties();
  27. props.put("mail.smtp.auth", "true");
  28. props.put("mail.smtp.starttls.enable", "true");
  29. props.put("mail.smtp.host", "smtp.gmail.com");
  30. props.put("mail.smtp.port", "587");
  31.  
  32. Session session = Session.getInstance(props,
  33. new javax.mail.Authenticator() {
  34. protected PasswordAuthentication getPasswordAuthentication() {
  35. return new PasswordAuthentication(username, password);
  36. }
  37. });
  38.  
  39. try {
  40. Message message = new MimeMessage(session);
  41. message.setFrom(new InternetAddress(username));
  42. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(adresses));
  43. message.setSubject(topic);
  44. message.setText(textMessage);
  45. Transport.send(message);
  46. } catch(MessagingException e) {
  47. throw new RuntimeException(e);
  48. }
  49. }
  50.  
  51. @Override
  52. public void update(Aktivnost a) {
  53. String naslov = a.getLastnik().getEmail();
  54. String tema = "Sprememba aktivnosti od osebe " + a.getLastnik().getIme() + " " + a.getLastnik().getPriimek();
  55. String sporocilo = "Pozdravljen " + a.getLastnik().getIme() + "!\nVaša aktivnost je bila spremenjena.";
  56. send(naslov, tema, sporocilo);
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement