简体   繁体   中英

Objective-C: Send email automatically (AppleScript)

I use this code to send an automated error message when something goes wrong, and it works fine, but it behaves a bit funny. I got the code from this SO question .

- (void)sendEmailWithMail:(NSString *) toAddress withSubject:(NSString *) subject Attachments:(NSArray *) attachments { 
NSString *bodyText = @"Your body text \n\r";    
NSString *emailString = [NSString stringWithFormat:@"\
                         tell application \"Mail\"\n\
                         set newMessage to make new outgoing message with properties {subject:\"%@\", content:\"%@\" & return} \n\
                         tell newMessage\n\
                         set visible to false\n\
                         set sender to \"%@\"\n\
                         make new to recipient at end of to recipients with properties {name:\"%@\", address:\"%@\"}\n\
                         tell content\n\
                         ",subject, bodyText, @"McAlarm alert", @"McAlarm User", toAddress ];

//add attachments to script
for (NSString *alarmPhoto in attachments) {
    emailString = [emailString stringByAppendingFormat:@"make new attachment with properties {file name:\"%@\"} at after the last paragraph\n\
                   ",alarmPhoto];

}
//finish script
emailString = [emailString stringByAppendingFormat:@"\
               end tell\n\
               send\n\
               end tell\n\
               end tell"];



//NSLog(@"%@",emailString);
NSAppleScript *emailScript = [[NSAppleScript alloc] initWithSource:emailString];
[emailScript executeAndReturnError:nil];
[emailScript release];

/* send the message */
NSLog(@"Message passed to Mail");
}

It composes and sends a new message with the subject and body specified but it leaves the composed message open and i have to manually close the composed message as well as Mail itself.

Any ideas on how to tell Mail to close the mail and itself automatically?

It's from my answer and it works fine for me. My guess is there is an error somewhere in your code. Maybe a variable is not initialised or badly formatted?

Try to put the static Apple Script code into the Apple Script editor and run it to see whether it works there for you. If it does than it should be something wrong in your Obj-C code.

我认为,仅当Mail是当时打开的全屏应用程序时,才会发生此错误,但是如果您关闭了窗口(但邮件仍在扩展坞中运行),则不会使撰写的邮件仍处于打开状态。

+ (void)sendEmailWithMail:(NSString *) toAddress withSubject:(NSString *) subject Attachments:(NSArray *) attachments withBody:(NSString*)str_Body
{
    @try
    {
        NSString *emailString = [NSString stringWithFormat:@"\
                                 tell application \"Mail\"\n\
                                 set newMessage to make new outgoing message with properties {subject:\"%@\", content:\"%@\" & return} \n\
                                 tell newMessage\n\
                                 set visible to false\n\
                                 set sender to \"%@\"\n\
                                 make new to recipient at end of to recipients with properties {name:\"%@\", address:\"%@\"}\n\
                                 tell content\n\
                                 ",subject, str_Body, @"", @"", toAddress ];

        //add attachments to script
        for (NSString *alarmPhoto in attachments)
        {
            emailString = [emailString stringByAppendingFormat:@"make new attachment with properties {file name:\"%@\"} at after the last paragraph\n\
                           ",alarmPhoto];
        }
        //finish script
        emailString = [emailString stringByAppendingFormat:@"\
                       end tell\n\
                       send\n\
                       end tell\n\
                       end tell"];

        NSAppleScript *emailScript = [[NSAppleScript alloc] initWithSource:emailString];
        [emailScript executeAndReturnError:nil];
        emailScript = nil;

        /* send the message */
        //    NSLog(@"Message passed to Mail");
    }
    @catch (NSException *exception)
    {
        NSLog(@"Error from sendEmailWithMail in Cls_Quiz_View.m : %@",exception);
    }
}

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