Class MailMessage

All Implemented Interfaces:
EditableData, JcmsConstants, ImportConstants, MashupConstants, StrongLockable, Searchable, Storable, JaliosConstants, LangPropertyArgument, TreeNode, Cloneable, Comparable<BasicStorable>

public class MailMessage extends Content
Class used to build and send a mail message.

Example :

   MailMessage msg = new MailMessage("myplugin.log-mail");
   msg.setTo(channel.getDefaultAdmin());
   msg.setSubject("[" + channel.getName() + "] Daily log");
   msg.setContentText("Daily log attached");
   msg.addFile(new File(channel.getRealPath("WEB-INF/data/logs/jcms.log")));
   msg.send();
   logger.info("Log mail sent in " + DateUtil.formatDuration(msg.getDuration()));
 
   MailMessage msg = new MailMessage("starwarsplugin.war-mail-alert");
   msg.setFrom("Darth Vader <dv@deathstar.com>");
   msg.setTo("Son <luke@alliance.com>");
   msg.setBcc("Palpatine <palpatine@deathstar.com>");
   for (Member sithMember : sithGroup.getMemberSet()) {   
      msg.addCc(sithMember);
   }
   msg.setSubject("I am your father");
   msg.setContentText("The Force is with you, young Skywalker. But you are not a Jedi yet.");
   msg.setContentHtml("The <strong>Force</strong> is with you, young Skywalker. But you are not a Jedi yet.");
   msg.send();
 
Since:
jcms-6.0.1
  • Field Details

  • Constructor Details

    • MailMessage

      public MailMessage()
    • MailMessage

      public MailMessage(Publication other)
    • MailMessage

      public MailMessage(MailMessage other)
    • MailMessage

      public MailMessage(javax.mail.Message msg) throws IOException, javax.mail.MessagingException
      Throws:
      IOException
      javax.mail.MessagingException
    • MailMessage

      public MailMessage(String origin)
    • MailMessage

      public MailMessage(String from, String to, String subject, String body)
  • Method Details

    • getSubject

      public String getSubject()
    • getOrigin

      public String getOrigin()
    • getFrom

      public String getFrom()
    • getTo

      public Set<String> getTo()
    • getCc

      public Set<String> getCc()
    • getBcc

      public Set<String> getBcc()
    • getReplyTo

      public String getReplyTo()
    • getFromMember

      public Member getFromMember()
    • getToMember

      public Set<Member> getToMember()
    • getToMember

      public Set<Member> getToMember(boolean b)
    • getCcMember

      public Set<Member> getCcMember()
    • getCcMember

      public Set<Member> getCcMember(boolean b)
    • getBccMember

      public Set<Member> getBccMember()
    • getBccMember

      public Set<Member> getBccMember(boolean b)
    • getReplyToMember

      public Member getReplyToMember()
    • getSenderMember

      public Member getSenderMember()
    • getAttachements

      public Set<FileDocument> getAttachements()
    • getAttachements

      public Set<FileDocument> getAttachements(boolean b)
    • getContentText

      public String getContentText()
    • getContentHtml

      public String getContentHtml()
    • getAccount

      public String getAccount()
    • getPriority

      public int getPriority()
    • getSentDate

      public Date getSentDate()
    • getReceivedDate

      public Date getReceivedDate()
    • getExpires

      public Date getExpires()
    • getMessageId

      public String getMessageId()
    • getInReplyTo

      public String getInReplyTo()
    • getReferences

      public Set<String> getReferences()
    • getThread

      public String getThread()
    • getCustomBodyParts

      public javax.mail.BodyPart[] getCustomBodyParts()
    • setSubject

      public void setSubject(String v)
    • setOrigin

      public void setOrigin(String origin)
    • setFrom

      public void setFrom(String v)
    • setTo

      public void setTo(Set<String> v)
    • setCc

      public void setCc(Set<String> v)
    • setBcc

      public void setBcc(Set<String> v)
    • setReplyTo

      public void setReplyTo(String v)
    • setFromMember

      public void setFromMember(Member v)
    • setToMember

      public void setToMember(Set<Member> v)
    • setCcMember

      public void setCcMember(Set<Member> v)
    • setBccMember

      public void setBccMember(Set<Member> v)
    • setReplyToMember

      public void setReplyToMember(Member v)
    • setAttachements

      public void setAttachements(Set<FileDocument> v)
    • setContentText

      public void setContentText(String v)
    • setContentHtml

      public void setContentHtml(String v)
    • setAccount

      public void setAccount(String v)
    • setPriority

      public void setPriority(Integer v)
    • setSentDate

      public void setSentDate(Date v)
    • setReceivedDate

      public void setReceivedDate(Date v)
    • setExpires

      public void setExpires(Date v)
    • setMessageId

      public void setMessageId(String v)
    • setInReplyTo

      public void setInReplyTo(String v)
    • setReferences

      public void setReferences(Set<String> v)
    • setThread

      public void setThread(String v)
    • setCustomBodyParts

      public void setCustomBodyParts(javax.mail.BodyPart[] v)
    • setSendOneMailPerRecipient

      public void setSendOneMailPerRecipient(boolean sendOneMailPerRecipient)
      Set sending mode, one mail for all recipients (false), or one mail for each recipients (true).

      Default is false, ie send one mail for all.

      Parameters:
      sendOneMailPerRecipient - set to true to send one mail to each recipient, false to send one mail for all.
      Since:
      jcms-6.1
    • setSendAcknowledge

      public void setSendAcknowledge(boolean sendAcknowledge)
      Set whether acknowledge message should be sent at end of process. Default is false, ie no acknowledge is sent.
      Parameters:
      sendAcknowledge - the acknowledge message
      Since:
      jcms-6.1
    • getFromIA

      public javax.mail.internet.InternetAddress[] getFromIA()
    • getToIA

      public javax.mail.internet.InternetAddress[] getToIA()
    • getCcIA

      public javax.mail.internet.InternetAddress[] getCcIA()
    • getBccIA

      public javax.mail.internet.InternetAddress[] getBccIA()
    • getReplyToIA

      public javax.mail.internet.InternetAddress[] getReplyToIA()
    • setTo

      public MailMessage setTo(String v)
    • setCc

      public MailMessage setCc(String v)
    • setBcc

      public MailMessage setBcc(String v)
    • setFrom

      public MailMessage setFrom(Member v)
    • setTo

      public MailMessage setTo(Member v)
    • setCc

      public MailMessage setCc(Member v)
    • setBcc

      public MailMessage setBcc(Member v)
    • setReplyTo

      public MailMessage setReplyTo(Member v)
    • addTo

      public MailMessage addTo(String v)
    • addCc

      public MailMessage addCc(String v)
    • addBcc

      public MailMessage addBcc(String v)
    • addTo

      public MailMessage addTo(Member v)
    • addCc

      public MailMessage addCc(Member v)
    • addBcc

      public MailMessage addBcc(Member v)
    • addTo

      public MailMessage addTo(Set<Member> v)
    • addCc

      public MailMessage addCc(Set<Member> v)
    • addBcc

      public MailMessage addBcc(Set<Member> v)
    • addAttachements

      public MailMessage addAttachements(FileDocument v)
    • addAttachements

      public MailMessage addAttachements(Set<FileDocument> v)
    • getRecipient

      public Set<String> getRecipient()
      Returns:
      E-mails union of To, Cc, and Bcc fields.
    • getRecipientMember

      public Set<Member> getRecipientMember()
      Returns:
      Members union of To, Cc, and Bcc fields.
    • getSendOneMailPerRecipient

      public boolean getSendOneMailPerRecipient()
      Get sending mode, one mail for all recipients (false), or one mail for each recipients (true).

      Default is false, ie send one mail for all.

      Returns:
      true to send one mail to each recipient, false to send one mail for all.
      Since:
      jcms-6.1
    • getSendAcknowledge

      public boolean getSendAcknowledge()
      Get whether acknowledge message should be sent at end of process. Default is false, ie no acknowledge is sent
      Returns:
      true if acknowledge should be sent, false otherwise.
      Since:
      jcms-6.1
    • getAbstract

      public String getAbstract(String lang, boolean useDefault)
      Returns an empty String.
      Overrides:
      getAbstract in class Publication
      Parameters:
      lang - the user language (ISO-639 language code)
      useDefault - should the default language be used
      Returns:
      an empty String
      Since:
      jcms-9.0
      See Also:
    • getMatchingRecipient

      public String getMatchingRecipient(String address)
      Returns the address matching (indexOf) the given address in all recipient and members
      Parameters:
      address - to match
      Returns:
      String the matching address or null
    • getMatchingRecipient

      public String getMatchingRecipient(Pattern pattern)
      Returns the address matching the given pattern in all recipient and members
      Parameters:
      pattern - to match
      Returns:
      String email
    • getFirstFromIA

      public javax.mail.internet.InternetAddress getFirstFromIA()
      Resolve "From" from all froms and Member's from
      Returns:
      InternetAddress from email
    • getFirstIA

      protected javax.mail.internet.InternetAddress getFirstIA(String email, Member mbr)
    • getSingleRecipient

      public Member getSingleRecipient()
      Returns the single Member recipients of the message or null if there is multiple recipients
      Returns:
      Member the recipients
    • getIAQuietly

      protected javax.mail.internet.InternetAddress[] getIAQuietly(Set<String> address, Set<Member> addressMember)
      Returns an array of InternetAddress joins of addresses and Member's addresses
      Parameters:
      address - Set of email address
      addressMember - Set of member
      Returns:
      Array of InternetAddress
    • getAddressSet

      protected Set<String> getAddressSet(javax.mail.Address[] ia)
      Returns a Set of Address.toString()
      Parameters:
      ia - an array of Address
      Returns:
      Set of String
    • getFirstAddress

      protected String getFirstAddress(javax.mail.Address[] ia)
      Returns the first Address.toString()
      Parameters:
      ia - an array of Address
      Returns:
      String the first address
    • setContentHtmlFromJsp

      public MailMessage setContentHtmlFromJsp(String jspPath, Member loggedMember, String language, HashMap<Object,Object> requestAttribute, HashMap<Object,Object> sessionAttribute)
      Set the HTML text content of this MailMessage from the rendering of the specified jsp.
      Parameters:
      jspPath - a jsp path relative to the webapp root e.g "/custom/jcms/doNotificationText.jsp"
      loggedMember - the Member that will be set as the logged member when invoking jsp
      language - the language that will be set when invoking jsp
      requestAttribute - a HashMap of request attribute available to the jsp execution
      sessionAttribute - a HashMap of session attribute available to the jsp execution
      Returns:
      this (for method chaining)
    • setContentTextFromJsp

      public MailMessage setContentTextFromJsp(String jspPath, Member loggedMember, String language, HashMap<Object,Object> requestAttribute, HashMap<Object,Object> sessionAttribute)
      Set the plain text content of this MailMessage from the rendering of the specified jsp.
      Parameters:
      jspPath - a jsp path relative to the webapp root e.g "/custom/jcms/doNotificationText.jsp"
      loggedMember - the Member that will be set as the logged member when invoking jsp
      language - the language that will be set when invoking jsp
      requestAttribute - a HashMap of request attribute available to the jsp execution
      sessionAttribute - a HashMap of session attribute available to the jsp execution
      Returns:
      this (for method chaining)
    • addHeader

      public void addHeader(String key, String value)
    • getHeader

      public String getHeader(String key)
    • removeHeader

      public void removeHeader(String key)
    • getHeadersMap

      public Map<String,String> getHeadersMap()
    • getTemporaryMessage

      public javax.mail.Message getTemporaryMessage()
      Returns temporary javax.mail.Message store while fetching mail
      • Message might be null.
      • Message fields calls can throw Exception if MailBox is closed. Try/Catch is strongly recommended.
      Returns:
      Message
    • getTemporaryMultiparts

      public Collection<MailMessage.Part> getTemporaryMultiparts()
      Returns flatten collection of MailMessage.Part with decoded Files and Contents
      Returns:
      MailMessage.Part
    • getTemporaryHeaders

      public Map<String,Object> getTemporaryHeaders()
      Returns a temporary Map of all mail's header.
      Returns:
      Map of String/Object
    • setMessage

      protected void setMessage(javax.mail.Message msg) throws javax.mail.MessagingException, IOException
      Set fields for the given Message and download attachements in temporary folder Caller should also set:
      • Account setAccount()
      • Author setAuthor()
      Parameters:
      msg - the message
      Throws:
      javax.mail.MessagingException - if an error occurs
      IOException - if an error occurs
    • performBeforeWrite

      protected void performBeforeWrite(int op, Member mbr, Map context)
      Description copied from class: Data
      Called to perform some action before the write.
      Overrides:
      performBeforeWrite in class Publication
      Parameters:
      op - the operation (OP_CREATE, OP_UPDATE, ...)
      mbr - the member which requests the write operation
      context - a map which contains context parameters (may be null)
    • getFileMap

      public Map<File,String> getFileMap()
      Retrieve the Map of File/id to send in this MailMessage. The map is transient and never stored in the CMS
      Returns:
      a Map with File as the key and an optional id as the value (may return null if no file was added yet).
    • addFile

      public MailMessage addFile(File file)
      Add a file to send with this MailMessage. The file never stored in the CMS
      Parameters:
      file - a File to send.
      Returns:
      this (for method chaining)
    • addFile

      public MailMessage addFile(File file, String id)
      Add a file to send with this MailMessage. The file never stored in the CMS
      Parameters:
      file - a File to send.
      id - an optional identifier for this file
      Returns:
      this (for method chaining)
    • getDataHandler

      protected javax.activation.DataHandler getDataHandler(File file) throws javax.mail.MessagingException
      Compute the correct handler to manage this file
      Parameters:
      file - the File to add
      Returns:
      the correct DataHandler
      Throws:
      javax.mail.MessagingException - if any exception occurs
    • getDuration

      public long getDuration()
      Retrieve the time the sending process took to execute.
      Returns:
      a duration in millisecond (0 if sending did not occured yet)
    • send

      public void send() throws javax.mail.MessagingException
      Send this MailMessage.
      Throws:
      javax.mail.MessagingException - if an error occurs
    • send

      public void send(HashMap<String,Object> ctxt) throws javax.mail.MessagingException
      Send this MailMessage.
      Parameters:
      ctxt - an optional context used to store information for this mail sending (context may be used by MailPolicyFilters in beforeSendMail and afterSensMail)
      Throws:
      javax.mail.MessagingException - if an error occurs
      Since:
      jcms-6.1
    • sendInThread

      public void sendInThread()
      Send this MailMessage in a seperate thread.
      Since:
      jcms-6.1
    • sendInThread

      public void sendInThread(HashMap<String,Object> ctxt)
      Send this MailMessage in a seperate thread.
      Parameters:
      ctxt - an optional context used to store information for this mail sending (context may be used by MailPolicyFilters in beforeSendMail and afterSensMail)
      Since:
      jcms-6.1
    • sendAcknowledge

      protected void sendAcknowledge(Collection<MailMessage> successMessages, Collection<MailMessage> errorMessages)
    • getTreeParent

      public TreeNode getTreeParent()
      Description copied from interface: TreeNode
      Returns parent of the current node
      Specified by:
      getTreeParent in interface TreeNode
      Overrides:
      getTreeParent in class Publication
      Returns:
      TreeNode parent Node
      See Also:
    • getTreeChildren

      public Collection<? extends TreeNode> getTreeChildren()
      Description copied from interface: TreeNode
      Returns a Collection of TreeNode children of the current node
      Specified by:
      getTreeChildren in interface TreeNode
      Overrides:
      getTreeChildren in class Publication
      Returns:
      Collection of TreeNode
      See Also:
    • getMailMessageReplies

      public Collection<? extends MailMessage> getMailMessageReplies()
      Returns a Collection of MailMessage replies of the current MailMessage
      Returns:
      Collection of MailMessage
    • getMailMessageThread

      public Collection<? extends MailMessage> getMailMessageThread()
      Returns a Collection of MailMessage, thread of the current discussion in ascending order (oldest date first).
      • Default: Use inReplyTo field
      • Use thread field if provided
      Returns:
      Collection of MailMessage
    • checkAuthKey

      public boolean checkAuthKey()
      Returns true if the authkey contained in the localpart of the "to" field is consistent with the "from" field.
      Returns:
      true if the authkey contained in the localpart of the "to" field is consistent with the "from" field.
      Since:
      jcms-8.0.0