类 JavaMailSenderImpl
- java.lang.Object
- org.springframework.mail.javamail.JavaMailSenderImpl
- 所有已实现的接口:
JavaMailSender,MailSender
public class JavaMailSenderImpl extends Object implements JavaMailSender
Production implementation of theJavaMailSenderinterface, supporting both JavaMailMimeMessagesand SpringSimpleMailMessages. Can also be used as a plainMailSenderimplementation.Allows for defining all settings locally as bean properties. Alternatively, a pre-configured JavaMail
Sessioncan be specified, possibly pulled from an application server's JNDI environment.Non-default properties in this object will always override the settings in the JavaMail
Session. Note that if overriding all values locally, there is no added value in setting a pre-configuredSession.- 从以下版本开始:
- 10.09.2003
- 作者:
- Dmitriy Kopylenko, Juergen Hoeller
- 另请参阅:
MimeMessage,Session,setSession(javax.mail.Session),setJavaMailProperties(java.util.Properties),setHost(java.lang.String),setPort(int),setUsername(java.lang.String),setPassword(java.lang.String)
字段概要
字段 修饰符和类型 字段 说明 static intDEFAULT_PORTThe default port: -1static StringDEFAULT_PROTOCOLThe default protocol: 'smtp'
构造器概要
构造器 构造器 说明 JavaMailSenderImpl()Create a new instance of theJavaMailSenderImplclass.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected TransportconnectTransport()Obtain and connect a Transport from the underlying JavaMail Session, passing in the specified host, port, username, and password.MimeMessagecreateMimeMessage()This implementation creates a SmartMimeMessage, holding the specified default encoding and default FileTypeMap.MimeMessagecreateMimeMessage(InputStream contentStream)Create a new JavaMail MimeMessage for the underlying JavaMail Session of this sender, using the given input stream as the message source.protected voiddoSend(MimeMessage[] mimeMessages, Object[] originalMessages)Actually send the given array of MimeMessages via JavaMail.StringgetDefaultEncoding()Return the default encoding forMimeMessages, ornullif none.FileTypeMapgetDefaultFileTypeMap()StringgetHost()Return the mail server host.PropertiesgetJavaMailProperties()Allow Map access to the JavaMail properties of this sender, with the option to add or override specific entries.StringgetPassword()Return the password for the account at the mail host.intgetPort()Return the mail server port.StringgetProtocol()Return the mail protocol.SessiongetSession()Return the JavaMailSession, lazily initializing it if hasn't been specified explicitly.protected TransportgetTransport(Session session)Obtain a Transport object from the given JavaMail Session, using the configured protocol.StringgetUsername()Return the username for the account at the mail host.voidsend(MimeMessage mimeMessage)Send the given JavaMail MIME message.voidsend(MimeMessage... mimeMessages)Send the given array of JavaMail MIME messages in batch.voidsend(MimeMessagePreparator mimeMessagePreparator)Send the JavaMail MIME message prepared by the given MimeMessagePreparator.voidsend(MimeMessagePreparator... mimeMessagePreparators)Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.voidsend(SimpleMailMessage simpleMessage)Send the given simple mail message.voidsend(SimpleMailMessage... simpleMessages)Send the given array of simple mail messages in batch.voidsetDefaultEncoding(String defaultEncoding)Set the default encoding to use forMimeMessagescreated by this instance.voidsetDefaultFileTypeMap(FileTypeMap defaultFileTypeMap)Set the default Java ActivationFileTypeMapto use forMimeMessagescreated by this instance.voidsetHost(String host)Set the mail server host, typically an SMTP host.voidsetJavaMailProperties(Properties javaMailProperties)Set JavaMail properties for theSession.voidsetPassword(String password)Set the password for the account at the mail host, if any.voidsetPort(int port)Set the mail server port.voidsetProtocol(String protocol)Set the mail protocol.voidsetSession(Session session)Set the JavaMailSession, possibly pulled from JNDI.voidsetUsername(String username)Set the username for the account at the mail host, if any.voidtestConnection()Validate that this instance can connect to the server that it is configured for.
字段详细资料
DEFAULT_PROTOCOL
public static final String DEFAULT_PROTOCOL
The default protocol: 'smtp'- 另请参阅:
- 常量字段值
DEFAULT_PORT
public static final int DEFAULT_PORT
The default port: -1- 另请参阅:
- 常量字段值
构造器详细资料
JavaMailSenderImpl
public JavaMailSenderImpl()
Create a new instance of theJavaMailSenderImplclass.Initializes the
"defaultFileTypeMap"property with a defaultConfigurableMimeFileTypeMap.
方法详细资料
setJavaMailProperties
public void setJavaMailProperties(Properties javaMailProperties)
Set JavaMail properties for theSession.A new
Sessionwill be created with those properties. Use either this method orsetSession(javax.mail.Session), but not both.Non-default properties in this instance will override given JavaMail properties.
getJavaMailProperties
public Properties getJavaMailProperties()
Allow Map access to the JavaMail properties of this sender, with the option to add or override specific entries.Useful for specifying entries directly, for example via "javaMailProperties[mail.smtp.auth]".
setSession
public void setSession(Session session)
Set the JavaMailSession, possibly pulled from JNDI.Default is a new
Sessionwithout defaults, that is completely configured via this instance's properties.If using a pre-configured
Session, non-default properties in this instance will override the settings in theSession.
getSession
public Session getSession()
Return the JavaMailSession, lazily initializing it if hasn't been specified explicitly.
setProtocol
public void setProtocol(String protocol)
Set the mail protocol. Default is "smtp".
getProtocol
public String getProtocol()
Return the mail protocol.
setHost
public void setHost(String host)
Set the mail server host, typically an SMTP host.Default is the default host of the underlying JavaMail Session.
setPort
public void setPort(int port)
Set the mail server port.Default is
DEFAULT_PORT, letting JavaMail use the default SMTP port (25).
getPort
public int getPort()
Return the mail server port.
setUsername
public void setUsername(String username)
Set the username for the account at the mail host, if any.Note that the underlying JavaMail
Sessionhas to be configured with the property"mail.smtp.auth"set totrue, else the specified username will not be sent to the mail server by the JavaMail runtime. If you are not explicitly passing in aSessionto use, simply specify this setting viasetJavaMailProperties(java.util.Properties).
getUsername
public String getUsername()
Return the username for the account at the mail host.
setPassword
public void setPassword(String password)
Set the password for the account at the mail host, if any.Note that the underlying JavaMail
Sessionhas to be configured with the property"mail.smtp.auth"set totrue, else the specified password will not be sent to the mail server by the JavaMail runtime. If you are not explicitly passing in aSessionto use, simply specify this setting viasetJavaMailProperties(java.util.Properties).
getPassword
public String getPassword()
Return the password for the account at the mail host.
setDefaultEncoding
public void setDefaultEncoding(String defaultEncoding)
Set the default encoding to use forMimeMessagescreated by this instance.Such an encoding will be auto-detected by
MimeMessageHelper.
getDefaultEncoding
public String getDefaultEncoding()
Return the default encoding forMimeMessages, ornullif none.
setDefaultFileTypeMap
public void setDefaultFileTypeMap(FileTypeMap defaultFileTypeMap)
Set the default Java ActivationFileTypeMapto use forMimeMessagescreated by this instance.A
FileTypeMapspecified here will be autodetected byMimeMessageHelper, avoiding the need to specify theFileTypeMapfor eachMimeMessageHelperinstance.For example, you can specify a custom instance of Spring's
ConfigurableMimeFileTypeMaphere. If not explicitly specified, a defaultConfigurableMimeFileTypeMapwill be used, containing an extended set of MIME type mappings (as defined by themime.typesfile contained in the Spring jar).
getDefaultFileTypeMap
public FileTypeMap getDefaultFileTypeMap()
send
public void send(SimpleMailMessage simpleMessage) throws MailException
从接口复制的说明:MailSenderSend the given simple mail message.- 指定者:
send在接口中MailSender- 参数:
simpleMessage- the message to send- 抛出:
MailParseException- in case of failure when parsing the messageMailAuthenticationException- in case of authentication failureMailSendException- in case of failure when sending the messageMailException
send
public void send(SimpleMailMessage... simpleMessages) throws MailException
从接口复制的说明:MailSenderSend the given array of simple mail messages in batch.- 指定者:
send在接口中MailSender- 参数:
simpleMessages- the messages to send- 抛出:
MailParseException- in case of failure when parsing a messageMailAuthenticationException- in case of authentication failureMailSendException- in case of failure when sending a messageMailException
createMimeMessage
public MimeMessage createMimeMessage()
This implementation creates a SmartMimeMessage, holding the specified default encoding and default FileTypeMap. This special defaults-carrying message will be autodetected byMimeMessageHelper, which will use the carried encoding and FileTypeMap unless explicitly overridden.- 指定者:
createMimeMessage在接口中JavaMailSender- 返回:
- the new MimeMessage instance
- 另请参阅:
setDefaultEncoding(java.lang.String),setDefaultFileTypeMap(FileTypeMap)
createMimeMessage
public MimeMessage createMimeMessage(InputStream contentStream) throws MailException
从接口复制的说明:JavaMailSenderCreate a new JavaMail MimeMessage for the underlying JavaMail Session of this sender, using the given input stream as the message source.- 指定者:
createMimeMessage在接口中JavaMailSender- 参数:
contentStream- the raw MIME input stream for the message- 返回:
- the new MimeMessage instance
- 抛出:
MailParseException- in case of message creation failureMailException
send
public void send(MimeMessage mimeMessage) throws MailException
从接口复制的说明:JavaMailSenderSend the given JavaMail MIME message. The message needs to have been created withJavaMailSender.createMimeMessage().- 指定者:
send在接口中JavaMailSender- 参数:
mimeMessage- message to send- 抛出:
MailAuthenticationException- in case of authentication failureMailSendException- in case of failure when sending the messageMailException- 另请参阅:
JavaMailSender.createMimeMessage()
send
public void send(MimeMessage... mimeMessages) throws MailException
从接口复制的说明:JavaMailSenderSend the given array of JavaMail MIME messages in batch. The messages need to have been created withJavaMailSender.createMimeMessage().- 指定者:
send在接口中JavaMailSender- 参数:
mimeMessages- messages to send- 抛出:
MailAuthenticationException- in case of authentication failureMailSendException- in case of failure when sending a messageMailException- 另请参阅:
JavaMailSender.createMimeMessage()
send
public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException
从接口复制的说明:JavaMailSenderSend the JavaMail MIME message prepared by the given MimeMessagePreparator.Alternative way to prepare MimeMessage instances, instead of
JavaMailSender.createMimeMessage()andJavaMailSender.send(MimeMessage)calls. Takes care of proper exception conversion.- 指定者:
send在接口中JavaMailSender- 参数:
mimeMessagePreparator- the preparator to use- 抛出:
MailPreparationException- in case of failure when preparing the messageMailParseException- in case of failure when parsing the messageMailAuthenticationException- in case of authentication failureMailSendException- in case of failure when sending the messageMailException
send
public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException
从接口复制的说明:JavaMailSenderSend the JavaMail MIME messages prepared by the given MimeMessagePreparators.Alternative way to prepare MimeMessage instances, instead of
JavaMailSender.createMimeMessage()andJavaMailSender.send(MimeMessage[])calls. Takes care of proper exception conversion.- 指定者:
send在接口中JavaMailSender- 参数:
mimeMessagePreparators- the preparator to use- 抛出:
MailPreparationException- in case of failure when preparing a messageMailParseException- in case of failure when parsing a messageMailAuthenticationException- in case of authentication failureMailSendException- in case of failure when sending a messageMailException
testConnection
public void testConnection() throws MessagingException
Validate that this instance can connect to the server that it is configured for. Throws aMessagingExceptionif the connection attempt failed.
doSend
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException
Actually send the given array of MimeMessages via JavaMail.- 参数:
mimeMessages- the MimeMessage objects to sendoriginalMessages- corresponding original message objects that the MimeMessages have been created from (with same array length and indices as the "mimeMessages" array), if any- 抛出:
MailAuthenticationException- in case of authentication failureMailSendException- in case of failure when sending a messageMailException
connectTransport
protected Transport connectTransport() throws MessagingException
Obtain and connect a Transport from the underlying JavaMail Session, passing in the specified host, port, username, and password.- 返回:
- the connected Transport object
- 抛出:
MessagingException- if the connect attempt failed- 从以下版本开始:
- 4.1.2
- 另请参阅:
getTransport(javax.mail.Session),getHost(),getPort(),getUsername(),getPassword()
getTransport
protected Transport getTransport(Session session) throws NoSuchProviderException
Obtain a Transport object from the given JavaMail Session, using the configured protocol.Can be overridden in subclasses, e.g. to return a mock Transport object.