Newer
Older
marking / src / report / SendReport.java
package report;

import dao.MarkingProperties;
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 message
				  = "Hi " + student.getName() + ".\n"
				  + "\n"
				  + "Here is our feedback from marking your INFO202 " + assessment + " project.  The feedback is in the attached PDF document.\n"
				  + "\n"
				  + "Cheers,\n"
				  + "Mark";

		Email email = new EmailBuilder()
				  .from("Mark George", "mark.george@otago.ac.nz")
				  .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) {
				String result = getPassword("Staff E-Mail Password");
				if (result != null) {
					password = result;
					new Mailer(MarkingProperties.smtpHost(), MarkingProperties.smtpPort(), userName, password, TransportStrategy.SMTP_TLS).sendMail(email);
				}
			} else {
				new Mailer(MarkingProperties.smtpHost(), MarkingProperties.smtpPort(), userName, password, TransportStrategy.SMTP_TLS).sendMail(email);
			}
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "Error sending email - password wrong?", "Mail Error", JOptionPane.ERROR_MESSAGE);
			password = null;
		}

	}

	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;
	}

}