Package org.compiere.util
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 referEMail
in case internet line is slow, handle error when analysis message by fetch message part when need can complicate. consider to add flag fetch all message at one time (with retry when error). after that, analysis offline message. http://www.oracle.com/technetwork/java/javamail/faq/index.html#imapserverbug- Author:
- hieplq base in RequestEMailProcessor
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
EmailSrv.EmailContent
manipulate fromMessage
separate attach file to embed, attach, un-know liststatic class
EmailSrv.EmailEmbedProvideBase64Data
../../.. this class implement source of image from attach of emailstatic interface
EmailSrv.ProcessEmailHandle
this class inject to email reading process of functionprocessMessage(Message, ProcessEmailHandle, Store, Folder)
static interface
EmailSrv.ProvideBase64Data
when process an email content sometimes we wish embed image as base64 string to mail.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static 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.void
clearResource()
static 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 every pattern of embed image with each replace cid by base64 data.static String
getBinaryAsBASE64(javax.mail.BodyPart mailPart)
download attach file and convert to base64 encodingstatic byte[]
getBinaryData(javax.mail.Part binaryPart)
read binary from a multi-partstatic String
getContentID(javax.mail.Part attachPart)
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 processstatic ArrayList<javax.mail.BodyPart>
getEmbededImages(String mailContent, EmailSrv.ProvideBase64Data provideBase64Data, String embedPattern)
static javax.mail.Folder
getFolder(javax.mail.Store mailStore, String folderName, Boolean isNestInbox, boolean createWhenNonExists)
open a folder in read/write mode.protected javax.mail.Session
getMailSession()
javax.mail.Store
getMailStore()
static String[]
getPartHeader(javax.mail.Part msg, String headerName)
static String
getTextFromMailPart(javax.mail.Part txtPart)
http://www.oracle.com/technetwork/java/javamail/faq/index.html#unsupenstatic boolean
isBinaryPart(javax.mail.Part binaryPart)
static void
logMailPartInfo(javax.mail.Part msg, CLogger log)
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)
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
-
-
-
Field Detail
-
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
-
-
Method Detail
-
logMailPartInfo
public static void logMailPartInfo(javax.mail.Part msg, CLogger log) throws javax.mail.MessagingException
- Throws:
javax.mail.MessagingException
-
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/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 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 inboxprocessEmailHandle
-- 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:
just manipulate message
-
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(Part, EmailContent, boolean)
-
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 attach file and put in toEmailSrv.EmailContent
for late process- 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 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
-
-