Javamail-api-smtp-servers

提供:Dev Guides
移動先:案内検索

JavaMail API-SMTPサーバー

SMTPは Simple Mail Transfer Protocol の頭字語です。 これは、インターネットプロトコル(IP)ネットワークを介した電子メール(電子メール)伝送のインターネット標準です。 SMTPはTCPポート25を使用します。 SMTPSはそれ自体ではプロトコルではありませんが、SSLで保護されたSMTP接続は簡易SMTPSで知られています。

JavaMail APIには、SMTPサーバーにアクセスするためのSMTPプロトコルプロバイダーとして機能するパッケージ com.sun.mail.smtp があります。 次の表に、このパッケージに含まれるクラスを示します。

Class Description
SMTPMessage This class is a specialization of the MimeMessage class that allows you to specify various SMTP options and parameters that will be used when this message is sent over SMTP.
SMTPSSLTransport This class implements the Transport abstract class using SMTP over SSL for message submission and transport.
SMTPTransport This class implements the Transport abstract class using SMTP for message submission and transport.

次の表に、スローされる例外を示します。

Exception Description
SMTPAddressFailedException This exception is thrown when the message cannot be sent.
SMTPAddressSucceededException This exception is chained off a SendFailedException when the mail.smtp.reportsuccess property is true.
SMTPSenderFailedException This exception is thrown when the message cannot be sent.
SMTPSendFailedException This exception is thrown when the message cannot be sent.The exception includes the sender’s address, which the mail server rejected.
*_com.sun.mail.smtp_* プロバイダーは、オプションでSMTP認証を使用します。 SMTP認証を使用するには、SMTPサーバーに接続するときに_mail.smtp.auth_プロパティを設定するか、SMTPトランスポートにユーザー名とパスワードを提供する必要があります。 これは、次のアプローチのいずれかを使用して実行できます。
  • メールセッションの作成時に認証システムオブジェクトを提供し、認証システムコールバック中にユーザー名とパスワード情報を提供します。 _mail.smtp.user_プロパティを設定して、コールバックのデフォルトのユーザー名を指定できますが、パスワードを明示的に指定する必要があります。 このアプローチにより、静的なトランスポート送信メソッドを使用してメッセージを送信できます。 例えば:
Transport.send(message);

ユーザー名とパスワードの引数を使用して、トランスポート接続メソッドを明示的に呼び出します。 例えば:

Transport tr = session.getTransport("smtp");
tr.connect(smtphost, username, password);
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();

SMTPプロトコルプロバイダーは、JavaMail Sessionオブジェクトで設定できる以下のプロパティをサポートします。 プロパティは常に文字列として設定されます。 例えば:

 props.put("mail.smtp.port", "587");

ここで、 Type 列は、文字列の解釈方法を説明しています。

