将java代码转换为coldfusion代码(CFIMAP)

时间:2018-02-13 15:29:11

标签: coldfusion

我正在尝试建立与office365邮箱的连接。此函数基于java示例(请参阅https://stackoverflow.com/a/28689722/2482184)。我差不多完成但我无法弄清楚如何转换orignal java示例中的以下代码行:

Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));

完整功能下方:

<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>

我知道有一个cfimap标签。我正在尝试找到一种更快的方法来下载电子邮件。

3 个答案:

答案 0 :(得分:4)

获取对Flag.SEEN

的引用
  

Flags.Flag - 此内部类表示单个系统标志。

然后用它来构建其他对象(Flags和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);

答案 1 :(得分:2)

创建MailFetch.java文件。

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[]

}

}

使用javac

编译文件
  • 启动命令提示符。

  • 导航到包含类文件的文件夹:

    C:&gt; cd \ mywork

  • 设置路径以包含JDK的bin。例如:

    C:\ MYWORK&GT;路径c:\ Program Files \ Java \ jdk1.8.0_121 \ bin;%path%

  • 编译您的班级:

    C:\ MYWORK&GT; javac * .java

  • 创建清单文件和您的jar文件:

    C:\ MYWORK&GT; jar cvfm Craps.jar manifest.txt * .class

将jar复制到\ ColdFusion \ cfusion \ wwwroot \ WEB-INF \ lib或\ ColdFusion \ cfusion \ lib

重启Coldfusion服务

使用

进行访问
mailobj = createObject("java","com.your.path.MailFetch");

messages = mailobj.fetchFolder("outlook.office365.com", "993", "xxxx", "xxxx", "INBOX");

你也可以尝试这个

<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">

答案 2 :(得分:0)

在我写的实际功能之下,部分基于Ageax答案。它比CFIMAP快得多,但仍然没有我希望的那么快。 fetch方法的实现可以真正加快速度。我在/ * * /之间放置了fetch命令的实现,因为它不起作用。我想我做错了什么。如果有人知道如何正确实施这种方法,我将非常感激。

<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>