package report; import dao.MarkingProperties; import dao.ResultDAO; import javax.activation.FileDataSource; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import model.Student; import org.simplejavamail.email.Email; import org.simplejavamail.email.EmailBuilder; import org.simplejavamail.mailer.Mailer; import org.simplejavamail.mailer.config.TransportStrategy; /** * * @author Mark George <mark.george@otago.ac.nz> */ public class SendReport { private static String password = null; public void sendReport(Student student, String reportFileName) { String assessment = MarkingProperties.assessmentDescription(); String file = MarkingProperties.attachmentEmailName(); String mName = MarkingProperties.markerName(); String mEmail = MarkingProperties.markerEmail(); String message = "Hi " + student.getName() + ".\n" + "\n" + "Here is our feedback from marking your INFO202 " + assessment + " submission. The feedback is in the attached PDF document.\n" + "\n" + "Cheers,\n" + mName; Email email = new EmailBuilder() .from(mName, mEmail) .to(student.getEmail()) .cc("mark.george@otago.ac.nz") .subject("INFO202 " + assessment + " Result") .text(message) .build(); email.addAttachment(file, new FileDataSource(reportFileName)); try { String userName = MarkingProperties.smtpUserName(); if (password == null && userName != null) { String result = getPassword("Staff E-Mail Password"); if (result != null) { password = result; } else { System.out.println("Cancelled"); return; } } new Mailer(MarkingProperties.smtpHost(), MarkingProperties.smtpPort(), userName, password, TransportStrategy.SMTP_TLS).sendMail(email); new ResultDAO().mailSent(student); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Error sending email - password wrong?", "Mail Error", JOptionPane.ERROR_MESSAGE); password = null; ex.printStackTrace(); } } public static String getPassword(String prompt) { JPasswordField txtPasswd = new JPasswordField(); int resp = JOptionPane.showConfirmDialog(null, txtPasswd, prompt, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (resp == JOptionPane.OK_OPTION) { return new String(txtPasswd.getPassword()); } return null; } }