全屏

SMTP是一个缩写为简单邮件传输协议。它是跨越互联网协议(IP)网络电子邮件(电子邮件)传输的Internet标准。 SMTP使用TCP端口25。受SSL保护的SMTP连接通过缩写SMTPS称,虽然SMTPS不是一个协议在自己的权利。

JavaMail API 有包 com.sun.mail.smtp 它作为SMTP协议提供访问SMTP服务器。下表列出了包含在这个包中的类:

描述
SMTPMessageThis 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.
SMTPSSLTransportThis class implements the Transport abstract class using SMTP over SSL for message submission and transport.
SMTPTransportThis class implements the Transport abstract class using SMTP for message submission and transport.

下表列出抛出的异常:

异常描述
SMTPAddressFailedExceptionThis exception is thrown when the message cannot be sent.
SMTPAddressSucceededExceptionThis exception is chained off a SendFailedException when the mail.smtp.reportsuccess property is true.
SMTPSenderFailedExceptionThis exception is thrown when the message cannot be sent.
SMTPSendFailedExceptionThis 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验证,您需要设置mail.smtp.auth属性或提供SMTP传输与连接到SMTP服务器的用户名和密码。您可以使用以下方法之一进行:

Transport.send(message);
  • 创建您的邮件会话时提供一个Authenticator对象,并提供身份验证器回调过程中的用户名和密码信息。mail.smtp.user属性可以设置,以提供一个默认的用户名回调,但密码仍然需要显式提供。这种方法允许您使用静态传输send方法来发送消息。例如:

  • 调用传输用户名和密码参数显式连接方法。例如:

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

SMTP协议提供程序支持以下属性,这些属性可以在JavaMail会话对象进行设置。该属性始终设置为字符串。例如:

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

在这里,类型列描述字符串是如何解释的。

名称类型描述
mail.smtp.userStringDefault user name for SMTP.
mail.smtp.hostStringThe SMTP server to connect to.
mail.smtp.portintThe SMTP server port to connect to, if the connect() method doesn't explicitly specify one. Defaults to 25.
mail.smtp.connectiontimeoutintSocket connection timeout value in milliseconds. Default is infinite timeout.
mail.smtp.timeoutintSocket I/O timeout value in milliseconds. Default is infinite timeout.
mail.smtp.fromStringEmail address to use for SMTP MAIL command. This sets the envelope return address. Defaults to msg.getFrom() or InternetAddress.getLocalAddress().
mail.smtp.localhostStringLocal 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.localaddressStringLocal 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.localportintLocal port number to bind to when creating the SMTP socket. Defaults to the port number picked by the Socket class.
mail.smtp.ehlobooleanIf false, do not attempt to sign on with the EHLO command. Defaults to true.
mail.smtp.authbooleanIf true, attempt to authenticate the user using the AUTH command. Defaults to false.
mail.smtp.auth.mechanismsStringIf 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.disablebooleanIf true, prevents use of the AUTH LOGIN command. Default is false.
mail.smtp.auth.plain.disablebooleanIf true, prevents use of the AUTH PLAIN command. Default is false.
mail.smtp.auth.digest-md5.disablebooleanIf true, prevents use of the AUTH DIGEST-MD5 command. Default is false.
mail.smtp.auth.ntlm.disablebooleanIf true, prevents use of the AUTH NTLM command. Default is false.
mail.smtp.auth.ntlm.domainStringThe NTLM authentication domain.
mail.smtp.auth.ntlm.flagsintNTLM protocol-specific flags.
mail.smtp.submitterStringThe 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.notifyStringThe NOTIFY option to the RCPT command. Either NEVER, or some combination of SUCCESS, FAILURE, and DELAY (separated by commas).
mail.smtp.dsn.retStringThe RET option to the MAIL command. Either FULL or HDRS.
mail.smtp.sendpartialbooleanIf 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.enablebooleanIf set to true, attempt to use the javax.security.sasl package to choose an authentication mechanism for login. Defaults to false.
mail.smtp.sasl.mechanismsStringA space or comma separated list of SASL mechanism names to try to use.
mail.smtp.sasl.authorizationidStringThe authorization ID to use in the SASL authentication. If not set, the authentication ID (user name) is used.
mail.smtp.sasl.realmStringThe realm to use with DIGEST-MD5 authentication.
mail.smtp.quitwaitbooleanIf 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.reportsuccessbooleanIf set to true, causes the transport to include an SMTPAddressSucceededException for each address that is successful.
mail.smtp.socketFactorySocketFactoryIf set to a class that implements the javax.net.SocketFactory interface, this class will be used to create SMTP sockets.
mail.smtp.socketFactory.classStringIf 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.fallbackbooleanIf 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.portintSpecifies the port to connect to when using the specified socket factory. If not set, the default port will be used.
mail.smtp.ssl.enablebooleanIf 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.checkserveridentitybooleanIf set to true, checks the server identity as specified by RFC 2595. Defaults to false.
mail.smtp.ssl.trustStringIf 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.socketFactorySSLSocketFactoryIf 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.classStringIf 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.portintSpecifies the port to connect to when using the specified socket factory. If not set, the default port will be used.
mail.smtp.ssl.protocolsstringSpecifies 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.enablebooleanIf 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.requiredbooleanIf 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.hoststringSpecifies the host name of a SOCKS5 proxy server that will be used for connections to the mail server.
mail.smtp.socks.portstringSpecifies 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.mailextensionStringExtension string to append to the MAIL command.
mail.smtp.usersetbooleanIf 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对象。

示例使用的SMTP服务器是体现在 发送邮件


分类导航
点击按住视频可拖动

缩小

关闭

  • 正在学习
  • 北京总部地址:北京市海淀区西三旗街道建材城西路中腾建华商务大厦东侧二层尚学堂
  • 咨询电话:400-009-1906 010-56233821
  • Copyright 2007-2015 北京尚学堂科技有限公司
  • 京ICP备13018289号-1 京公网安备11010802015183