Java Reference
In-Depth Information
How It Works
By using the
setRecipients()
method of the
Message
object, you can specify
multiple recipients on the same message. The
setRecipients()
method accepts an
array of
Address
objects. In this recipe, because you have a collection of strings, you
create the array as the size of the collection and create
InternetAddress
objects to
fill the array. Sending e-mails using multiple e-mail addresses (as opposed to individual
e-mails) is much more efficient because only one message is sent from your client to
the target mail servers. Each target mail server will then deliver to all recipients that it
sage.
Tip
If you want to send bulk messages, you might want to specify the Recipient
Type as BCC, so that the e-mail received doesn't show everyone else that is getting the
e-mail. To do so, specify
Message.RecipientType.BCC
in the
setRecipi-
ents()
method.
19-6. Checking E-Mail
Problem
You need to check if a new e-mail has arrived for a specified e-mail account.
Solution
You can use
javax.mail.Store
to connect, query, and retrieve messages from an
Internet Message Access Protocol (IMAP) e-mail account. For example, the following
code connects to an IMAP account, retrieves the last five messages from that IMAP ac-
count, and marks the messages as
read
.
Session session = Session.getDefaultInstance(properties,
null);