Class MailManager


  • public class MailManager
    extends java.lang.Object
    Provides convenient methods to manage DBMailMessage stored in DataBase and on HardDrive
    Version:
    $Revision: 135995 $
    Author:
    Jean-Philippe Encausse
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ADD_IMAGES_AUTHKEY
      Name of both JCMS property and context map attribute defining a MailMessage behavior.
      protected static java.util.Map<java.lang.String,​java.lang.String> mailHeaderMap  
      static java.lang.String REVISION  
      static java.lang.String SEND_WITH_SITE_EMAIL
      Name of both JCMS property and context map attribute defining a MailMessage behavior.
      static java.lang.String SET_AUTO_GENERATED
      Name of both JCMS property and context map attribute defining a MailMessage behavior.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void addAuthKeyToImagesSrc​(MailMessage msg, java.util.HashMap<java.lang.String,​java.lang.Object> ctxt)
      Rewrite the HTML content of the specified MailMessage to add an authentication key to all images URL.
      static java.lang.String addAuthKeyToImagesSrc​(java.lang.String content, Member mbr)
      Add an authentication key to all images URL in the specified HTML content.
      static void addDefaultHeaders​(MailMessage msg, java.util.HashMap<java.lang.String,​java.lang.Object> ctxt)
      An internal method used to add default mail headers configured in properties in the message.
      static java.lang.String buildContentFromJsp​(java.lang.String jspPath, Member loggedMember, java.lang.String language, java.util.HashMap<java.lang.Object,​java.lang.Object> requestAttributeMap, java.util.HashMap<java.lang.Object,​java.lang.Object> sessionAttributeMap)
      Builds a mail content from a given jsp
      MailMessage createDBMailMessage​(javax.mail.Message msg, MailAccount account)
      Build a DBMessage from a Message and save in into the DB
      MailMessage getDBMailMessage​(java.lang.String msgid)
      Returns the DBMailMessage for the given Message Id
      MailMessage getDBMailMessage​(javax.mail.Message msg)
      Returns the DBMailMessage for the given Message
      int getDBMailMessageCount()
      Returns the count of DBMailMessage
      int getDBMailMessageCount​(java.lang.String account)
      Returns the count of DBMailMessage for given account
      java.util.Collection<? extends MailMessage> getDBMailMessageReplies​(java.lang.String msgid)
      Returns the DBMailMessage for the given Message Id
      java.util.Set<java.lang.Object> getDBMailMessageSet​(java.lang.String[] ids)
      Returns a Collection of MailMessage or Message-ID (if not found)
      java.util.Collection<? extends MailMessage> getDBMailMessageThread​(java.lang.String thread)  
      static java.util.Set<javax.mail.internet.InternetAddress> getIAFromMemberSet​(java.util.Set<Member> mbrSet)
      Returns a Set of InternetAddress build from a Set of Members
      static java.lang.String getIncomingAddress​(java.lang.String localpart, Member mbr, java.lang.String label)
      Returns JCMS incoming email for the given Member
      static MailManager getInstance()  
      static java.util.TreeSet<java.lang.String> getInvalidEmailSet​(java.util.Set<java.lang.String> mailSet, java.util.Set<Member> mbrSet)
      Retrieve all INVALID email from the specified mail and member Set.
      static java.lang.String getLocalPartSuffixAuthKey​(Member mbr)
      Returns a localpart suffix authkey for given Member.
      MailFetcher getMailFetcher()
      Returns the MailFetcher
      java.lang.String getMessageIdQuietly​(javax.mail.Message msg)
      Convenient method that delegate to MailFetcher method
      static void improveMailMessageConformity​(MailMessage msg, java.util.HashMap<java.lang.String,​java.lang.Object> ctxt)
      Improve the specified MailMessage conformity with mail sending best practices (if enabled).
      void init()  
      static java.lang.String replaceRelativeUrlsWithAbsoluteUrls​(java.lang.String contentHtml)
      Convert all relative URLs inside the specified HTML content into absolute URL.
      static Member resolveAddress​(java.lang.String addr)
      Retrieves the Member from the given E-mail.
      static java.util.Set<Member> resolveAddress​(java.util.Set<java.lang.String> addressSet)
      Retrieves the set of Members given the set of E-mail addresses.
      static void resolveAddresses​(MailMessage mail)
      Updates the given mailMessage's addresses field.
      void saveDBMailMessage​(MailMessage mail, java.util.Map<java.lang.String,​java.lang.Object> context)
      Build a DBMessage from a Message and save in into the DB
      java.io.File writeEml​(javax.mail.Message msg)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SET_AUTO_GENERATED

        public static final java.lang.String SET_AUTO_GENERATED
        Name of both JCMS property and context map attribute defining a MailMessage behavior.

        The value is a boolean which defines if MailMessage should be sent using the header Auto-Submitted: auto-generated (RFC 3834 §5.2) to indicate mail was automatically generated.

        Value can be specified :

        • in JCMS properties (its default value is true)
        • in context map attribute of MailMessage.send(HashMap) method (this value overrides the behavior of JCMS property)
        Default JCMS behavior (following fix JCMS-9853) is to indicate mail was automatically generated.
        Since:
        JCMS-9853
        See Also:
        Constant Field Values
      • SEND_WITH_SITE_EMAIL

        public static final java.lang.String SEND_WITH_SITE_EMAIL
        Name of both JCMS property and context map attribute defining a MailMessage behavior.

        The value is a boolean which defines if MailMessage should be sent using the Member name and e-mail, or using the user name but with the site e-mail address.
        If set to true, mail are sent using the site default email address, and the original Member is used in the ReplyTo header.
        If set to false, mail are sent using original member email address.

        Value can be specified :

        • in JCMS properties (its default value is true)
        • in context map attribute of MailMessage.send(HashMap) method (this value overrides the behavior of JCMS property)
        Default JCMS behavior (following fix JCMS-3740) is to send mail using the site email address.
        Since:
        JCMS-3740
        See Also:
        Constant Field Values
      • mailHeaderMap

        protected static java.util.Map<java.lang.String,​java.lang.String> mailHeaderMap
      • ADD_IMAGES_AUTHKEY

        public static final java.lang.String ADD_IMAGES_AUTHKEY
        Name of both JCMS property and context map attribute defining a MailMessage behavior.

        The value is a boolean which defines if MailMessage should be modified to add authentication key to all images source URL to allow images to mailer without any authentication.
        If set to true, authkey are added to URL.
        If set to false, default behavior is applied.

        Value can be specified :

        • in JCMS properties (its default value is true)
        • in context map attribute of MailMessage.send(HashMap) method (this value overrides the behavior of JCMS property)
        Default JCMS behavior is to add autkey only when site is private.
        Since:
        JCMS-2677
        See Also:
        Constant Field Values
    • Method Detail

      • getInstance

        public static MailManager getInstance()
      • init

        public void init()
      • improveMailMessageConformity

        public static void improveMailMessageConformity​(MailMessage msg,
                                                        java.util.HashMap<java.lang.String,​java.lang.Object> ctxt)
        Improve the specified MailMessage conformity with mail sending best practices (if enabled).

        Implementation details

        Message is indicated as auto generated, through header Auto-Submitted: auto-generated (RFC 3834 §5.2).
        Since JCMS-9853, if following criterias are met :

        • property mail.set-auto-generated is set to true
        • attribute mail.set-auto-generated is either undefined or set to true in the context map (this attribute overrides behavior of JCMS property)

        Message is sent using site name/email, more precisely, a message sent following one Member action, is sent with a From header using the site name/email, and the original member name/email specified in From is used as Reply-To.
        Since JCMS-3740, and if following criterias are met :

        • property mail.send-with-site-email is set to true
        • attribute mail.send-with-site-email is either undefined or set to true in the context map (this attribute overrides behavior of JCMS property)
        • MailMessage.getFromMember() is not null
        • MailMessage.getReplyToMember() is null or is the same member specified in MailMessage.getFromMember()
        The MailMessage is rewriten to use the site e-mail address in the From header :
        • the FromMember is used as the ReplyToMember
        • the From header as "{Member Full Name} ({Site Name}) <{default-email or admin-email}>"
        • the FromMember is removed
        Parameters:
        msg - the MailMessage to modify if criterias are met
        ctxt - the context map attribute of MailMessage.send(HashMap), MUST NOT be null
        Since:
        JCMS-3740, updated with JCMS-9853
      • addDefaultHeaders

        public static void addDefaultHeaders​(MailMessage msg,
                                             java.util.HashMap<java.lang.String,​java.lang.Object> ctxt)
        An internal method used to add default mail headers configured in properties in the message.

        Mail headers are read from properties mail.header.{Header-Name}: {Header-Value}

        Parameters:
        msg - the MailMessage to modify
        ctxt - the context map attribute of MailMessage.send(HashMap), MUST NOT be null
        Since:
        jcms-10.0.7 / JCMS-9854
      • addAuthKeyToImagesSrc

        public static void addAuthKeyToImagesSrc​(MailMessage msg,
                                                 java.util.HashMap<java.lang.String,​java.lang.Object> ctxt)
        Rewrite the HTML content of the specified MailMessage to add an authentication key to all images URL.

        It allows mailer to access images without any authentication.

        Default behavior is to perform this operation only when the site is private.
        Since improvement JCMS-2677, the operation can be forced if :

        • property mail.add-images-authkey is set to true
        • attribute mail.add-images-authkey is set to true in the context map (this attribute overrides behavior of JCMS property)

        Authkeys are added only if the message is sent on one and only one Member.

        Parameters:
        msg - the MailMessage to modify if criterias are met
        ctxt - the context map attribute of MailMessage.send(HashMap), MUST NOT be null
        Since:
        JCMS-2677
      • addAuthKeyToImagesSrc

        public static java.lang.String addAuthKeyToImagesSrc​(java.lang.String content,
                                                             Member mbr)
        Add an authentication key to all images URL in the specified HTML content.

        All image URL in tag src='...' are modified to add an authkey.

        Parameters:
        content - html content to update
        mbr - the recipient member
        Returns:
        String the modified content
      • replaceRelativeUrlsWithAbsoluteUrls

        public static java.lang.String replaceRelativeUrlsWithAbsoluteUrls​(java.lang.String contentHtml)
        Convert all relative URLs inside the specified HTML content into absolute URL.

        Uses the current channel URL as prefix of relative URL.

        Parameters:
        contentHtml - the HTML content in which to look for relative URL (in src and href attributes)
        Returns:
        the modified content
      • getDBMailMessageSet

        public java.util.Set<java.lang.Object> getDBMailMessageSet​(java.lang.String[] ids)
        Returns a Collection of MailMessage or Message-ID (if not found)
        Parameters:
        ids - a list of Message-ID
        Returns:
        Collection of MailMessage or Message-ID
      • getDBMailMessageCount

        public int getDBMailMessageCount()
        Returns the count of DBMailMessage
        Returns:
        int the DBMailMessageCount
      • getDBMailMessageCount

        public int getDBMailMessageCount​(java.lang.String account)
        Returns the count of DBMailMessage for given account
        Parameters:
        account - the account name
        Returns:
        int the DBMailMessageCount
      • getDBMailMessage

        public MailMessage getDBMailMessage​(javax.mail.Message msg)
        Returns the DBMailMessage for the given Message
        Parameters:
        msg - a javax.mail.Message
        Returns:
        a DBMailMessage stored in db
      • getDBMailMessage

        public MailMessage getDBMailMessage​(java.lang.String msgid)
        Returns the DBMailMessage for the given Message Id
        Parameters:
        msgid - the message id
        Returns:
        a DBMailMessage stored in db
      • getDBMailMessageReplies

        public java.util.Collection<? extends MailMessage> getDBMailMessageReplies​(java.lang.String msgid)
        Returns the DBMailMessage for the given Message Id
        Parameters:
        msgid - the message id
        Returns:
        a DBMailMessage stored in db
      • getDBMailMessageThread

        public java.util.Collection<? extends MailMessage> getDBMailMessageThread​(java.lang.String thread)
      • createDBMailMessage

        public MailMessage createDBMailMessage​(javax.mail.Message msg,
                                               MailAccount account)
        Build a DBMessage from a Message and save in into the DB
        Parameters:
        msg - a javax.mail.Message
        account - the mailAccount
        Returns:
        MailMessage instanciate or null if exception appends
      • saveDBMailMessage

        public void saveDBMailMessage​(MailMessage mail,
                                      java.util.Map<java.lang.String,​java.lang.Object> context)
        Build a DBMessage from a Message and save in into the DB
        Parameters:
        mail - the MailMessage
        context - the contextual map
      • writeEml

        public java.io.File writeEml​(javax.mail.Message msg)
      • getMailFetcher

        public MailFetcher getMailFetcher()
        Returns the MailFetcher
        Returns:
        MailFetcher
      • getMessageIdQuietly

        public java.lang.String getMessageIdQuietly​(javax.mail.Message msg)
        Convenient method that delegate to MailFetcher method
        Parameters:
        msg - the javax.mail.Message
        Returns:
        String the message Id or null
        See Also:
        MailFetcher.getMessageIdQuietly(Message)
      • getIncomingAddress

        public static java.lang.String getIncomingAddress​(java.lang.String localpart,
                                                          Member mbr,
                                                          java.lang.String label)
        Returns JCMS incoming email for the given Member
        Parameters:
        localpart - the mail prefix
        mbr - Member author of the mail
        label - the email label
        Returns:
        String valid incoming email
      • getLocalPartSuffixAuthKey

        public static java.lang.String getLocalPartSuffixAuthKey​(Member mbr)
        Returns a localpart suffix authkey for given Member.
        Parameters:
        mbr - the member
        Returns:
        String localpart sufix authkey (starting with +)
      • getIAFromMemberSet

        public static java.util.Set<javax.mail.internet.InternetAddress> getIAFromMemberSet​(java.util.Set<Member> mbrSet)
                                                                                     throws javax.mail.MessagingException
        Returns a Set of InternetAddress build from a Set of Members
        Parameters:
        mbrSet - a Set of Members
        Returns:
        Set of InternetAddress
        Throws:
        javax.mail.MessagingException - a base class for all exceptions thrown by the Messaging classes
      • getInvalidEmailSet

        public static java.util.TreeSet<java.lang.String> getInvalidEmailSet​(java.util.Set<java.lang.String> mailSet,
                                                                             java.util.Set<Member> mbrSet)
        Retrieve all INVALID email from the specified mail and member Set.
        Parameters:
        mailSet - a set of email addresses
        mbrSet - a set of Member
        Returns:
        a set of invalid email
        Since:
        jcms-6.1
      • buildContentFromJsp

        public static java.lang.String buildContentFromJsp​(java.lang.String jspPath,
                                                           Member loggedMember,
                                                           java.lang.String language,
                                                           java.util.HashMap<java.lang.Object,​java.lang.Object> requestAttributeMap,
                                                           java.util.HashMap<java.lang.Object,​java.lang.Object> sessionAttributeMap)
        Builds a mail content from a given jsp
        Parameters:
        jspPath - the jsp to process
        loggedMember - the loggedMember
        language - the user language
        requestAttributeMap - Map of request attributes
        sessionAttributeMap - Map of session attributes
        Returns:
        String builded content
      • resolveAddresses

        public static void resolveAddresses​(MailMessage mail)
        Updates the given mailMessage's addresses field.

        String addresses will be resolved to Members if possible.

        optional already set Members will be kept.

        Parameters:
        mail - the mail to resolved String email to Members.
        Since:
        jcms-9.0
      • resolveAddress

        public static Member resolveAddress​(java.lang.String addr)
        Retrieves the Member from the given E-mail.
        Parameters:
        addr - an email address that will be parsed using MailUtil.getInternetAddress(String)
        Returns:
        the Member from E-mail if any
        Since:
        jcms-9.0
      • resolveAddress

        public static java.util.Set<Member> resolveAddress​(java.util.Set<java.lang.String> addressSet)
        Retrieves the set of Members given the set of E-mail addresses.

        Retrieved Member's E-mail will be removed from addressSet when resolved.

        Parameters:
        addressSet - the set of E-mails to retrieve their related Member if any
        Returns:
        the Set of Member
        Since:
        jcms-9.0