UML diagram of the MailSender class
CHAPTER 4 n INSTAMAIL: AN AJAX-BASED WEBMAIL CLIENT 176
MailSender has only a single purpose in life: to send a message via SMTP. As such, it has
only a single method, sendMessage() (clearly I was not in a creative mode when it came to
naming these three classes!). When a message is sent, MailSender also has to write it to disk
since we want to retain all sent messages locally.
First we need to populate a Properties object for use with JavaMail, the standard Java API
for working with e-mail. This object contains details like the SMTP server address, the username
and password to use, and so forth. All of this is mandated by the JavaMail API. Once that
object is ready, we open a session with the server and construct a MimeMessage object. This
contains the text of the message, the recipient address, and the subject, among other things.
Finally, the server is asked to send the message.
Once that is complete, we still have to save the local copy. To do this we populate a
MessageDTO object. The file name we??™ll be saving is constructed as a string beginning with msg_
and then containing the current date and time, down to the second. This should ensure, barring
someone with a very quick finger or the computer having an incorrect date and time set,
that each e-mail sent has a unique file name.
Pages:
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334