Advertisement
Guest User

Untitled

a guest
Mar 18th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. package com.ddsoft.loggers;
  2.  
  3.  
  4. import com.ddsoft.interfaces.Logger;
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.InternetAddress;
  10. import javax.mail.internet.MimeMessage;
  11. import java.util.Properties;
  12. import javax.mail.PasswordAuthentication;
  13.  
  14. public class MailLogger implements Logger {
  15.  
  16.     @Override
  17.     public void log(String status, String student) {
  18.  
  19.         Properties props = new Properties();
  20.     props.put("mail.smtp.host", "smtp.gmail.com");
  21.     props.put("mail.smtp.socketFactory.port", "465");
  22.     props.put("mail.smtp.socketFactory.class",
  23.             "javax.net.ssl.SSLSocketFactory");
  24.     props.put("mail.smtp.auth", "true");
  25.     props.put("mail.smtp.port", "465");
  26.     Session session = Session.getDefaultInstance(props,
  27.             new javax.mail.Authenticator() {
  28.                 @Override
  29.                 protected PasswordAuthentication getPasswordAuthentication() {
  30.                     return new PasswordAuthentication("crawlertesten@gmail.com","asdfghjkl123");
  31.                 }
  32.             });
  33.  
  34.     try {
  35.  
  36.         Message message = new MimeMessage(session);
  37.         message.setFrom(new InternetAddress("crawlertesten@gmail.com"));
  38.         message.setRecipients(Message.RecipientType.TO,
  39.                 InternetAddress.parse("crawlertesten@gmail.com"));
  40.         if(!status.equals("NOTHING CHANGED")) {
  41.             message.setSubject("Crawler notification (" + status + " person)");
  42.             if (status.equals("ADDED"))
  43.                 message.setText("The FILE or PAGE has been changed!\nTHERE IS A NEW PERSON ! \nNew person is:\n" + student);
  44.             else if (status.equals("REMOVED"))
  45.                 message.setText("The FILE or PAGe has been changed!\nMISSING ONE PERSON :(\nOld person was:\n" + student);
  46.         }
  47.         else {
  48.             message.setSubject("Crawler notification (" + status + ")");
  49.             message.setText("The FILE or PAGE hasn't been changed :)");
  50.         }
  51.  
  52.         Transport.send(message);
  53.  
  54.     } catch (MessagingException e) {
  55.         throw new RuntimeException(e);
  56.     }
  57. }
  58.  
  59.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement