我正在使用 Apple 提供的默认 SBSendEmail 示例中的代码来发送电子邮件。我的情况唯一的区别是我事先不知道收件人,我希望用户在邮件窗口的“收件人”字段中输入收件人。这是我的代码:
MailApplication *mail = [SBApplication
applicationWithBundleIdentifier:@"com.apple.Mail"];
/* create a new outgoing message object */
MailOutgoingMessage *emailMessage =
[[[mail classForScriptingClass:@"outgoing message"] alloc]
initWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
@"this is my subject", @"subject",
@"this is my content", @"content",
nil]];
/* add the object to the mail app */
[[mail outgoingMessages] addObject: emailMessage];
/* set the sender, show the message */
// emailMessage.sender = [self.fromField stringValue];
emailMessage.visible = YES;
/* create a new recipient and add it to the recipients list */
// MailToRecipient *theRecipient =
// [[[mail classForScriptingClass:@"to recipient"] alloc]
// initWithProperties:
// [NSDictionary dictionaryWithObjectsAndKeys:
// @"recipientEmailHere@example.com", @"address",
// nil]];
// [emailMessage.toRecipients addObject: theRecipient];
/* add an attachment, if one was specified */
NSString *attachmentFilePath = "<my provided file path>";
if ( [attachmentFilePath length] > 0 ) {
/* create an attachment object */
MailAttachment *theAttachment = [[[mail
classForScriptingClass:@"attachment"] alloc]
initWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
attachmentFilePath, @"fileName",
nil]];
/* add it to the list of attachments */
[[emailMessage.content attachments] addObject: theAttachment];
}
/* send the message */
[emailMessage send];
由于我没有指定收件人,邮件应用程序会打开一条警告,提示“错误,您没有指定任何收件人”。尽管此警报只有一个“编辑消息”按钮,用户可以使用该按钮添加收件人。此警报是否有可能无法打开?
最佳答案
你可以试试
[emailMessage open];
这将导致 Mail.app 在撰写窗口中打开您的邮件。
要使 Mail.app 成为最前面的应用程序,以便用户可以看到新创建的消息窗口,请使用:
[mail activate];
关于objective-c - 使用 Cocoa Scripting Bridge 在事先不知道收件人的情况下发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6191231/