【问题标题】:Using Cocoa Scripting Bridge to send email without knowing the recipient beforehand使用 Cocoa Scripting Bridge 在事先不知道收件人的情况下发送电子邮件
【发布时间】:2011-05-31 17:28:26
【问题描述】:

我正在使用 Apple 默认提供的 SB​​SendEmail 示例中的代码来发送电子邮件。就我而言,唯一的区别是我事先不知道收件人,我希望用户在邮件窗口的收件人字段中输入收件人。这是我的代码:

    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];

由于我没有指定收件人,邮件应用程序会打开一条警报,提示错误,您没有指定任何收件人。尽管此警报只有一个按钮“编辑消息”,但用户可以使用该按钮添加收件人。此警报是否有可能无法打开?

【问题讨论】:

    标签: objective-c cocoa scripting-bridge


    【解决方案1】:

    你可以试试

    [emailMessage open];
    

    这将导致 Mail.app 在撰写窗口中打开您的邮件。

    要使 Mail.app 成为最前面的应用程序以便用户可以看到新创建的消息窗口,请使用:

     [mail activate];
    

    【讨论】:

    • 谢谢diciu,但是我也需要用户的注意——是否有可能将打开的撰写窗口放在前面?
    • 我已经编辑了我的答案。您可以在 MailApplication 实例上调用“activate”(代码中的“mail”)。
    猜你喜欢
    • 1970-01-01
    • 2014-01-01
    • 2011-05-18
    • 2011-06-09
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多