我使用此代码在出现问题时发送自动错误消息,并且工作正常,但它表现得有点滑稽。我从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");
}
它编写并发送一条指定了主题和正文的新消息,但它使组合消息保持打开状态,我必须手动关闭撰写的消息以及Mail本身。
有关如何告诉Mail自动关闭邮件及其自身的任何想法吗?
答案 0 :(得分:0)
这是我的回答,它对我来说很好。 我猜你的代码中某处有错误。也许变量没有初始化或格式错误?
尝试将静态Apple Script代码放入Apple Script编辑器并运行它以查看它是否适合您。如果确实如此,那么你的Obj-C代码应该是错误的。
答案 1 :(得分:0)
我认为只有当Mail是一个当时打开的全屏应用程序时才会出现此错误,但如果您关闭了窗口(但邮件仍在Dock中运行),您将无法获得合成邮件消息开放。
答案 2 :(得分: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);
}
}