/** * A simple implementation of mail messages. Intended to illustrate * a basic, but correct, solution for assignment 1 of CS152 99F. * * Right now, includes only one primary recipient and no carbon-copied * recipients. Also ignores many common header fields. All email * addresses should be of the form name@host. Names should not include * quotation marks. Of all the components you can set and get, only * dates can be null. * * May eventually be expanded to include that additional information. * * @author Samuel A. Rebelsky * @version 1.1 of September 1999 */ public class SimpleMailMessage { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The name of the person who sent the message. */ protected String senderName; /** The email address of the person who sent the message. */ protected String senderEmail; /** The name of the intended recipient. */ protected String recipientName; /** The email address of that recipient. */ protected String recipientEmail; /** The date the message was sent. */ protected SimpleDate dateSent; /** The subject of the message. */ protected String subject; /** The body of the message. */ protected String body; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Create a new message, specifying all of the components. * Of these components, only the date can be null. For the * others, use the empty string ("") for "unspecified". * If the body is not empty, please use a carraige return at the * end of the body. */ public SimpleMailMessage( String senderName, String senderEmail, String recipientName, String recipientEmail, SimpleDate dateSent, String subject, String body) { this.senderName = senderName; this.senderEmail = senderEmail; this.recipientName = recipientName; this.recipientEmail = recipientEmail; this.dateSent = dateSent; this.subject = subject; this.body = body; } // SimpleMailMessage(...) /** * Create a simple mail message, with no components. */ public SimpleMailMessage() { this.senderName = ""; this.senderEmail = ""; this.recipientName = ""; this.recipientEmail = ""; this.dateSent = null; this.subject = ""; this.body = ""; } // SimpleMailMessage() // +-----------+----------------------------------------------- // | Accessors | // +-----------+ /** * Get the body of the message. */ public String getBody() { return this.body; } // getBody() /** * Get the date the message was sent. */ public SimpleDate getDate() { return this.dateSent; } // getDate() /** * Get the email address of the recipient of the message. */ public String getRecipientEmail() { return this.recipientEmail; } // getRecipientEmail() /** * Get the name of the recipient of the message. */ public String getRecipientName() { return this.recipientName; } // getRecipientName() /** * Get the email address of the sender of the message. */ public String getSenderEmail() { return this.senderEmail; } // getSenderEmail() /** * Get the name of the sender of the message. */ public String getSenderName() { return this.senderName; } // getSenderName() /** * Get the subject of the message. */ public String getSubject() { return this.subject; } // getSubject() /** * Get a simple, printable, version of the message. */ public String toString() { String message = ""; // Add a from address, if it exists. if (!this.senderEmail.equals("") || !this.senderName.equals("")) { message = message + "From: "; if (!this.senderName.equals("")) { message = message + "\"" + this.senderName + "\" "; } if (!this.senderEmail.equals("")) { message = message + "<" + this.senderEmail + ">"; } // Don't forget the carriage return message = message + "\n"; } // if there is a from address // Add a to address, if it exists. if (!this.recipientEmail.equals("") || !this.recipientName.equals("")) { message = message + "To: "; if (!this.recipientName.equals("")) { message = message + "\"" + this.recipientName + "\" "; } if (!this.recipientEmail.equals("")) { message = message + "<" + this.recipientEmail + ">"; } // Don't forget the carriage return message = message + "\n"; } // if there is a to address // Add a subject, if it exists. if (!this.subject.equals("")) { message = message + "Subject: " + this.subject + "\n"; } // Add a date, if it exsits. if (this.dateSent != null) { message = message + "Date: " + this.dateSent.toString() + "\n"; } // Add a blank line if the header is nonempty. if (!message.equals("")) { message = message + "\n"; } // Add the body. The body should end with a carriage return. message = message + body; // That's it. Return the result. return message; } // toString() // +-----------+----------------------------------------------- // | Modifiers | // +-----------+ /** * Set the body. Empty messages should have the empty string * ("") as their bodies. Returns the old body. */ public String setBody(String newBody) { String oldBody = this.body; this.body = newBody; return oldBody; } // setBody(String) /** * Set the date. Use null for unknown date. Returns the old date. */ public SimpleDate setDate(SimpleDate newDate) { SimpleDate oldDate = this.dateSent; this.dateSent = newDate; return oldDate; } // setDate(SimpleDate) /** * Set the recipient. Requires both a name and an email address. * Neither should be null. */ public void setRecipient(String newName, String newEmail) { this.recipientName = newName; this.recipientEmail = newEmail; } // setRecipeint(String,String) /** * Set the sender. Requires both a name and an email address. * Neither should be null. */ public void setSender(String newName, String newEmail) { this.senderName = newName; this.senderEmail = newEmail; } // setSender(String,String) /** * Set the subject. An empty message should have the empty string * ("") as its subject. Returns the old subject. */ public String setSubject(String newSubject) { String oldSubject = this.subject; this.subject= newSubject; return oldSubject; } // setSubject(String) // +---------------+------------------------------------------- // | Miscellaneous | // +---------------+ /** * Create the initial reply to this message. The reply will * have inverted sender and recipient and a slightly updated * subject. The body will be empty. Note that the choice to * make the recipient the sender is not necessarily appropriate, * since the person making the reply may be someone on the * Cc or Bcc list. However, it should suffice for now. */ public SimpleMailMessage makeReply() { SimpleMailMessage reply = new SimpleMailMessage(); reply.setSender(this.recipientName, this.recipientEmail); reply.setRecipient(this.senderName, this.senderEmail); reply.setSubject("Re: " + this.subject); return reply; } // makeReply() } // class SimpleMailMessage