001package edu.pdx.cs410J.grader; 002 003import com.google.common.annotations.VisibleForTesting; 004import jakarta.mail.Message; 005import jakarta.mail.MessagingException; 006import jakarta.mail.Session; 007import jakarta.mail.internet.InternetAddress; 008import jakarta.mail.internet.MimeMessage; 009 010import java.io.UnsupportedEncodingException; 011import java.util.Properties; 012 013public class EmailSender { 014 /** 015 * The grader's email address 016 */ 017 @VisibleForTesting 018 static final InternetAddress TA_EMAIL = newInternetAddress("sjavata@gmail.com", "Java Course Grader"); 019 020 static final InternetAddress DAVE_EMAIL = newInternetAddress("whitlock@cs.pdx.edu", "David Whitlock"); 021 022 protected static InternetAddress newInternetAddress(String address, String personal) { 023 try { 024 return new InternetAddress(address, personal); 025 026 } catch (UnsupportedEncodingException ex) { 027 String message = "Could not create InternetAddress for " + address + " " + personal; 028 throw new IllegalStateException(message, ex); 029 } 030 } 031 032 /** 033 * The name of the SMTP server that is used to send email 034 */ 035 protected static String serverName = "mailhost.cs.pdx.edu"; 036 037 private static int emailServerPort = 25; 038 039 protected static NewEmail newEmailTo(Session session, InternetAddress recipient) { 040 return new NewEmail(session).to(recipient); 041 } 042 043 protected static class NewEmail { 044 private final Session session; 045 private InternetAddress recipient; 046 private InternetAddress sender; 047 private InternetAddress replyTo; 048 private String subject; 049 050 private NewEmail(Session session) { 051 this.session = session; 052 } 053 054 MimeMessage createMessage() throws MessagingException { 055 056 // Make a new email message 057 MimeMessage message = new MimeMessage(session); 058 059 message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { recipient }); 060 message.setFrom(sender); 061 message.setSubject(subject); 062 if (replyTo != null) { 063 message.setReplyTo(new InternetAddress[] { replyTo }); 064 } 065 return message; 066 } 067 068 private NewEmail to(InternetAddress recipient) { 069 this.recipient = recipient; 070 return this; 071 } 072 073 public NewEmail from(InternetAddress sender) { 074 this.sender = sender; 075 return this; 076 } 077 078 public NewEmail withSubject(String subject) { 079 this.subject = subject; 080 return this; 081 } 082 083 public NewEmail replyTo(InternetAddress replyTo) { 084 this.replyTo = replyTo; 085 return this; 086 } 087 088 public NewEmail from(String address, String personal) { 089 return from(newInternetAddress(address, personal)); 090 } 091 } 092 093 protected static Session newEmailSession(boolean debug) { 094 // Obtain a Session for sending email 095 Properties props = new Properties(); 096 props.put("mail.smtp.host", serverName); 097 props.put("mail.smtp.port", emailServerPort); 098 props.put("mail.smtp.localhost", "127.0.0.1"); 099 Session session = Session.getDefaultInstance(props, null); 100 session.setDebug(debug); 101 return session; 102 } 103 104 public void setEmailServerPort(int port) { 105 emailServerPort = port; 106 } 107}