0

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

受信者を指定していないため、メール アプリケーションは「エラー、受信者が指定されていません」というアラートを開きます。このアラートには [メッセージの編集] ボタンが 1 つしかありませんが、ユーザーはこのボタンを使用して受信者を追加できます。このアラートが開かない可能性はありますか?

4

1 に答える 1

1

あなたが試すことができます

[emailMessage open];

これにより、Mail.app はメッセージを作成ウィンドウで開きます。

Mail.app を最前面のアプリケーションにして、ユーザーが新しく作成されたメッセージ ウィンドウを表示できるようにするには、次を使用します。

 [mail activate];
于 2011-05-31T18:30:36.810 に答える