可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to setup a connection to an office365 mailbox. This function is based on a java example (see https://stackoverflow.com/a/28689722/2482184). I am almost done but i cannot figure out how to convert the following line of code which is in the orignal java example:
Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
Below the full function:
<cfscript> mailSettings = {}; mailSettings.server = "outlook.office365.com"; mailSettings.port = "993"; mailSettings.username = "xxxx"; mailSettings.password = "xxxx"; mailSettings.action = ""; mailSettings.folder = "INBOX"; mailSettings.timeout = 5000; properties = createObject("Java","java.util.Properties"); jsession = createObject("Java","javax.mail.Session"); store = createObject("Java","javax.mail.Store"); message = CreateObject("Java", "javax.mail.Message"); properties.init(); properties.put("mail.store.protocol","imap"); properties.put("mail.from", mailSettings.username); properties.put("mail.imap.port", mailSettings.port); properties.put("mail.imap.connectiontimeout",mailSettings.timeout); properties.put("mail.imap.timeout",mailSettings.timeout); jsession = jsession.getInstance(properties); store = jsession.getStore("imaps"); store.connect(mailSettings.server, mailSettings.username, mailSettings.password); inbox = store.getFolder("#mailSettings.folder#"); inbox.open( inbox.READ_ONLY ); /********** NEED HELP TO CONVERT THIS LINE BELOW TO COLDFUSION SYNTAX --------- Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false)); --------- END NEED HELP **********/ inbox.close(true); </cfscript>
I know there is a cfimap tag. I am trying to find a faster method to download emails.
回答1:
Get a reference to Flag.SEEN
Flags.Flag
- This inner class represents an individual system flag.
Then use it to build the other objects (Flags and FlagTerm):
Flag = createObject("java", "javax.mail.Flags$Flag"); Seen = createObject("java", "javax.mail.Flags").init(Flag.SEEN); Term = createObject("java", "javax.mail.search.FlagTerm").init(Seen, false); // do search ... messages = inbox.search(Term);
回答2:
Create a MailFetch.java file.
imports .... ..... public class MailFetch { public function fetchFolder(String server, String port, String username, String password, String foldername){ Session session = Session.getDefaultInstance(new Properties( )); Store store = session.getStore("imaps"); store.connect(server, port, username, password); Folder inbox = store.getFolder(foldername); inbox.open( Folder.READ_ONLY ); .... code goes here ..... return messages; // Message[] }
}
Compile the file using javac
Start Command Prompt.
Navigate to the folder that holds your class files:
C:>cd \mywork
Set path to include JDK’s bin. For example:
C:\mywork> path c:\Program Files\Java\jdk1.8.0_121\bin;%path%
Compile your class(es):
C:\mywork> javac *.java
Create a manifest file and your jar file:
C:\mywork> jar cvfm Craps.jar manifest.txt *.class
copy the jar to \ColdFusion\cfusion\wwwroot\WEB-INF\lib or \ColdFusion\cfusion\lib
restart Coldfusion service
Access using
mailobj = createObject("java","com.your.path.MailFetch"); messages = mailobj.fetchFolder("outlook.office365.com", "993", "xxxx", "xxxx", "INBOX");
you can try this also
<cfexchangeConnection action="open" username="xxxx" password="xxxx" port="993" server="outlook.office365.com" connection="testconn1"> <cfexchangemail action="get" name="inbox" connection="testconn1" folder = "INBOX"></cfexchangemail> <cfexchangeConnection action="close" connection="testconn1">
回答3:
Below the actual function i have written, partly based on Ageax answer. It is way faster then CFIMAP but still not as fast as i would like it to be. Implementation of the fetch method could really speed things up. I placed my implementation of the fetch command between /* */ because it does not work. I guess i am doing something wrong. If someone knows how to properly implement this method i would be very grateful.
<cfscript> function fecthImap() { var local = {}; local.settings = {}; local.settings.server = "outlook.office365.com"; local.settings.port = "993"; local.settings.username = "xxxx"; local.settings.password = "xxxx"; local.settings.action = ""; local.settings.folder = "INBOX"; local.settings.timeout = 5000; local.settings.tick = GetTickCount(); local.settings.x = 1; local.java = {}; local.java.properties = createObject("Java","java.util.Properties"); local.java.properties.init(); local.java.properties.put("mail.store.protocol","imap"); local.java.properties.put("mail.from", local.settings.username); local.java.properties.put("mail.imap.port", local.settings.port); local.java.properties.put("mail.imap.connectiontimeout",local.settings.timeout); local.java.properties.put("mail.imap.timeout",local.settings.timeout); local.java.session = createObject("Java","javax.mail.Session"); local.java.session = local.java.session.getInstance(local.java.properties); local.java.store = createObject("Java","javax.mail.Store"); local.java.store = local.java.session.getStore("imaps"); local.java.store.connect(local.settings.server, local.settings.username, local.settings.password); local.java.folder = local.java.store.getFolder("#local.settings.folder#"); local.java.folder.open( local.java.folder.READ_ONLY ); local.java.mailFlags = createObject("java", "javax.mail.Flags$Flag"); local.java.mailFlagsSeen = createObject("java", "javax.mail.Flags").init(local.java.mailFlags.SEEN); local.java.mailFlagTerm = createObject("java", "javax.mail.search.FlagTerm").init(local.java.mailFlagsSeen, false); local.java.messages = local.java.folder.search(local.java.mailFlagTerm); /*This part could realy speed thinks up, but it does not work as expected*/ /**************************** local.java.profile = createObject("Java","javax.mail.FetchProfile"); local.java.profileItem = createObject("Java","javax.mail.FetchProfile$Item"); local.java.profile.add(local.java.profileItem.ENVELOPE); local.java.folder.fetch(local.java.messages, local.java.profile); ****************************/ for(local.settings.x=1;local.settings.x<=arraylen(local.java.messages);local.settings.x=local.settings.x+1) { local.java.message = local.java.messages[local.settings.x]; WriteOutput(local.settings.x & ':' & GetTickCount()-local.settings.tick & ':' & local.java.message.getSubject() & '<br>'); } local.java.folder.close(true); } fecthImap(); </cfscript>