2011-05-31 76 views
0

我使用默认Apple提供的SBSendEmail示例中的代码发送电子邮件。在我的情况唯一的区别是,我不知道收件人事先,我希望用户在邮件的收件人窗口中输入收件人。这里是我的代码:使用Cocoa Scripting Bridge在事先不知道收件人的情况下发送电子邮件

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: 
//   @"[email protected]", @"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]; 

因为我还没有指定的收件人,邮件应用程序打开一个警告,指出错误,你有没有指定收件人。尽管此警报只有一个“Edit Message”按钮,用户可以使用该按钮然后添加收件人。这种警报无法打开吗?

回答

1

您可以尝试

[emailMessage open]; 

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

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

[mail activate]; 
+0

感谢diciu,但是是我需要用户的注意力,也 - 是它在某种程度上可能将打开的撰写窗口放在前面? – AmaltasCoder 2011-05-31 18:54:08

+0

我编辑了我的答案。您可以在MailApplication实例上调用“activate”(代码中的“mail”)。 – diciu 2011-06-01 06:24:39

+0

辉煌,谢谢... – AmaltasCoder 2011-06-01 12:54:43

相关问题