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 refer EMail.
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)
    • 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
      Throws:
      javax.mail.MessagingException
    • getMailSession

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

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

      public void clearResource()
    • getFolder

      public static javax.mail.Folder getFolder(javax.mail.Store mailStore, String folderName, Boolean isNestInbox, boolean createWhenNonExists) throws javax.mail.MessagingException
      open a 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 to processing in case error close folder or close session (by disconnect) with retry 3 times when error with 5 continue message, with stop process
      Parameters:
      emailSrv -
      folderName - folder name can hierarchy by use "\"
      isNestInbox - true in case start folder from inbox
      processEmailHandle -
      Returns:
    • 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
      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 attach file and put in to EmailSrv.EmailContent for late process
      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 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 attach file and convert to base64 encoding
      Parameters:
      mailPart -
      Returns:
      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 every pattern of embed image with each replace cid by base64 data. preview in cfEditor pattern is "\\s+src\\s*=\\s*\"cid:(.*?)\"" with embed image in gmail, pattern is "\\s+src\\s*=\\s*3D\\s*\"cid:(.*?)\"" with embed 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
      Throws:
      javax.mail.MessagingException
      IOException
    • isBinaryPart

      public static boolean isBinaryPart(javax.mail.Part binaryPart) throws javax.mail.MessagingException
      Throws:
      javax.mail.MessagingException
    • getContentID

      public static String getContentID(javax.mail.Part attachPart) throws javax.mail.MessagingException
      get contentID from header, with each inline attach, will have a contentID value in case value at contentID difference 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
      Throws:
      javax.mail.MessagingException