cocoa:从Mail.app读取邮件

时间:2012-09-04 09:01:23

标签: macos cocoa

我想创建一个Mac应用程序,它从Mail.app读取电子邮件并显示它们,但我找不到有关如何从Mail.app读取邮件的任何信息。我的问题是:

  1. 如何阅读Mail.app的电子邮件?任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:3)

Applescript可能是最简单的方法,例如:

tell application "Mail"
    set theMessage to message 1 of inbox
    set theBody to content of theMessage as rich text
end tell

通过NSAppleScript运行脚本,快速脏示例(无错误处理等):

NSString *theSource = @"tell application \"Mail\"\n"
"set theMessage to message 4 of inbox\n"
"set theBody to content of theMessage as rich text\n"
"end tell\n"
"return theBody";
NSAppleScript* theScript = [[NSAppleScript alloc] initWithSource:theSource];
NSAppleEventDescriptor* theDescriptor = [theScript executeAndReturnError:NULL];
NSLog(@"%@", theDescriptor);

替代使用osascriptNSTask来运行脚本。

编辑(回复评论)

遍历所有消息并将其正文添加到theMessages列表。

set theMessages to {}
tell application "Mail"
    set theCount to get the count of messages of inbox
    repeat with theIncrementValue from 1 to theCount by 1
        set TheMessage to message 1 of inbox
        set theBody to content of TheMessage as rich text
        set end of theMessages to theBody
    end repeat
end tell
return theMessages