Package org.compiere.util
Class EmailSrv
java.lang.Object
org.compiere.util.EmailSrv
Provide function for sent, receive email in imap protocol.
Current only support receive email, for sent email, use
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
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
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Manipulate fromMessage
Separate attached file to embed, attach, un-know list.static class
../../.. this class implement source of image from attachment of emailstatic interface
This class inject to email reading process (processMessage(Message, ProcessEmailHandle, Store, Folder)
)static interface
When process an email content, sometimes we wish to embed image as base64 string to mail. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
analysisEmailStructure
(javax.mail.Part msg, EmailSrv.EmailContent emailContent) static void
analysisEmailStructure
(javax.mail.Part msg, EmailSrv.EmailContent emailContent, boolean isRoot) AnalysisPart
object.
Get content in plan or html text.
Detect type of attached file and put it in toEmailSrv.EmailContent
for later processing.void
Close mail storestatic String
embedImgToEmail
(String mailContent, EmailSrv.ProvideBase64Data provideBase64Data) embedImgToEmail(String, ProvideBase64Data, String)
use default pattern for embed image is "\\s+src\\s*=\\s*\"cid:(.*?)static String
embedImgToEmail
(String mailContent, EmailSrv.ProvideBase64Data provideBase64Data, String embedPattern) 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:(.*?)static String
getBinaryAsBASE64
(javax.mail.BodyPart mailPart) Download attached file and convert to base64 encodingstatic byte[]
getBinaryData
(javax.mail.Part binaryPart) Read binary attachment from a multi-partstatic String
getContentID
(javax.mail.Part attachPart) Get contentID from header, with each inline attachment, will have a contentID value.static ArrayList<javax.mail.BodyPart>
getEmbededImages
(String mailContent, EmailSrv.ProvideBase64Data provideBase64Data, String embedPattern) Get embedded imagesstatic javax.mail.Folder
getFolder
(javax.mail.Store mailStore, String folderName, Boolean isNestInbox, boolean createWhenNonExists) Open a mail store folder in read/write mode.protected javax.mail.Session
Get mail sessionjavax.mail.Store
Get mail storestatic String[]
getPartHeader
(javax.mail.Part msg, String headerName) Get part headersstatic String
getTextFromMailPart
(javax.mail.Part txtPart) http://www.oracle.com/technetwork/java/javamail/faq/index.html#unsupenstatic boolean
isBinaryPart
(javax.mail.Part binaryPart) Is binaryPart a binary Partstatic void
logMailPartInfo
(javax.mail.Part msg, CLogger log) Log msg info with INFO log level.static EmailSrv.EmailContent
processMessage
(javax.mail.Message msg) static EmailSrv.EmailContent
processMessage
(javax.mail.Message msg, EmailSrv.ProcessEmailHandle evaluateEmailHead, javax.mail.Store mailStore, javax.mail.Folder mailFolder) Process messagestatic 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.
-
Field Details
-
log
-
imapHost
-
imapUser
-
imapPass
-
imapPort
protected int imapPort -
isSSL
protected boolean isSSL -
mailSession
protected javax.mail.Session mailSession -
mailStore
protected javax.mail.Store mailStore
-
-
Constructor Details
-
EmailSrv
- Parameters:
imapHost
-imapUser
-imapPass
-imapPort
-isSSL
-
-
EmailSrv
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
Get mail session- Returns:
- mail session
- Throws:
Exception
-
getMailStore
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/folder3isNestInbox
- in case true, open folder start from default inbox, other open from root foldercreateWhenNonExists
- 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 inboxprocessEmailHandle
-- 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 AnalysisPart
object.
Get content in plan or html text.
Detect type of attached file and put it in toEmailSrv.EmailContent
for later processing.- Parameters:
msg
- mime part to analysisemailContent
- object contain result analysisisRoot
- true when part isMessage
- 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
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
-