objective-c - 使用 Cocoa Scripting Bridge 在事先不知道收件人的情况下发送电子邮件

标签 objective-c cocoa scripting-bridge

我正在使用 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/

相关文章:

ios - 我有一个嵌套在 for-in 循环中的 if 语句,它的计算不正确

iPhone 多 View 旋转 hell

objective-c - NSMutableDictionary keyEnumerator 还是 NSArray?

iphone - 如何检查 NSSet 或 NSCountedSet 中是否存在现有项目?

python - 操作系统 X : Move window from Python

objective-c - SB应用程序: how to send raw data

ios - UINavigationBar 高度错误,因为状态栏在加载时被隐藏

iphone - 使用 Storyboard时子类化 UIWindow

objective-c - 复制、粘贴、删除操作的系统音效

cocoa - 使用 ScriptingBridge 框架与 Entourage 进行通信