• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaMail - How to Design for HTML, Plain Text and Multiple Recipients?

 
Michael K. Wilson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

JavaMail programming is so fun! I first start off mixing the JavaMail code inside a servlet and then refactored it to two separate classes.

Mailer is the independent class which uses JNDI to look up config parameters, sends e-mails, and then closes the connection with the protocol.



MailController is just a simple Java servlet where a Mailer object is instantiated and a String is passed inside the Mailer.sendMsg() method.



Have some OO design issues...

If you can notice, I manually place the body of the e-mail (as a String) from inside the client (in this case, a Java Servlet). Also, I manually have to switch the content type inside the doPost().

Question(s):

(1) How can the Mailer class be set up, so it can send both HTML and plain text messages?

(2) How can the Mailer class be structured to send e-mail to multiple recipients or one user?

(3) Would both of these features / actions require writing two overloaded methods for sendMsg()?

Would appreciate it if someone could help me...

Happy programming,

Michael
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael K. Wilson wrote:(1) How can the Mailer class be set up, so it can send both HTML and plain text messages?

Multipart emails, I think multipart/mixed is needed but you'd have to read up on that a bit more.

(2) How can the Mailer class be structured to send e-mail to multiple recipients or one user?

You're already using "message.setRecipients(javax.mail.Message.RecipientType.TO, ..."; with that you can set mulitple To addresses. By changing the recipient type you can set CC and BCC addresses as well. And if I recall correctly, there is also an addRecipient(s) method.

(3) Would both of these features / actions require writing two overloaded methods for sendMsg()?

Probably, yes. With so many possible properties for emails, I'd be surprised if you would need just 3, or even just 5, methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic