程序包 com.sh.mail.core

类 Email

java.lang.Object
com.sh.mail.core.Email
直接已知子类:
MultiPartEmail, SimpleEmail

public abstract class Email extends Object

Description: The base class for all email messages. This class sets the sender's email name, receiver's email name, subject, and the sent date. Subclasses are responsible for setting the message body.

Title:首亨软件 - TSS/CRM等

从以下版本开始:
Aug 13, 2012
  • 字段详细资料

    • SENDER_EMAIL

      public static final String SENDER_EMAIL
      另请参阅:
    • SENDER_NAME

      public static final String SENDER_NAME
      另请参阅:
    • RECEIVER_EMAIL

      public static final String RECEIVER_EMAIL
      另请参阅:
    • RECEIVER_NAME

      public static final String RECEIVER_NAME
      另请参阅:
    • EMAIL_SUBJECT

      public static final String EMAIL_SUBJECT
      另请参阅:
    • EMAIL_BODY

      public static final String EMAIL_BODY
      另请参阅:
    • CONTENT_TYPE

      public static final String CONTENT_TYPE
      另请参阅:
    • MAIL_HOST

      public static final String MAIL_HOST
      另请参阅:
    • MAIL_PORT

      public static final String MAIL_PORT
      另请参阅:
    • MAIL_SMTP_FROM

      public static final String MAIL_SMTP_FROM
      另请参阅:
    • MAIL_SMTP_AUTH

      public static final String MAIL_SMTP_AUTH
      另请参阅:
    • MAIL_SMTP_USER

      public static final String MAIL_SMTP_USER
      另请参阅:
    • MAIL_SMTP_PASSWORD

      public static final String MAIL_SMTP_PASSWORD
      另请参阅:
    • MAIL_TRANSPORT_PROTOCOL

      public static final String MAIL_TRANSPORT_PROTOCOL
      另请参阅:
    • MAIL_TRANSPORT_TLS

      public static final String MAIL_TRANSPORT_TLS
      从以下版本开始:
      1.1
      另请参阅:
    • MAIL_SMTP_SOCKET_FACTORY_FALLBACK

      public static final String MAIL_SMTP_SOCKET_FACTORY_FALLBACK
      另请参阅:
    • MAIL_SMTP_SOCKET_FACTORY_CLASS

      public static final String MAIL_SMTP_SOCKET_FACTORY_CLASS
      另请参阅:
    • MAIL_SMTP_SOCKET_FACTORY_PORT

      public static final String MAIL_SMTP_SOCKET_FACTORY_PORT
      另请参阅:
    • MAIL_SMTP_CONNECTIONTIMEOUT

      public static final String MAIL_SMTP_CONNECTIONTIMEOUT
      Socket connection timeout value in milliseconds. Default is infinite timeout.
      从以下版本开始:
      1.2
      另请参阅:
    • MAIL_SMTP_TIMEOUT

      public static final String MAIL_SMTP_TIMEOUT
      Socket I/O timeout value in milliseconds. Default is infinite timeout.
      从以下版本开始:
      1.2
      另请参阅:
    • SMTP

      public static final String SMTP
      另请参阅:
    • TEXT_HTML

      public static final String TEXT_HTML
      另请参阅:
    • TEXT_PLAIN

      public static final String TEXT_PLAIN
      另请参阅:
    • ATTACHMENTS

      public static final String ATTACHMENTS
      另请参阅:
    • FILE_SERVER

      public static final String FILE_SERVER
      另请参阅:
    • MAIL_DEBUG

      public static final String MAIL_DEBUG
      另请参阅:
    • KOI8_R

      public static final String KOI8_R
      另请参阅:
    • ISO_8859_1

      public static final String ISO_8859_1
      另请参阅:
    • US_ASCII

      public static final String US_ASCII
      另请参阅:
    • message

      protected jakarta.mail.internet.MimeMessage message
      The email message to send.
    • charset

      protected String charset
      The charset to use for this message
    • fromAddress

      protected jakarta.mail.internet.InternetAddress fromAddress
      The Address of the sending party, mandatory
    • subject

      protected String subject
      The Subject
    • emailBody

      protected jakarta.mail.internet.MimeMultipart emailBody
      An attachment
    • content

      protected Object content
      The content
    • contentType

      protected String contentType
      The content type
    • debug

      protected boolean debug
      Set session debugging on or off
    • sentDate

      protected Date sentDate
      Sent date
    • authenticator

      protected jakarta.mail.Authenticator authenticator
      Instance of an Authenticator object that will be used when authentication is requested from the mail server.
    • hostName

      protected String hostName
      The hostname of the mail server with which to connect. If null will try to get property from system.properties. If still null, quit
    • smtpPort

      protected String smtpPort
      The port number of the mail server to connect to. Defaults to the standard port ( 25 ).
    • sslSmtpPort

      protected String sslSmtpPort
      The port number of the SSL enabled SMTP server; defaults to the standard port, 465.
    • toList

      protected List toList
      List of "to" email adresses
    • ccList

      protected List ccList
      List of "cc" email adresses
    • bccList

      protected List bccList
      List of "bcc" email adresses
    • replyList

      protected List replyList
      List of "replyTo" email adresses
    • bounceAddress

      protected String bounceAddress
      Address to which undeliverable mail should be sent. Because this is handled by JavaMail as a String property in the mail session, this property is of type String rather than InternetAddress.
    • headers

      protected Map headers
      Used to specify the mail headers. Example: X-Mailer: Sendmail, X-Priority: 1( highest ) or 2( high ) 3( normal ) 4( low ) and 5( lowest ) Disposition-Notification-To: user@domain.net
    • popBeforeSmtp

      protected boolean popBeforeSmtp
      Used to determine whether to use pop3 before smtp, and if so the settings.
    • popHost

      protected String popHost
      the host name of the pop3 server
    • popUsername

      protected String popUsername
      the user name to log into the pop3 server
    • popPassword

      protected String popPassword
      the password to log into the pop3 server
    • tls

      protected boolean tls
      does server require TLS encryption for authentication
    • ssl

      protected boolean ssl
      does the current transport use SSL encryption?
    • socketTimeout

      protected int socketTimeout
      socket I/O timeout value in milliseconds
    • socketConnectionTimeout

      protected int socketConnectionTimeout
      socket connection timeout value in milliseconds
  • 构造器详细资料

    • Email

      public Email()
  • 方法详细资料

    • setDebug

      public void setDebug(boolean d)
      Setting to true will enable the display of debug information.
      参数:
      d - A boolean.
      从以下版本开始:
      1.0
    • setAuthentication

      public void setAuthentication(String userName, String password)
      Sets the userName and password if authentication is needed. If this method is not used, no authentication will be performed.

      This method will create a new instance of DefaultAuthenticator using the supplied parameters.

      参数:
      userName - User name for the SMTP server
      password - password for the SMTP server
      从以下版本开始:
      1.0
      另请参阅:
    • setAuthenticator

      public void setAuthenticator(jakarta.mail.Authenticator newAuthenticator)
      Sets the Authenticator to be used when authentication is requested from the mail server.

      This method should be used when your outgoing mail server requires authentication. Your mail server must also support RFC2554.

      参数:
      newAuthenticator - the Authenticator object.
      从以下版本开始:
      1.0
      另请参阅:
      • Authenticator
    • setCharset

      public void setCharset(String newCharset)
      Set the charset of the message.
      参数:
      newCharset - A String.
      抛出:
      IllegalCharsetNameException - if the charset name is invalid
      UnsupportedCharsetException - if no support for the named charset exists in the current JVM
      从以下版本开始:
      1.0
    • setContent

      public void setContent(jakarta.mail.internet.MimeMultipart aMimeMultipart)
      Set the emailBody to a MimeMultiPart
      参数:
      aMimeMultipart - aMimeMultipart
      从以下版本开始:
      1.0
    • setContent

      public void setContent(Object aObject, String aContentType)
      Set the content contentType
      参数:
      aObject - aObject
      aContentType - aContentType
      从以下版本开始:
      1.0
    • updateContentType

      public void updateContentType(String aContentType)
      Update the contentType.
      参数:
      aContentType - aContentType
      从以下版本开始:
      1.2
    • setHostName

      public void setHostName(String aHostName)
      Set the hostname of the outgoing mail server
      参数:
      aHostName - aHostName
      从以下版本开始:
      1.0
    • setTLS

      public void setTLS(boolean withTLS)
      Set or disable the TLS encryption
      参数:
      withTLS - true if TLS needed, false otherwise
      从以下版本开始:
      1.1
    • setSmtpPort

      public void setSmtpPort(int aPortNumber)
      Set the port number of the outgoing mail server.
      参数:
      aPortNumber - aPortNumber
      从以下版本开始:
      1.0
    • setMailSession

      public void setMailSession(jakarta.mail.Session aSession)
      Supply a mail Session object to use. Please note that passing a username and password (in the case of mail authentication) will create a new mail session with a DefaultAuthenticator. This is a convience but might come unexpected. If mail authentication is used but NO username and password is supplied the implementation assumes that you have set a authenticator and will use the existing mail session (as expected).
      参数:
      aSession - mail session to be used
      从以下版本开始:
      1.0
    • setMailSessionFromJNDI

      public void setMailSessionFromJNDI(String jndiName) throws NamingException
      Supply a mail Session object from a JNDI directory
      参数:
      jndiName - name of JNDI ressource (javax.mail.Session type), ressource if searched in java:comp/env if name dont start with "java:"
      抛出:
      IllegalArgumentException - JNDI name null or empty
      NamingException - ressource can be retrieved from JNDI directory
      从以下版本开始:
      1.1
    • getMailSession

      public jakarta.mail.Session getMailSession() throws EmailException
      Initialise a mailsession object
      返回:
      A Session.
      抛出:
      EmailException - thrown when host name was not set.
      从以下版本开始:
      1.0
    • setFrom

      public Email setFrom(String email) throws EmailException
      Set the FROM field of the email to use the specified address. The email address will also be used as the personal name. The name will be encoded by the charset of setCharset(). If it is not set, it will be encoded using the Java platform's default charset (UTF-16) if it contains non-ASCII characters; otherwise, it is used as is.
      参数:
      email - A String.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address.
      从以下版本开始:
      1.0
    • setFrom

      public Email setFrom(String email, String name) throws EmailException
      Set the FROM field of the email to use the specified address and the specified personal name. The name will be encoded by the charset of setCharset(). If it is not set, it will be encoded using the Java platform's default charset (UTF-16) if it contains non-ASCII characters; otherwise, it is used as is.
      参数:
      email - A String.
      name - A String.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address.
      从以下版本开始:
      1.0
    • setFrom

      public Email setFrom(String email, String name, String charset) throws EmailException
      Set the FROM field of the email to use the specified address, personal name, and charset encoding for the name.
      参数:
      email - A String.
      name - A String.
      charset - The charset to encode the name with.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address or charset.
      从以下版本开始:
      1.1
    • addTo

      public Email addTo(String email) throws EmailException
      Add a recipient TO to the email. The email address will also be used as the personal name. The name will be encoded by the charset of setCharset(). If it is not set, it will be encoded using the Java platform's default charset (UTF-16) if it contains non-ASCII characters; otherwise, it is used as is.
      参数:
      email - A String.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address.
      从以下版本开始:
      1.0
    • addTo

      public Email addTo(String email, String name) throws EmailException
      Add a recipient TO to the email using the specified address and the specified personal name. The name will be encoded by the charset of setCharset(). If it is not set, it will be encoded using the Java platform's default charset (UTF-16) if it contains non-ASCII characters; otherwise, it is used as is.
      参数:
      email - A String.
      name - A String.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address.
      从以下版本开始:
      1.0
    • addTo

      public Email addTo(String email, String name, String charset) throws EmailException
      Add a recipient TO to the email using the specified address, personal name, and charset encoding for the name.
      参数:
      email - A String.
      name - A String.
      charset - The charset to encode the name with.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address or charset.
      从以下版本开始:
      1.1
    • setTo

      public Email setTo(Collection aCollection) throws EmailException
      Set a list of "TO" addresses. All elements in the specified Collection are expected to be of type java.mail.internet.InternetAddress.
      参数:
      aCollection - collection of InternetAddress objects.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address.
      从以下版本开始:
      1.0
      另请参阅:
      • javax.mail.internet.InternetAddress
    • addCc

      public Email addCc(String email) throws EmailException
      Add a recipient CC to the email. The email address will also be used as the personal name. The name will be encoded by the charset of setCharset(). If it is not set, it will be encoded using the Java platform's default charset (UTF-16) if it contains non-ASCII characters; otherwise, it is used as is.
      参数:
      email - A String.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address.
      从以下版本开始:
      1.0
    • addCc

      public Email addCc(String email, String name) throws EmailException
      Add a recipient CC to the email using the specified address and the specified personal name. The name will be encoded by the charset of setCharset(). If it is not set, it will be encoded using the Java platform's default charset (UTF-16) if it contains non-ASCII characters; otherwise, it is used as is.
      参数:
      email - A String.
      name - A String.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address.
      从以下版本开始:
      1.0
    • addCc

      public Email addCc(String email, String name, String charset) throws EmailException
      Add a recipient CC to the email using the specified address, personal name, and charset encoding for the name.
      参数:
      email - A String.
      name - A String.
      charset - The charset to encode the name with.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address or charset.
      从以下版本开始:
      1.1
    • setCc

      public Email setCc(Collection aCollection) throws EmailException
      Set a list of "CC" addresses. All elements in the specified Collection are expected to be of type java.mail.internet.InternetAddress.
      参数:
      aCollection - collection of InternetAddress objects.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address.
      从以下版本开始:
      1.0
      另请参阅:
      • javax.mail.internet.InternetAddress
    • addBcc

      public Email addBcc(String email) throws EmailException
      Add a blind BCC recipient to the email. The email address will also be used as the personal name. The name will be encoded by the charset of setCharset(). If it is not set, it will be encoded using the Java platform's default charset (UTF-16) if it contains non-ASCII characters; otherwise, it is used as is.
      参数:
      email - A String.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address
      从以下版本开始:
      1.0
    • addBcc

      public Email addBcc(String email, String name) throws EmailException
      Add a blind BCC recipient to the email using the specified address and the specified personal name. The name will be encoded by the charset of setCharset(). If it is not set, it will be encoded using the Java platform's default charset (UTF-16) if it contains non-ASCII characters; otherwise, it is used as is.
      参数:
      email - A String.
      name - A String.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address
      从以下版本开始:
      1.0
    • addBcc

      public Email addBcc(String email, String name, String charset) throws EmailException
      Add a blind BCC recipient to the email using the specified address, personal name, and charset encoding for the name.
      参数:
      email - A String.
      name - A String.
      charset - The charset to encode the name with.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address
      从以下版本开始:
      1.1
    • setBcc

      public Email setBcc(Collection aCollection) throws EmailException
      Set a list of "BCC" addresses. All elements in the specified Collection are expected to be of type java.mail.internet.InternetAddress.
      参数:
      aCollection - collection of InternetAddress objects
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address
      从以下版本开始:
      1.0
      另请参阅:
      • javax.mail.internet.InternetAddress
    • addReplyTo

      public Email addReplyTo(String email) throws EmailException
      Add a reply to address to the email. The email address will also be used as the personal name. The name will be encoded by the charset of setCharset(). If it is not set, it will be encoded using the Java platform's default charset (UTF-16) if it contains non-ASCII characters; otherwise, it is used as is.
      参数:
      email - A String.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address
      从以下版本开始:
      1.0
    • addReplyTo

      public Email addReplyTo(String email, String name) throws EmailException
      Add a reply to address to the email using the specified address and the specified personal name. The name will be encoded by the charset of setCharset(). If it is not set, it will be encoded using the Java platform's default charset (UTF-16) if it contains non-ASCII characters; otherwise, it is used as is.
      参数:
      email - A String.
      name - A String.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address
      从以下版本开始:
      1.0
    • addReplyTo

      public Email addReplyTo(String email, String name, String charset) throws EmailException
      Add a reply to address to the email using the specified address, personal name, and charset encoding for the name.
      参数:
      email - A String.
      name - A String.
      charset - The charset to encode the name with.
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address or charset.
      从以下版本开始:
      1.1
    • setReplyTo

      public Email setReplyTo(Collection aCollection) throws EmailException
      Set a list of reply to addresses. All elements in the specified Collection are expected to be of type java.mail.internet.InternetAddress.
      参数:
      aCollection - collection of InternetAddress objects
      返回:
      An Email.
      抛出:
      EmailException - Indicates an invalid email address
      从以下版本开始:
      1.1
      另请参阅:
      • javax.mail.internet.InternetAddress
    • setHeaders

      public void setHeaders(Map map)
      Used to specify the mail headers. Example: X-Mailer: Sendmail, X-Priority: 1( highest ) or 2( high ) 3( normal ) 4( low ) and 5( lowest ) Disposition-Notification-To: user@domain.net
      参数:
      map - A Map.
      从以下版本开始:
      1.0
    • addHeader

      public void addHeader(String name, String value)
      Adds a header ( name, value ) to the headers Map.
      参数:
      name - A String with the name.
      value - A String with the value.
      从以下版本开始:
      1.0
    • setSubject

      public Email setSubject(String aSubject)
      Set the email subject.
      参数:
      aSubject - A String.
      返回:
      An Email.
      从以下版本开始:
      1.0
    • setBounceAddress

      public Email setBounceAddress(String email)
      Set the "bounce address" - the address to which undeliverable messages will be returned. If this value is never set, then the message will be sent to the address specified with the System property "mail.smtp.from", or if that value is not set, then to the "from" address.
      参数:
      email - A String.
      返回:
      An Email.
      从以下版本开始:
      1.0
    • setMsg

      public abstract Email setMsg(String msg) throws EmailException
      Define the content of the mail. It should be overidden by the subclasses.
      参数:
      msg - A String.
      返回:
      An Email.
      抛出:
      EmailException - generic exception.
      从以下版本开始:
      1.0
    • buildMimeMessage

      public void buildMimeMessage() throws EmailException
      Build the internal MimeMessage to be sent.
      抛出:
      EmailException - if there was an error.
      从以下版本开始:
      1.0
    • createMimeMessage

      protected jakarta.mail.internet.MimeMessage createMimeMessage(jakarta.mail.Session aSession)
      Factory method to create a customized MimeMessage which can be implemented by a derived class, e.g. to set the message id.
      参数:
      aSession - mail session to be used
      返回:
      the newly created message
    • sendMimeMessage

      public String sendMimeMessage() throws EmailException
      Sends the previously created MimeMessage to the SMTP server.
      返回:
      the message id of the underlying MimeMessage
      抛出:
      EmailException - the sending failed
    • getMimeMessage

      public jakarta.mail.internet.MimeMessage getMimeMessage()
      Returns the internal MimeMessage. Please not that the MimeMessage is build by the buildMimeMessage() method.
      返回:
      the MimeMessage
    • send

      public String send() throws EmailException
      Sends the email. Internally we build a MimeMessage which is afterwards sent to the SMTP server.
      返回:
      the message id of the underlying MimeMessage
      抛出:
      EmailException - the sending failed
    • setSentDate

      public void setSentDate(Date date)
      Sets the sent date for the email. The sent date will default to the current date if not explictly set.
      参数:
      date - Date to use as the sent date on the email
      从以下版本开始:
      1.0
    • getSentDate

      public Date getSentDate()
      Gets the sent date for the email.
      返回:
      date to be used as the sent date for the email
      从以下版本开始:
      1.0
    • getSubject

      public String getSubject()
      Gets the subject of the email.
      返回:
      email subject
    • getFromAddress

      public jakarta.mail.internet.InternetAddress getFromAddress()
      Gets the sender of the email.
      返回:
      from address
    • getHostName

      public String getHostName()
      Gets the host name of the SMTP server,
      返回:
      host name
    • getSmtpPort

      public String getSmtpPort()
      Gets the listening port of the SMTP server.
      返回:
      smtp port
    • isTLS

      public boolean isTLS()
      Gets encryption mode for authentication
      返回:
      true if using TLS for authentication, false otherwise
      从以下版本开始:
      1.1
    • toInternetAddressArray

      protected jakarta.mail.internet.InternetAddress[] toInternetAddressArray(List list)
      Utility to copy List of known InternetAddress objects into an array.
      参数:
      list - A List.
      返回:
      An InternetAddress[].
      从以下版本开始:
      1.0
    • setPopBeforeSmtp

      public void setPopBeforeSmtp(boolean newPopBeforeSmtp, String newPopHost, String newPopUsername, String newPopPassword)
      Set details regarding "pop3 before smtp" authentication.
      参数:
      newPopBeforeSmtp - Wether or not to log into pop3 server before sending mail.
      newPopHost - The pop3 host to use.
      newPopUsername - The pop3 username.
      newPopPassword - The pop3 password.
      从以下版本开始:
      1.0
    • isSSL

      public boolean isSSL()
      Returns whether SSL encryption for the transport is currently enabled.
      返回:
      true if SSL enabled for the transport
    • setSSL

      public void setSSL(boolean ssl)
      Sets whether SSL encryption should be enabled for the SMTP transport.
      参数:
      ssl - whether to enable the SSL transport
    • getSslSmtpPort

      public String getSslSmtpPort()
      Returns the current SSL port used by the SMTP transport.
      返回:
      the current SSL port used by the SMTP transport
    • setSslSmtpPort

      public void setSslSmtpPort(String sslSmtpPort)
      Sets the SSL port to use for the SMTP transport. Defaults to the standard port, 465.
      参数:
      sslSmtpPort - the SSL port to use for the SMTP transport
    • getToAddresses

      public List getToAddresses()
      Get the list of "To" addresses.
      返回:
      List addresses
    • resetToAddress

      public void resetToAddress()
      重置收件人列表
    • getCcAddresses

      public List getCcAddresses()
      Get the list of "CC" addresses.
      返回:
      List addresses
    • getBccAddresses

      public List getBccAddresses()
      Get the list of "Bcc" addresses.
      返回:
      List addresses
    • getReplyToAddresses

      public List getReplyToAddresses()
      Get the list of "Reply-To" addresses.
      返回:
      List addresses
    • getSocketConnectionTimeout

      public int getSocketConnectionTimeout()
      Get the socket connection timeout value in milliseconds.
      返回:
      the timeout in milliseconds.
      从以下版本开始:
      1.2
    • setSocketConnectionTimeout

      public void setSocketConnectionTimeout(int socketConnectionTimeout)
      Set the socket connection timeout value in milliseconds. Default is infinite timeout.
      参数:
      socketConnectionTimeout - the connection timeout
      从以下版本开始:
      1.2
    • getSocketTimeout

      public int getSocketTimeout()
      Get the socket I/O timeout value in milliseconds.
      返回:
      the socket I/O timeout
      从以下版本开始:
      1.2
    • setSocketTimeout

      public void setSocketTimeout(int socketTimeout)
      Set the socket I/O timeout value in milliseconds. Default is infinite timeout.
      参数:
      socketTimeout - the socket I/O timeout
      从以下版本开始:
      1.2