• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22803
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Hang a left on main. Then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic