0

当出现问题时,我使用此代码发送自动错误消息,它工作正常,但它的行为有点有趣。我从这个 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");
}

它撰写并发送一条指定主题和正文的新邮件,但它使撰写的邮件保持打开状态,我必须手动关闭撰写的邮件以及邮件本身。

关于如何告诉邮件自动关闭邮件及其本身的任何想法?

4

3 回答 3

0

这是我的回答,对我来说效果很好。我的猜测是您的代码中某处存在错误。也许变量未初始化或格式错误?

尝试将静态 Apple Script 代码放入 Apple Script 编辑器并运行它以查看它是否适合您。如果确实如此,那么您的 Obj-C 代码应该有问题。

于 2012-10-31T10:50:55.533 回答
0

我认为只有当 Mail 是当时打开的全屏应用程序时才会出现此错误,但如果您关闭了窗口(但邮件仍在扩展坞中运行),您将不会收到仍然打开的撰写邮件消息。

于 2013-04-27T01:25:24.237 回答
0
+ (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);
    }
}
于 2014-07-28T07:29:02.980 回答