Java Reference
In-Depth Information
Property
Default Value
Purpose
mail.
protocol
.class
Fully package qualified class name of the provider for the specified
protocol
mail.transport.protocol
first transport
provider in the
configuration file
Default protocol with which to send messages
mail.alternates
Other email addresses for the current user that will not be included
when replying to a message
mail.replyallcc
false
When replying to all, put all recipients in the Cc list of the reply message
instead of the To field
This
Session
object is then used to construct a new
Message
object:
Message
msg
=
new
MimeMessage
(
session
);
Here I specify the
MimeMessage
class in particular since I know I'm sending Internet
email. However, this is the one place where I do explicitly choose a format for the email
message.
Now that I have a
Message
object, I need to set up its fields and contents. The From:
address and To: address will each be
javax.mail.internet.InternetAddress
objects.
You can provide either an email address alone or an email address and a personal name:
Address
bill
=
new
InternetAddress
(
"god@microsoft.com"
,
"Bill Gates"
);
Address
elliotte
=
new
InternetAddress
(
"elharo@ibiblio.org"
);
The
setFrom()
method specifies who's sending the message by setting the From: header.
There's no protection against forgery. It's quite easy for me to masquerade as Bill Gates
at a (presumably) fictitious email address:
msg
.
setFrom
(
bill
);
The
setRecipient()
method is slightly more complex. You not only have to specify the
address that the message will be sent to, but how that address is used; that is, as a To:
field, a Cc: field, or a Bcc: field. These are indicated by three mnemonic constants of the
Message.RecipientType
class:
Message
.
RecipientType
.
TO
Message
.
RecipientType
.
CC
Message
.
RecipientType
.
BCC
For example:
msg
.
setRecipient
(
Message
.
RecipientType
.
TO
,
elliotte
);
The subject is set as a simple string of text. For example:
msg
.
setSubject
(
"You must comply."
);
The body is also set as a single string of text. For example: