Class MailMessage

  • All Implemented Interfaces:
    EditableData, JcmsConstants, ImportConstants, MashupConstants, StrongLockable, Searchable, Storable, JaliosConstants, LangPropertyArgument, TreeNode, java.lang.Cloneable, java.lang.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 Detail

      • DB_MESSAGE_REPLYTO

        public static final java.lang.String DB_MESSAGE_REPLYTO
        See Also:
        Constant Field Values
      • DB_MESSAGE_INREPLYTO

        public static final java.lang.String DB_MESSAGE_INREPLYTO
        See Also:
        Constant Field Values
      • DB_MESSAGE_REFERENCES

        public static final java.lang.String DB_MESSAGE_REFERENCES
        See Also:
        Constant Field Values
      • DB_MESSAGE_SUBJECT

        public static final java.lang.String DB_MESSAGE_SUBJECT
        See Also:
        Constant Field Values
      • DB_MESSAGE_ACCOUNT

        public static final java.lang.String DB_MESSAGE_ACCOUNT
        See Also:
        Constant Field Values
      • DB_MESSAGE_THREAD

        public static final java.lang.String DB_MESSAGE_THREAD
        See Also:
        Constant Field Values
      • message

        protected transient javax.mail.Message message
      • receivedHeaders

        protected transient java.util.Map<java.lang.String,​java.lang.Object> receivedHeaders
      • multiparts

        protected transient java.util.Collection<MailMessage.Part> multiparts
      • fileMap

        protected transient java.util.Map<java.io.File,​java.lang.String> fileMap
      • duration

        protected transient long duration
    • Constructor Detail

      • MailMessage

        public MailMessage()
      • MailMessage

        public MailMessage​(Publication other)
      • MailMessage

        public MailMessage​(MailMessage other)
      • MailMessage

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

        public MailMessage​(java.lang.String origin)
      • MailMessage

        public MailMessage​(java.lang.String from,
                           java.lang.String to,
                           java.lang.String subject,
                           java.lang.String body)
    • Method Detail

      • getSubject

        public java.lang.String getSubject()
      • getOrigin

        public java.lang.String getOrigin()
      • getFrom

        public java.lang.String getFrom()
      • getTo

        public java.util.Set<java.lang.String> getTo()
      • getCc

        public java.util.Set<java.lang.String> getCc()
      • getBcc

        public java.util.Set<java.lang.String> getBcc()
      • getReplyTo

        public java.lang.String getReplyTo()
      • getFromMember

        public Member getFromMember()
      • getToMember

        public java.util.Set<Member> getToMember()
      • getToMember

        public java.util.Set<Member> getToMember​(boolean b)
      • getCcMember

        public java.util.Set<Member> getCcMember()
      • getCcMember

        public java.util.Set<Member> getCcMember​(boolean b)
      • getBccMember

        public java.util.Set<Member> getBccMember()
      • getBccMember

        public java.util.Set<Member> getBccMember​(boolean b)
      • getReplyToMember

        public Member getReplyToMember()
      • getSenderMember

        public Member getSenderMember()
      • getAttachements

        public java.util.Set<FileDocument> getAttachements()
      • getAttachements

        public java.util.Set<FileDocument> getAttachements​(boolean b)
      • getContentText

        public java.lang.String getContentText()
      • getContentHtml

        public java.lang.String getContentHtml()
      • getAccount

        public java.lang.String getAccount()
      • getPriority

        public int getPriority()
      • getSentDate

        public java.util.Date getSentDate()
      • getReceivedDate

        public java.util.Date getReceivedDate()
      • getExpires

        public java.util.Date getExpires()
      • getMessageId

        public java.lang.String getMessageId()
      • getInReplyTo

        public java.lang.String getInReplyTo()
      • getReferences

        public java.util.Set<java.lang.String> getReferences()
      • getThread

        public java.lang.String getThread()
      • getCustomBodyParts

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

        public void setSubject​(java.lang.String v)
      • setOrigin

        public void setOrigin​(java.lang.String origin)
      • setFrom

        public void setFrom​(java.lang.String v)
      • setTo

        public void setTo​(java.util.Set<java.lang.String> v)
      • setCc

        public void setCc​(java.util.Set<java.lang.String> v)
      • setBcc

        public void setBcc​(java.util.Set<java.lang.String> v)
      • setReplyTo

        public void setReplyTo​(java.lang.String v)
      • setFromMember

        public void setFromMember​(Member v)
      • setToMember

        public void setToMember​(java.util.Set<Member> v)
      • setCcMember

        public void setCcMember​(java.util.Set<Member> v)
      • setBccMember

        public void setBccMember​(java.util.Set<Member> v)
      • setReplyToMember

        public void setReplyToMember​(Member v)
      • setAttachements

        public void setAttachements​(java.util.Set<FileDocument> v)
      • setContentText

        public void setContentText​(java.lang.String v)
      • setContentHtml

        public void setContentHtml​(java.lang.String v)
      • setAccount

        public void setAccount​(java.lang.String v)
      • setPriority

        public void setPriority​(java.lang.Integer v)
      • setSentDate

        public void setSentDate​(java.util.Date v)
      • setReceivedDate

        public void setReceivedDate​(java.util.Date v)
      • setExpires

        public void setExpires​(java.util.Date v)
      • setMessageId

        public void setMessageId​(java.lang.String v)
      • setInReplyTo

        public void setInReplyTo​(java.lang.String v)
      • setReferences

        public void setReferences​(java.util.Set<java.lang.String> v)
      • setThread

        public void setThread​(java.lang.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​(java.lang.String v)
      • setCc

        public MailMessage setCc​(java.lang.String v)
      • setBcc

        public MailMessage setBcc​(java.lang.String v)
      • addTo

        public MailMessage addTo​(java.lang.String v)
      • addCc

        public MailMessage addCc​(java.lang.String v)
      • addBcc

        public MailMessage addBcc​(java.lang.String v)
      • getRecipient

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

        public java.util.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 java.lang.String getAbstract​(java.lang.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:
        Publication.getAbstract(String, boolean)
      • getMatchingRecipient

        public java.lang.String getMatchingRecipient​(java.lang.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 java.lang.String getMatchingRecipient​(java.util.regex.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​(java.lang.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​(java.util.Set<java.lang.String> address,
                                                                     java.util.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 java.util.Set<java.lang.String> getAddressSet​(javax.mail.Address[] ia)
        Returns a Set of Address.toString()
        Parameters:
        ia - an array of Address
        Returns:
        Set of String
      • getFirstAddress

        protected java.lang.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​(java.lang.String jspPath,
                                                 Member loggedMember,
                                                 java.lang.String language,
                                                 java.util.HashMap<java.lang.Object,​java.lang.Object> requestAttribute,
                                                 java.util.HashMap<java.lang.Object,​java.lang.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​(java.lang.String jspPath,
                                                 Member loggedMember,
                                                 java.lang.String language,
                                                 java.util.HashMap<java.lang.Object,​java.lang.Object> requestAttribute,
                                                 java.util.HashMap<java.lang.Object,​java.lang.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​(java.lang.String key,
                              java.lang.String value)
      • getHeader

        public java.lang.String getHeader​(java.lang.String key)
      • removeHeader

        public void removeHeader​(java.lang.String key)
      • getHeadersMap

        public java.util.Map<java.lang.String,​java.lang.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 java.util.Collection<MailMessage.Part> getTemporaryMultiparts()
        Returns flatten collection of MailMessage.Part with decoded Files and Contents
        Returns:
        MailMessage.Part
      • getTemporaryHeaders

        public java.util.Map<java.lang.String,​java.lang.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,
                                  java.io.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
        java.io.IOException - if an error occurs
      • performBeforeWrite

        protected void performBeforeWrite​(int op,
                                          Member mbr,
                                          java.util.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 java.util.Map<java.io.File,​java.lang.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​(java.io.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​(java.io.File file,
                                   java.lang.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​(java.io.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​(java.util.HashMap<java.lang.String,​java.lang.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​(java.util.HashMap<java.lang.String,​java.lang.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​(java.util.Collection<MailMessage> successMessages,
                                       java.util.Collection<MailMessage> errorMessages)
      • getMailMessageReplies

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

        public java.util.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