Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.swing.DefaultListModel;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- public class MainWork {
- /**
- * @param args
- *
- */
- static JTextField textField = new JTextField(40);
- static JFrame frame = new JFrame("look up Email System");
- JPanel panel;
- private static JLabel fstnamelb;
- private static JLabel roomnblb;
- private static JLabel emlb;
- private static HSSFWorkbook xlWBook;
- private static HSSFSheet xlSheet;
- private static HSSFRow xlRow;
- private static HSSFCell xlCell;
- @SuppressWarnings("rawtypes")
- static DefaultListModel names = new DefaultListModel();
- static String in;
- static String fstname;
- static String rn;
- @SuppressWarnings({ "unused", "unchecked" })
- public static void main(String[] args) {
- // i am getting a java.lang.NullPointerException error for the next 6 lines
- fstnamelb.setText("First Name: ");
- roomnblb.setText("Room Number: ");
- emlb.setText("Email: ");
- frame.getContentPane().add(fstnamelb, "North");
- frame.getContentPane().add(roomnblb, "East");
- frame.getContentPane().add(emlb, "West");
- frame.getContentPane().add(textField, "Center");
- frame.setLocationRelativeTo(null);
- frame.pack();
- // Sets up emailing with Gmail
- final String username = "no-reply@gmail.com";
- final String password = "i am not that dumb";
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.port", "587");
- Session session = Session.getInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- try {
- FileInputStream XlFile = new FileInputStream("src/ips.xls");
- xlWBook = new HSSFWorkbook(XlFile);
- xlSheet = xlWBook.getSheet("Sheet1");
- xlRow = xlSheet.getRow(1);
- System.out.println(xlRow.getCell(0).getStringCellValue());
- int noOfRows = xlSheet.getPhysicalNumberOfRows();
- // gives column count in sheet
- xlRow = xlSheet.getRow(0);
- int noOfColumns = xlRow.getLastCellNum();
- for (int i = 0; i == noOfColumns; i++){
- names.addElement(xlRow.getCell(i));
- }
- textField.addActionListener(new ActionListener() {
- /**
- * Responds to pressing the enter key in the textfield by sending
- * the contents of the text field to the server. Then clear
- * the text area in preparation for the next message.
- */
- public void actionPerformed(ActionEvent e) {
- in = textField.getText();
- textField.setText("");
- int inx = names.indexOf(in);
- xlRow = xlSheet.getRow(1);
- fstname = xlRow.getCell(inx).toString();
- fstnamelb.setText("First Name: " + fstname);
- xlRow = xlSheet.getRow(2);
- rn = xlRow.getCell(inx).toString();
- roomnblb.setText("Room number: " + rn);
- xlRow = xlSheet.getRow(2);
- String em = xlRow.getCell(inx).toString();
- emlb.setText("EMail: " + em);
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress("no-reply@gmail.com"));
- message.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse("no-reply@gmail.com"));
- message.setSubject("Testing Subject");
- message.setText("Dear " /*+ fstname + " " + in + "," */
- + "\n\n No spam to my email, please!");
- //Transport.send(message);
- System.out.println("Done");
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void act(String in){
- int inx = names.indexOf(in);
- xlRow = xlSheet.getRow(1);
- String fstname = xlRow.getCell(inx).toString();
- fstnamelb.setText("First Name: " + fstname);
- xlRow = xlSheet.getRow(2);
- String rn = xlRow.getCell(inx).toString();
- roomnblb.setText("Room number: " + rn);
- xlRow = xlSheet.getRow(2);
- String em = xlRow.getCell(inx).toString();
- emlb.setText("EMail: " + em);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement