Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.06 KB | None | 0 0
  1.  /**
  2.     *
  3.     */
  4.  
  5.  
  6.     import java.awt.event.ActionEvent;
  7.     import java.awt.event.ActionListener;
  8.     import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.util.Properties;
  11.  
  12. import javax.mail.Message;
  13. import javax.mail.MessagingException;
  14. import javax.mail.PasswordAuthentication;
  15. import javax.mail.Session;
  16. import javax.mail.Transport;
  17. import javax.mail.internet.InternetAddress;
  18. import javax.mail.internet.MimeMessage;
  19. import javax.swing.DefaultListModel;
  20. import javax.swing.JFrame;
  21. import javax.swing.JLabel;
  22. import javax.swing.JPanel;
  23. import javax.swing.JTextField;
  24.  
  25. import org.apache.poi.hssf.usermodel.HSSFCell;
  26. import org.apache.poi.hssf.usermodel.HSSFRow;
  27. import org.apache.poi.hssf.usermodel.HSSFSheet;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. public class MainWork {
  30.  
  31.     /**
  32.      * @param args
  33.      *
  34.      */
  35.     static JTextField textField = new JTextField(40);
  36.     static JFrame frame = new JFrame("look up Email System");
  37.     JPanel panel;
  38.     private static JLabel fstnamelb;
  39.     private static JLabel roomnblb;
  40.     private static JLabel emlb;
  41.     private static HSSFWorkbook xlWBook;
  42.     private static HSSFSheet xlSheet;
  43.     private static HSSFRow xlRow;
  44.     private static HSSFCell xlCell;
  45.     @SuppressWarnings("rawtypes")
  46.     static DefaultListModel names = new DefaultListModel();
  47.     static String in;
  48.     static String fstname;
  49.     static String rn;
  50.  
  51.  
  52.     @SuppressWarnings({ "unused", "unchecked" })
  53.     public static void main(String[] args) {
  54.             // i am getting a java.lang.NullPointerException error for the next 6 lines
  55.         fstnamelb.setText("First Name: ");
  56.         roomnblb.setText("Room Number: ");
  57.         emlb.setText("Email: ");
  58.         frame.getContentPane().add(fstnamelb, "North");
  59.         frame.getContentPane().add(roomnblb, "East");
  60.         frame.getContentPane().add(emlb, "West");
  61.  
  62.  
  63.  
  64.  
  65.         frame.getContentPane().add(textField, "Center");
  66.  
  67.         frame.setLocationRelativeTo(null);
  68.         frame.pack();
  69.  
  70.         // Sets up emailing with Gmail
  71.  
  72.         final String username = "no-reply@gmail.com";
  73.         final String password = "i am not that dumb";
  74.  
  75.         Properties props = new Properties();
  76.         props.put("mail.smtp.auth", "true");
  77.         props.put("mail.smtp.starttls.enable", "true");
  78.         props.put("mail.smtp.host", "smtp.gmail.com");
  79.         props.put("mail.smtp.port", "587");
  80.  
  81.         Session session = Session.getInstance(props,
  82.           new javax.mail.Authenticator() {
  83.             protected PasswordAuthentication getPasswordAuthentication() {
  84.                 return new PasswordAuthentication(username, password);
  85.             }
  86.           });
  87.  
  88.  
  89.  
  90.  
  91.                 try {
  92.  
  93.                     FileInputStream XlFile = new FileInputStream("src/ips.xls");
  94.  
  95.                     xlWBook = new HSSFWorkbook(XlFile);
  96.                     xlSheet = xlWBook.getSheet("Sheet1");
  97.                     xlRow = xlSheet.getRow(1);
  98.                     System.out.println(xlRow.getCell(0).getStringCellValue());
  99.                     int noOfRows = xlSheet.getPhysicalNumberOfRows();
  100.                     // gives column count in sheet
  101.                     xlRow = xlSheet.getRow(0);
  102.                     int noOfColumns = xlRow.getLastCellNum();
  103.                     for (int i = 0; i == noOfColumns; i++){
  104.                         names.addElement(xlRow.getCell(i));
  105.                     }
  106.                     textField.addActionListener(new ActionListener() {
  107.                         /**
  108.                          * Responds to pressing the enter key in the textfield by sending
  109.                          * the contents of the text field to the server.    Then clear
  110.                          * the text area in preparation for the next message.
  111.                          */
  112.                         public void actionPerformed(ActionEvent e) {
  113.                             in = textField.getText();
  114.                           textField.setText("");
  115.  
  116.                           int inx = names.indexOf(in);
  117.                           xlRow = xlSheet.getRow(1);
  118.                            fstname = xlRow.getCell(inx).toString();
  119.                           fstnamelb.setText("First Name: " + fstname);
  120.                           xlRow = xlSheet.getRow(2);
  121.                           rn = xlRow.getCell(inx).toString();
  122.                           roomnblb.setText("Room number: " + rn);
  123.                           xlRow = xlSheet.getRow(2);
  124.                           String em = xlRow.getCell(inx).toString();
  125.                           emlb.setText("EMail: " + em);
  126.                         }
  127.                     });
  128.  
  129.                     try {
  130.  
  131.                         Message message = new MimeMessage(session);
  132.                         message.setFrom(new InternetAddress("no-reply@gmail.com"));
  133.                         message.setRecipients(Message.RecipientType.TO,
  134.                             InternetAddress.parse("no-reply@gmail.com"));
  135.                         message.setSubject("Testing Subject");
  136.                         message.setText("Dear " /*+ fstname + " " + in + "," */
  137.                             + "\n\n No spam to my email, please!");
  138.  
  139.                         //Transport.send(message);
  140.  
  141.                         System.out.println("Done");
  142.  
  143.                     } catch (MessagingException e) {
  144.                         throw new RuntimeException(e);
  145.                     }  
  146.  
  147.  
  148.                 } catch (IOException e) {
  149.                     // TODO Auto-generated catch block
  150.                     e.printStackTrace();
  151.                 }          
  152.             }
  153.     public static void act(String in){
  154.         int inx = names.indexOf(in);
  155.         xlRow = xlSheet.getRow(1);
  156.         String fstname = xlRow.getCell(inx).toString();
  157.         fstnamelb.setText("First Name: " + fstname);
  158.         xlRow = xlSheet.getRow(2);
  159.         String rn = xlRow.getCell(inx).toString();
  160.         roomnblb.setText("Room number: " + rn);
  161.         xlRow = xlSheet.getRow(2);
  162.         String em = xlRow.getCell(inx).toString();
  163.         emlb.setText("EMail: " + em);
  164.  
  165.     }
  166.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement