我正在寻找一种从OS X
Cocoa应用程序创建HTML格式的电子邮件的方法.
我首选的工作流程是:用户选择一个菜单项,默认的邮件应用程序打开,前台有一个预先填好的新电子邮件.
我可以使用mailto和 – [NSWorkspace openURL]来处理纯文本电子邮件,但这不适用于HTML电子邮件.
解决方法
我也对此感兴趣,所以两天逆向工程Safaris的“本页的邮件内容”功能,我得到了它的工作.
- (void)mailWebArchive:(WebArchive *)webArchive title:(NSString *)aTitle URL:(NSString *)aURL { NSString *bundleID = @"com.apple.mail"; NSData* targetBundleID = [bundleID dataUsingEncoding:NSUTF8StringEncoding]; NSAppleEventDescriptor *targetDescriptor = nil; NSAppleEventDescriptor *appleEvent = nil; targetDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeApplicationBundleID data:targetBundleID]; appleEvent = [NSAppleEventDescriptor appleEventWithEventClass:'mail' eventID:'mlpg' targetDescriptor:targetDescriptor returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID]; [appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithDescriptorType:'tdta' data:[webArchive data]] forKeyword:'----']; [appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:aTitle] forKeyword:'urln']; [appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:aURL] forKeyword:'url ']; NSAppleEventDescriptor *replyDescriptor = nil; NSAppleEventDescriptor *errorDescriptor = nil; AEDesc reply = { typeNull,NULL }; // Send the AppleEvent OSStatus status = AESendMessage([appleEvent aeDesc],&reply,kAEWaitReply,kAEDefaultTimeout); if(status == noErr) { replyDescriptor = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&reply] autorelease]; errorDescriptor = [replyDescriptor paramDescriptorForKeyword:keyErrorNumber]; if(errorDescriptor != nil) status = [errorDescriptor int32Value]; if(status != noErr) NSLog(@"%s error %d",_cmd,status); } }
此代码不检查Mail是否正在运行,因此它仅在Mail已启动时才起作用.
这种方法的专业方面,它适用于实现MailLinkSupported和MailPageSupported的所有电子邮件客户端.见QA1722.
缺点是你不能像mailto一样设置收件人.为此,Scripting Bridge似乎是唯一的解决方案.见修改后的SBSendEmail sample.