📄 email.java
字号:
*/ public Session getMailSession() throws EmailException { if (this.session == null) { Properties properties = new Properties(System.getProperties()); properties.setProperty(MAIL_TRANSPORT_PROTOCOL, SMTP); if (EmailUtils.isEmpty(this.hostName)) { this.hostName = properties.getProperty(MAIL_HOST); } if (EmailUtils.isEmpty(this.hostName)) { throw new EmailException( "Cannot find valid hostname for mail session"); } properties.setProperty(MAIL_PORT, smtpPort); properties.setProperty(MAIL_HOST, hostName); properties.setProperty(MAIL_DEBUG, String.valueOf(this.debug)); if (this.authenticator != null) { properties.setProperty(MAIL_TRANSPORT_TLS, tls ? "true" : "false"); properties.setProperty(MAIL_SMTP_AUTH, "true"); } if (this.ssl) { properties.setProperty(MAIL_PORT, sslSmtpPort); properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_PORT, sslSmtpPort); properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_CLASS, "javax.net.ssl.SSLSocketFactory"); properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_FALLBACK, "false"); } if (this.bounceAddress != null) { properties.setProperty(MAIL_SMTP_FROM, this.bounceAddress); } // changed this (back) to getInstance due to security exceptions // caused when testing using maven this.session = Session.getInstance(properties, this.authenticator); } return this.session; } /** * Creates a InternetAddress. * * @param email An email address. * @param name A name. * @param charsetName The name of the charset to encode the name with. * @return An internet address. * @throws EmailException Thrown when the supplied address, name or charset were invalid. */ private InternetAddress createInternetAddress(String email, String name, String charsetName) throws EmailException { InternetAddress address = null; try { address = new InternetAddress(email); // check name input if (EmailUtils.isEmpty(name)) { name = email; } // check charset input. if (EmailUtils.isEmpty(charsetName)) { address.setPersonal(name); } else { // canonicalize the charset name and make sure // the current platform supports it. Charset set = Charset.forName(charsetName); address.setPersonal(name, set.name()); } // run sanity check on new InternetAddress object; if this fails // it will throw AddressException. address.validate(); } catch (AddressException e) { throw new EmailException(e); } catch (UnsupportedEncodingException e) { throw new EmailException(e); } return address; } /** * 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 * using the Java platform's default charset (UTF-16) if it contains * non-ASCII characters; otherwise, it is used as is. * * @param email A String. * @return An Email. * @throws EmailException Indicates an invalid email address. * @since 1.0 */ public Email setFrom(String email) throws EmailException { return setFrom(email, null); } /** * Set the FROM field of the email to use the specified address and the * specified personal name. The name will be encoded using the Java * platform's default charset (UTF-16) if it contains non-ASCII * characters; otherwise, it is used as is. * * @param email A String. * @param name A String. * @throws EmailException Indicates an invalid email address. * @return An Email. * @since 1.0 */ public Email setFrom(String email, String name) throws EmailException { return setFrom(email, name, null); } /** * Set the FROM field of the email to use the specified address, personal * name, and charset encoding for the name. * * @param email A String. * @param name A String. * @param charset The charset to encode the name with. * @throws EmailException Indicates an invalid email address or charset. * @return An Email. * @since 1.1 */ public Email setFrom(String email, String name, String charset) throws EmailException { this.fromAddress = createInternetAddress(email, name, charset); return this; } /** * Add a recipient TO to the email. The email * address will also be used as the personal name. The name will be encoded * using the Java platform's default charset (UTF-16) if it contains * non-ASCII characters; otherwise, it is used as is. * * @param email A String. * @throws EmailException Indicates an invalid email address. * @return An Email. * @since 1.0 */ public Email addTo(String email) throws EmailException { return addTo(email, null); } /** * Add a recipient TO to the email using the specified address and the * specified personal name. The name will be encoded using the Java * platform's default charset (UTF-16) if it contains non-ASCII * characters; otherwise, it is used as is. * * @param email A String. * @param name A String. * @throws EmailException Indicates an invalid email address. * @return An Email. * @since 1.0 */ public Email addTo(String email, String name) throws EmailException { return addTo(email, name, null); } /** * Add a recipient TO to the email using the specified address, personal * name, and charset encoding for the name. * * @param email A String. * @param name A String. * @param charset The charset to encode the name with. * @throws EmailException Indicates an invalid email address or charset. * @return An Email. * @since 1.1 */ public Email addTo(String email, String name, String charset) throws EmailException { this.toList.add(createInternetAddress(email, name, charset)); return this; } /** * Set a list of "TO" addresses. All elements in the specified * <code>Collection</code> are expected to be of type * <code>java.mail.internet.InternetAddress</code>. * * @param aCollection collection of <code>InternetAddress</code> objects. * @throws EmailException Indicates an invalid email address. * @return An Email. * @see javax.mail.internet.InternetAddress * @since 1.0 */ public Email setTo(Collection aCollection) throws EmailException { if (aCollection == null || aCollection.isEmpty()) { throw new EmailException("Address List provided was invalid"); } this.toList = new ArrayList(aCollection); return this; } /** * Add a recipient CC to the email. The email * address will also be used as the personal name. The name will be encoded * using the Java platform's default charset (UTF-16) if it contains * non-ASCII characters; otherwise, it is used as is. * * @param email A String. * @return An Email. * @throws EmailException Indicates an invalid email address. * @since 1.0 */ public Email addCc(String email) throws EmailException { return this.addCc(email, null); } /** * Add a recipient CC to the email using the specified address and the * specified personal name. The name will be encoded using the Java * platform's default charset (UTF-16) if it contains non-ASCII * characters; otherwise, it is used as is. * * @param email A String. * @param name A String. * @throws EmailException Indicates an invalid email address. * @return An Email. * @since 1.0 */ public Email addCc(String email, String name) throws EmailException { return addCc(email, name, null); } /** * Add a recipient CC to the email using the specified address, personal * name, and charset encoding for the name. * * @param email A String. * @param name A String. * @param charset The charset to encode the name with. * @throws EmailException Indicates an invalid email address or charset. * @return An Email. * @since 1.1 */ public Email addCc(String email, String name, String charset) throws EmailException { this.ccList.add(createInternetAddress(email, name, charset)); return this; } /** * Set a list of "CC" addresses. All elements in the specified * <code>Collection</code> are expected to be of type * <code>java.mail.internet.InternetAddress</code>. * * @param aCollection collection of <code>InternetAddress</code> objects. * @return An Email. * @throws EmailException Indicates an invalid email address. * @see javax.mail.internet.InternetAddress * @since 1.0 */ public Email setCc(Collection aCollection) throws EmailException { if (aCollection == null || aCollection.isEmpty()) { throw new EmailException("Address List provided was invalid"); } this.ccList = new ArrayList(aCollection); return this; } /** * Add a blind BCC recipient to the email. The email * address will also be used as the personal name. The name will be encoded * using the Java platform's default charset (UTF-16) if it contains * non-ASCII characters; otherwise, it is used as is. * * @param email A String. * @return An Email. * @throws EmailException Indicates an invalid email address * @since 1.0 */ public Email addBcc(String email) throws EmailException { return this.addBcc(email, null); } /** * Add a blind BCC recipient to the email using the specified address and * the specified personal name. The name will be encoded using the Java * platform's default charset (UTF-16) if it contains non-ASCII * characters; otherwise, it is used as is. * * @param email A String. * @param name A String. * @return An Email. * @throws EmailException Indicates an invalid email address * @since 1.0 */ public Email addBcc(String email, String name) throws EmailException { return addBcc(email, name, null); } /** * Add a blind BCC recipient to the email using the specified address, * personal name, and charset encoding for the name. * * @param email A String. * @param name A String. * @param charset The charset to encode the name with. * @return An Email. * @throws EmailException Indicates an invalid email address * @since 1.1 */ public Email addBcc(String email, String name, String charset) throws EmailException { this.bccList.add(createInternetAddress(email, name, charset)); return this; } /** * Set a list of "BCC" addresses. All elements in the specified * <code>Collection</code> are expected to be of type * <code>java.mail.internet.InternetAddress</code>. * * @param aCollection collection of <code>InternetAddress</code> objects * @return An Email. * @throws EmailException Indicates an invalid email address * @see javax.mail.internet.InternetAddress * @since 1.0 */ public Email setBcc(Collection aCollection) throws EmailException { if (aCollection == null || aCollection.isEmpty()) { throw new EmailException("Address List provided was invalid"); } this.bccList = new ArrayList(aCollection); return this; } /** * Add a reply to address to the email. The email * address will also be used as the personal name. The name will be encoded * using the Java platform's default charset (UTF-16) if it contains * non-ASCII characters; otherwise, it is used as is. * * @param email A String. * @return An Email. * @throws EmailException Indicates an invalid email address * @since 1.0 */ public Email addReplyTo(String email) throws EmailException { return this.addReplyTo(email, null); } /** * Add a reply to address to the email using the specified address and * the specified personal name. The name will be encoded using the Java * platform's default charset (UTF-16) if it contains non-ASCII * characters; otherwise, it is used as is. * * @param email A String. * @param name A String. * @return An Email. * @throws EmailException Indicates an invalid email address * @since 1.0 */ public Email addReplyTo(String email, String name) throws EmailException { return addReplyTo(email, name, null); } /** * Add a reply to address to the email using the specified address, * personal name, and charset encoding for the name. * * @param email A String. * @param name A String. * @param charset The charset to encode the name with. * @return An Email. * @throws EmailException Indicates an invalid email address or charset. * @since 1.1 */ public Email addReplyTo(String email, String name, String charset) throws EmailException { this.replyList.add(createInternetAddress(email, name, charset)); return this; } /** * Set a list of reply to addresses. All elements in the specified * <code>Collection</code> are expected to be of type * <code>java.mail.internet.InternetAddress</code>. * * @param aCollection collection of <code>InternetAddress</code> objects * @return An Email. * @throws EmailException Indicates an invalid email address * @see javax.mail.internet.InternetAddress * @since 1.1 */ public Email setReplyTo(Collection aCollection) throws EmailException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -