Java Reference
In-Depth Information
8. Get a
Transport
from the session.
9. Connect the transport to a named host using a username and password.
10. Send the message to all recipients over the transport.
Individually, each of the steps is quite simple.
The first step is to start a
Session
with a specified group of properties using the
Ses
sion.getInstance()
factory method, like this:
Properties
props
=
new
Properties
();
Session
session
=
Session
.
getInstance
(
props
);
For the moment, we'll just accept the default properties. For future reference,
Table 2-1
lists the most common system properties that control sending messages. Note the gen‐
eral pattern that
mail.foo
sets the foo property for all protocols (SMTP, NNTP, IMAP,
etc.) while
mail.protocol_name.foo
overrides the foo property for the named protocol.
For instance, if
mail.host
is set to
mail.example.com
and
mail.smtp.host
is set to
smtp.gmail.com
then IMAP and POP will retrieve messages from
mail.example.com
but
SMTP will send via
smtp.gmail.com
. In most cases, the default values provided by system
properties can be overridden when invoking a particular method.
Table 2-1. System properties used for sending mail
Property
Default Value
Purpose
mail.host
Host name of the mail server
mail.smtp.host
Host name of the SMTP server; overrides mail.host for SMTP connections
only
mail.
protocol
.host
Host name of the server for the specified protocol. Overrides mail.host
mail.user
Default username sent to mail servers
mail.
protocol
.user
Default username sent to servers for the specified protocol; e.g.,
mail.smtp.user for an SMTP server or mail.smtps.user for an SMTPS
server; overrides mail.user
mail.smtp.port
25
Port on which the SMTP server is listening
mail.
protocol
.port
default port for
the protocol
Port on which the server for the specified protocol is listening
mail.smtp.starttls.enable
Upgrade the regular SMTP connection on the usual port to an encrypted
(TLS or SSL) connection
mail.smtp.connectiontimeout
infinite
Number of milliseconds to wait for a connection before hanging up
mail.debug
false
Print various debugging information to the console
mail.from
The email address to use in the From: header
mail.mime.charset
file.encoding
Default character set used to send messages. You should always set
this to UTF-8
mail.transport.protocol.
address-
type
The message transport protocol such as SMTP for the specified address
type; e.g., mail.transport.protocol.rfc822