Name Type Description
mail.smtp.user String Default user name for SMTP.
mail.smtp.host String The SMTP server to connect to.
mail.smtp.port int The SMTP server port to connect to, if the connect() method doesn’t explicitly specify one. Defaults to 25.
mail.smtp.connectiontimeout int Socket connection timeout value in milliseconds. Default is infinite timeout.
mail.smtp.timeout int Socket I/O timeout value in milliseconds. Default is infinite timeout.
mail.smtp.from String Email address to use for SMTP MAIL command. This sets the envelope return address. Defaults to msg.getFrom() or InternetAddress.getLocalAddress().
mail.smtp.localhost String Local host name used in the SMTP HELO or EHLO command. Defaults to InetAddress.getLocalHost().getHostName(). Should not normally need to be set if your JDK and your name service are configured properly.
mail.smtp.localaddress String Local address (host name) to bind to when creating the SMTP socket. Defaults to the address picked by the Socket class. Should not normally need to be set.
mail.smtp.localport int Local port number to bind to when creating the SMTP socket. Defaults to the port number picked by the Socket class.
mail.smtp.ehlo boolean If false, do not attempt to sign on with the EHLO command. Defaults to true.
mail.smtp.auth boolean If true, attempt to authenticate the user using the AUTH command. Defaults to false.
mail.smtp.auth.mechanisms String If set, lists the authentication mechanisms to consider. Only mechanisms supported by the server and supported by the current implementation will be used. The default is "LOGIN PLAIN DIGEST-MD5 NTLM", which includes all the authentication mechanisms supported by the current implementation.
mail.smtp.auth.login.disable boolean If true, prevents use of the AUTH LOGIN command. Default is false.
mail.smtp.auth.plain.disable boolean If true, prevents use of the AUTH PLAIN command. Default is false.
mail.smtp.auth.digest-md5.disable boolean If true, prevents use of the AUTH DIGEST-MD5 command. Default is false.
mail.smtp.auth.ntlm.disable boolean If true, prevents use of the AUTH NTLM command. Default is false.
mail.smtp.auth.ntlm.domain String The NTLM authentication domain.
mail.smtp.auth.ntlm.flags int NTLM protocol-specific flags.
mail.smtp.submitter String The submitter to use in the AUTH tag in the MAIL FROM command. Typically used by a mail relay to pass along information about the original submitter of the message.
mail.smtp.dsn.notify String The NOTIFY option to the RCPT command. Either NEVER, or some combination of SUCCESS, FAILURE, and DELAY (separated by commas).
mail.smtp.dsn.ret String The RET option to the MAIL command. Either FULL or HDRS.
mail.smtp.sendpartial boolean If set to true, and a message has some valid and some invalid addresses, send the message anyway, reporting the partial failure with a SendFailedException. If set to false (the default), the message is not sent to any of the recipients if there is an invalid recipient address.
mail.smtp.sasl.enable boolean If set to true, attempt to use the javax.security.sasl package to choose an authentication mechanism for login. Defaults to false.
mail.smtp.sasl.mechanisms String A space or comma separated list of SASL mechanism names to try to use.
mail.smtp.sasl.authorizationid String The authorization ID to use in the SASL authentication. If not set, the authentication ID (user name) is used.
mail.smtp.sasl.realm String The realm to use with DIGEST-MD5 authentication.
mail.smtp.quitwait boolean If set to false, the QUIT command is sent and the connection is immediately closed. If set to true (the default), causes the transport to wait for the response to the QUIT command.
mail.smtp.reportsuccess boolean If set to true, causes the transport to include an SMTPAddressSucceededException for each address that is successful.
mail.smtp.socketFactory Socket Factory If set to a class that implements the javax.net.SocketFactory interface, this class will be used to create SMTP sockets.
mail.smtp.socketFactory.class String If set, specifies the name of a class that implements the javax.net.SocketFactory interface. This class will be used to create SMTP sockets.
mail.smtp.socketFactory.fallback boolean If set to true, failure to create a socket using the specified socket factory class will cause the socket to be created using the java.net.Socket class. Defaults to true.
mail.smtp.socketFactory.port int Specifies the port to connect to when using the specified socket factory. If not set, the default port will be used.
mail.smtp.ssl.enable boolean If set to true, use SSL to connect and use the SSL port by default. Defaults to false for the "smtp" protocol and true for the "smtps" protocol.
mail.smtp.ssl.checkserveridentity boolean If set to true, checks the server identity as specified by RFC 2595. Defaults to false.
mail.smtp.ssl.trust String If set, and a socket factory hasn’t been specified, enables use of a MailSSLSocketFactory. If set to "*", all hosts are trusted. If set to a whitespace separated list of hosts, those hosts are trusted. Otherwise, trust depends on the certificate the server presents.
mail.smtp.ssl.socketFactory SSL Socket Factory If set to a class that extends the javax.net.ssl.SSLSocketFactory class, this class will be used to create SMTP SSL sockets.
mail.smtp.ssl.socketFactory.class String If set, specifies the name of a class that extends the javax.net.ssl.SSLSocketFactory class. This class will be used to create SMTP SSL sockets.
mail.smtp.ssl.socketFactory.port int Specifies the port to connect to when using the specified socket factory. If not set, the default port will be used.
mail.smtp.ssl.protocols string Specifies the SSL protocols that will be enabled for SSL connections. The property value is a whitespace separated list of tokens acceptable to the javax.net.ssl.SSLSocket.setEnabledProtocols method.
mail.smtp.starttls.enable boolean If true, enables the use of the STARTTLS command (if supported by the server) to switch the connection to a TLS-protected connection before issuing any login commands. Defaults to false.
mail.smtp.starttls.required boolean If true, requires the use of the STARTTLS command. If the server doesn’t support the STARTTLS command, or the command fails, the connect method will fail. Defaults to false.
mail.smtp.socks.host string Specifies the host name of a SOCKS5 proxy server that will be used for connections to the mail server.
mail.smtp.socks.port string Specifies the port number for the SOCKS5 proxy server. This should only need to be used if the proxy server is not using the standard port number of 1080.
mail.smtp.mailextension String Extension string to append to the MAIL command.
mail.smtp.userset boolean If set to true, use the RSET command instead of the NOOP command in the isConnected method. In some cases sendmail will respond slowly after many NOOP commands; use of RSET avoids this sendmail issue. Defaults to false.

一般に、アプリケーションはこのパッケージのクラスを直接使用する必要はありません。 代わりに、javax.mailパッケージ(およびサブパッケージ)で定義されたAPIを使用する必要があります。 たとえば、アプリケーションでSMTPTransportのインスタンスを直接構築しないでください。 代わりに、SessionメソッドgetTransportを使用して適切なTransportオブジェクトを取得する必要があります。

SMPTサーバーの使用例は、章のリンク:/javamail_api/javamail_api_sending_emails [Sending Emails]に示されています。