开发者

Using Cocoa Scripting Bridge to send email without knowing the recipient beforehand

开发者 https://www.devze.com 2023-03-10 00:07 出处:网络
I am using the code from default Apple provided SBSendEmail example to send an email. The only difference in my case is that I don\'t know the recipient beforehand and I expect the user to enter the r

I am using the code from default Apple provided SBSendEmail example to send an email. The only difference in my case is that I don't know the recipient beforehand and I expect the user to enter the recipient in the To field of mail window. Here is my code:

    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 */
    //  emai开发者_开发问答lMessage.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];

Since I have not specified the recipient, the mail application opens an alert saying Error, you have not specified any recipients. Although this alert has only one button "Edit Message" using which the user can then go and add recipients. Is it somehow possible that this alert doesn't open?


You can try

[emailMessage open];

which will cause Mail.app to open your message in a compose window.

To make Mail.app the frontmost application so that the user can see the newly created message window, use:

 [mail activate];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号