Java Reference
In-Depth Information
ties that the JavaMail service might need. The hostname is set using the
mail.smtp.host
property, and if the host requires authentication then you must set
the
mail.smtp.auth
property to
true
. After the
properties
object is con-
figured, fetch a
javax.mail.Session
that will hold the connection information
for the e-mail message.
When you're creating a session, you can specify the login information if the service
requires authentication. This might be necessary when connecting to an SMTP service
that is outside of your local area network. To specify the login information, you must
create an
Authenticator
object, which will contain the
getPass-
wordAuthentication()
method. In this example, there is a new class identified
as
MessageAuthenticator
, which extends the
Authenticator
class. By mak-
ing the
getPasswordAuthentication()
method return a
Pass-
wordAuthentication
object, you can specify the username/password used for the
SMTP service.
The
Message
object represents an actual e-mail message and exposes e-mail
properties such as
From
/
To
/
Subject
and
Content
. After setting these properties,
you call the
Transport.send()
static method to send the e-mail message.
Tip
If you don't need authentication information, you can call the
Ses-
sion.getDefaultInstance(properties, null)
method, passing a
null
for the
Authenticator
parameter.
19-3. Attaching Files to an E-Mail Mes-
sage
Problem
You need to attach one or more files to an e-mail message.
Solution
Creating a message that contains different parts (called a
multipart message
) is what al-
lows you to send attachments such as files and images. You can specify the body of the