📄 email.java
字号:
if (aCollection == null || aCollection.isEmpty()) { throw new EmailException("Address List provided was invalid"); } this.replyList = new ArrayList(aCollection); return this; } /** * 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 * * @param map A Map. * @since 1.0 */ public void setHeaders(Map map) { Iterator iterKeyBad = map.entrySet().iterator(); while (iterKeyBad.hasNext()) { Map.Entry entry = (Map.Entry) iterKeyBad.next(); String strName = (String) entry.getKey(); String strValue = (String) entry.getValue(); if (EmailUtils.isEmpty(strName)) { throw new IllegalArgumentException("name can not be null"); } if (EmailUtils.isEmpty(strValue)) { throw new IllegalArgumentException("value can not be null"); } } // all is ok, update headers this.headers = map; } /** * Adds a header ( name, value ) to the headers Map. * * @param name A String with the name. * @param value A String with the value. * @since 1.0 */ public void addHeader(String name, String value) { if (EmailUtils.isEmpty(name)) { throw new IllegalArgumentException("name can not be null"); } if (EmailUtils.isEmpty(value)) { throw new IllegalArgumentException("value can not be null"); } this.headers.put(name, value); } /** * Set the email subject. * * @param aSubject A String. * @return An Email. * @since 1.0 */ public Email setSubject(String aSubject) { this.subject = aSubject; return this; } /** * 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. * * @param email A String. * @return An Email. * @since 1.0 */ public Email setBounceAddress(String email) { this.bounceAddress = email; return this; } /** * Define the content of the mail. It should be overidden by the * subclasses. * * @param msg A String. * @return An Email. * @throws EmailException generic exception. * @since 1.0 */ public abstract Email setMsg(String msg) throws EmailException; /** * Build the internal MimeMessage to be sent. * * @throws EmailException if there was an error. * @since 1.0 */ public void buildMimeMessage() throws EmailException { try { this.getMailSession(); this.message = new MimeMessage(this.session); if (EmailUtils.isNotEmpty(this.subject)) { if (EmailUtils.isNotEmpty(this.charset)) { this.message.setSubject(this.subject, this.charset); } else { this.message.setSubject(this.subject); } } // ======================================================== // Start of replacement code if (this.content != null) { this.message.setContent(this.content, this.contentType); } // end of replacement code // ======================================================== else if (this.emailBody != null) { this.message.setContent(this.emailBody); } else { this.message.setContent("", Email.TEXT_PLAIN); } if (this.fromAddress != null) { this.message.setFrom(this.fromAddress); } else { if (session.getProperty(MAIL_SMTP_FROM) == null) { throw new EmailException("From address required"); } } if (this.toList.size() + this.ccList.size() + this.bccList.size() == 0) { throw new EmailException( "At least one receiver address required"); } if (this.toList.size() > 0) { this.message.setRecipients( Message.RecipientType.TO, this.toInternetAddressArray(this.toList)); } if (this.ccList.size() > 0) { this.message.setRecipients( Message.RecipientType.CC, this.toInternetAddressArray(this.ccList)); } if (this.bccList.size() > 0) { this.message.setRecipients( Message.RecipientType.BCC, this.toInternetAddressArray(this.bccList)); } if (this.replyList.size() > 0) { this.message.setReplyTo( this.toInternetAddressArray(this.replyList)); } if (this.headers.size() > 0) { Iterator iterHeaderKeys = this.headers.keySet().iterator(); while (iterHeaderKeys.hasNext()) { String name = (String) iterHeaderKeys.next(); String value = (String) headers.get(name); this.message.addHeader(name, value); } } if (this.message.getSentDate() == null) { this.message.setSentDate(getSentDate()); } if (this.popBeforeSmtp) { Store store = session.getStore("pop3"); store.connect(this.popHost, this.popUsername, this.popPassword); } } catch (MessagingException me) { throw new EmailException(me); } } /** * Sends the previously created MimeMessage to the SMTP server. * * @return the message id of the underlying MimeMessage * @throws EmailException the sending failed */ public String sendMimeMessage() throws EmailException { EmailUtils.notNull(this.message, "message"); try { Transport.send(this.message); return this.message.getMessageID(); } catch (Throwable t) { String msg = "Sending the email to the following server failed : " + this.getHostName() + ":" + this.getSmtpPort(); throw new EmailException(msg, t); } } /** * Returns the internal MimeMessage. Please not that the * MimeMessage is build by the buildMimeMessage() method. * * @return the MimeMessage */ public MimeMessage getMimeMessage() { return this.message; } /** * Sends the email. Internally we build a MimeMessage * which is afterwards sent to the SMTP server. * * @return the message id of the underlying MimeMessage * @throws EmailException the sending failed */ public String send() throws EmailException { this.buildMimeMessage(); return this.sendMimeMessage(); } /** * Sets the sent date for the email. The sent date will default to the * current date if not explictly set. * * @param date Date to use as the sent date on the email * @since 1.0 */ public void setSentDate(Date date) { this.sentDate = date; } /** * Gets the sent date for the email. * * @return date to be used as the sent date for the email * @since 1.0 */ public Date getSentDate() { if (this.sentDate == null) { return new Date(); } return this.sentDate; } /** * Gets the subject of the email. * * @return email subject */ public String getSubject() { return this.subject; } /** * Gets the sender of the email. * * @return from address */ public InternetAddress getFromAddress() { return this.fromAddress; } /** * Gets the host name of the SMTP server, * * @return host name */ public String getHostName() { if (EmailUtils.isNotEmpty(this.hostName)) { return this.hostName; } else { return this.session.getProperty(MAIL_HOST); } } /** * Gets the listening port of the SMTP server. * * @return smtp port */ public String getSmtpPort() { if (EmailUtils.isNotEmpty(this.smtpPort)) { return this.smtpPort; } else { return this.session.getProperty(MAIL_PORT); } } /** * Gets encryption mode for authentication * * @return true if using TLS for authentication, false otherwise * @since 1.1 */ public boolean isTLS() { return this.tls; } /** * Utility to copy List of known InternetAddress objects into an * array. * * @param list A List. * @return An InternetAddress[]. * @since 1.0 */ protected InternetAddress[] toInternetAddressArray(List list) { InternetAddress[] ia = (InternetAddress[]) list.toArray(new InternetAddress[list.size()]); return ia; } /** * Set details regarding "pop3 before smtp" authentication. * * @param newPopBeforeSmtp Wether or not to log into pop3 * server before sending mail. * @param newPopHost The pop3 host to use. * @param newPopUsername The pop3 username. * @param newPopPassword The pop3 password. * @since 1.0 */ public void setPopBeforeSmtp( boolean newPopBeforeSmtp, String newPopHost, String newPopUsername, String newPopPassword) { this.popBeforeSmtp = newPopBeforeSmtp; this.popHost = newPopHost; this.popUsername = newPopUsername; this.popPassword = newPopPassword; } /** * Returns whether SSL encryption for the transport is currently enabled. * @return true if SSL enabled for the transport */ public boolean isSSL() { return ssl; } /** * Sets whether SSL encryption should be enabled for the SMTP transport. * @param ssl whether to enable the SSL transport */ public void setSSL(boolean ssl) { this.ssl = ssl; } /** * Returns the current SSL port used by the SMTP transport. * @return the current SSL port used by the SMTP transport */ public String getSslSmtpPort() { if (EmailUtils.isNotEmpty(this.sslSmtpPort)) { return this.sslSmtpPort; } else { return this.session.getProperty(MAIL_SMTP_SOCKET_FACTORY_PORT); } } /** * Sets the SSL port to use for the SMTP transport. Defaults to the standard * port, 465. * @param sslSmtpPort the SSL port to use for the SMTP transport */ public void setSslSmtpPort(String sslSmtpPort) { this.sslSmtpPort = sslSmtpPort; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -