Class EmailSrv

java.lang.Object
org.compiere.util.EmailSrv

public class EmailSrv extends Object
Provide function for sent, receive email in imap protocol.
Current only support receive email, for sent email, use EMail instead.
In case internet line is slow, handling error during analysis of message by fetching message in part can have complication.
Consider to add flag to fetch all message at one time (with retry when error) and after fetching, analysis fetched message offline. http://www.oracle.com/technetwork/java/javamail/faq/index.html#imapserverbug
Author:
hieplq base in RequestEMailProcessor
  • Field Details

    • log

      protected static transient CLogger log
    • imapHost

      protected String imapHost
    • imapUser

      protected String imapUser
    • imapPass

      protected String imapPass
    • imapPort

      protected int imapPort
    • isSSL

      protected boolean isSSL
    • mailSession

      protected javax.mail.Session mailSession
    • mailStore

      protected javax.mail.Store mailStore
  • Constructor Details

    • EmailSrv

      public EmailSrv(String imapHost, String imapUser, String imapPass, int imapPort, Boolean isSSL)
      Parameters:
      imapHost -
      imapUser -
      imapPass -
      imapPort -
      isSSL -
    • EmailSrv

      public EmailSrv(String imapHost, String imapUser, String imapPass)
      Deprecated.
      working only with gmail host.
      Parameters:
      imapHost -
      imapUser -
      imapPass -
  • Method Details

    • logMailPartInfo

      public static void logMailPartInfo(javax.mail.Part msg, CLogger log) throws javax.mail.MessagingException
      Log msg info with INFO log level.
      Parameters:
      msg -
      log -
      Throws:
      javax.mail.MessagingException
    • getMailSession

      protected javax.mail.Session getMailSession() throws Exception
      Get mail session
      Returns:
      mail session
      Throws:
      Exception
    • getMailStore

      public javax.mail.Store getMailStore() throws Exception
      Get mail store
      Returns:
      mail store
      Throws:
      Exception
    • clearResource

      public void clearResource()
      Close mail store
    • getFolder

      public static javax.mail.Folder getFolder(javax.mail.Store mailStore, String folderName, Boolean isNestInbox, boolean createWhenNonExists) throws javax.mail.MessagingException
      Open a mail store folder in read/write mode.
      Parameters:
      mailStore -
      folderName - open nest folder by use format folder1/folder2/folder3
      isNestInbox - in case true, open folder start from default inbox, other open from root folder
      createWhenNonExists - in case true, create folder by hierarchy if not exists, other not exists will make exception
      Returns:
      folder opened in r/w model
      Throws:
      javax.mail.MessagingException
    • readEmailFolder

      public static boolean readEmailFolder(EmailSrv emailSrv, String folderName, Boolean isNestInbox, EmailSrv.ProcessEmailHandle processEmailHandle)
      Read an email folder, with each email inject object processEmail for processing.
      In case error, close folder or close session (by disconnect) with retry of 3 times.
      When error with 5 continue message, stop process.
      Parameters:
      emailSrv -
      folderName - folder name can hierarchy by use "\"
      isNestInbox - true in case start folder from inbox
      processEmailHandle -
      Returns:
      true if success
    • processMessage

      public static EmailSrv.EmailContent processMessage(javax.mail.Message msg) throws javax.mail.MessagingException, IOException
      Parameters:
      msg -
      Returns:
      Throws:
      javax.mail.MessagingException
      IOException
      See Also:
    • processMessage

      public static EmailSrv.EmailContent processMessage(javax.mail.Message msg, EmailSrv.ProcessEmailHandle evaluateEmailHead, javax.mail.Store mailStore, javax.mail.Folder mailFolder) throws javax.mail.MessagingException, IOException
      Process message
      Parameters:
      msg -
      evaluateEmailHead -
      Returns:
      return EmailInfo contain info of email, in case evaluateEmailHead make cancel, return null
      Throws:
      javax.mail.MessagingException
      IOException
    • analysisEmailStructure

      public static void analysisEmailStructure(javax.mail.Part msg, EmailSrv.EmailContent emailContent) throws javax.mail.MessagingException, IOException
      Parameters:
      msg -
      emailContent -
      Throws:
      javax.mail.MessagingException
      IOException
      See Also:
    • analysisEmailStructure

      public static void analysisEmailStructure(javax.mail.Part msg, EmailSrv.EmailContent emailContent, boolean isRoot) throws javax.mail.MessagingException, IOException
      Analysis Part object.
      Get content in plan or html text.
      Detect type of attached file and put it in to EmailSrv.EmailContent for later processing.
      Parameters:
      msg - mime part to analysis
      emailContent - object contain result analysis
      isRoot - true when part is Message
      Throws:
      javax.mail.MessagingException
      IOException
    • getTextFromMailPart

      public static String getTextFromMailPart(javax.mail.Part txtPart) throws javax.mail.MessagingException, IOException
      http://www.oracle.com/technetwork/java/javamail/faq/index.html#unsupen
      Parameters:
      txtPart -
      Returns:
      Throws:
      javax.mail.MessagingException
      IOException
    • getBinaryData

      public static byte[] getBinaryData(javax.mail.Part binaryPart) throws IOException, javax.mail.MessagingException
      Read binary attachment from a multi-part
      Parameters:
      binaryPart -
      Returns:
      Throws:
      IOException
      javax.mail.MessagingException
    • getBinaryAsBASE64

      public static String getBinaryAsBASE64(javax.mail.BodyPart mailPart) throws IOException, javax.mail.MessagingException
      Download attached file and convert to base64 encoding
      Parameters:
      mailPart -
      Returns:
      base64 encoded content
      Throws:
      IOException
      javax.mail.MessagingException
    • embedImgToEmail

      public static String embedImgToEmail(String mailContent, EmailSrv.ProvideBase64Data provideBase64Data) throws Exception
      embedImgToEmail(String, ProvideBase64Data, String) use default pattern for embed image is "\\s+src\\s*=\\s*\"cid:(.*?)\"");
      Parameters:
      mailContent -
      provideBase64Data -
      Returns:
      Throws:
      Exception
    • embedImgToEmail

      public static String embedImgToEmail(String mailContent, EmailSrv.ProvideBase64Data provideBase64Data, String embedPattern) throws javax.mail.MessagingException, IOException
      Find in mailContent pattern of embedded image.
      For each of them, replace cid by base64 data.
      Preview in cfEditor pattern is "\\s+src\\s*=\\s*\"cid:(.*?)\""
      With embedded image in gmail, pattern is "\\s+src\\s*=\\s*3D\\s*\"cid:(.*?)\""
      with embedded image in other server (nmicoud), pattern is "\\s+src\\s*=\\s*\"cid:(.*?)\""
      REMEMBER:cid:(.*?) must in group 1
      Parameters:
      mailContent -
      provideBase64Data -
      embedPattern -
      Returns:
      Throws:
      Exception
      javax.mail.MessagingException
      IOException
    • getEmbededImages

      public static ArrayList<javax.mail.BodyPart> getEmbededImages(String mailContent, EmailSrv.ProvideBase64Data provideBase64Data, String embedPattern) throws javax.mail.MessagingException, IOException
      Get embedded images
      Parameters:
      mailContent -
      provideBase64Data -
      embedPattern -
      Returns:
      list of embedded image part
      Throws:
      javax.mail.MessagingException
      IOException
    • isBinaryPart

      public static boolean isBinaryPart(javax.mail.Part binaryPart) throws javax.mail.MessagingException
      Is binaryPart a binary Part
      Parameters:
      binaryPart -
      Returns:
      true if it is a binary part
      Throws:
      javax.mail.MessagingException
    • getContentID

      public static String getContentID(javax.mail.Part attachPart) throws javax.mail.MessagingException
      Get contentID from header, with each inline attachment, will have a contentID value. In case value at contentID difference from value at X-Attachment-Id, must manual recheck to add process.
      Parameters:
      attachPart -
      Returns:
      Throws:
      javax.mail.MessagingException
    • getPartHeader

      public static String[] getPartHeader(javax.mail.Part msg, String headerName) throws javax.mail.MessagingException
      Get part headers
      Parameters:
      msg -
      headerName -
      Returns:
      Throws:
      javax.mail.MessagingException