简体   繁体   中英

cocoa: read mail from Mail.app

I want to make an Mac application which read e-mails from Mail.app and display them, but I can't find any info about how to read mail from Mail.app. My question is:

  1. how to read e-mails from Mail.app? any suggestion would be appreciated.

Applescript is probably the easiest way, for example:

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

Run the script through NSAppleScript , quick dirty example (no error handling and so on):

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

Alternative use osascript through NSTask to run the script.

EDIT (response to comment)

Loop through all messages and add their body to the theMessages list.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM