如何从Cocoa发送HTML电子邮件? [英] How can I send a HTML email from Cocoa?

查看:105
本文介绍了如何从Cocoa发送HTML电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种从OS X Cocoa应用程序创建HTML格式的电子邮件的方法。

I'm looking for a way to create a HTML formatted email from a OS X Cocoa application.

我的首选工作流程是:用户选择菜单项并且默认邮件应用程序打开时在前台有一个预填的新电子邮件。

My preferred workflow would be: The user selects a menu item and the default mail application opens with a pre-filled new email in the foreground.

我可以用mailto和 - [NSWorkspace openURL]电子邮件,但这不适用于HTML电子邮件。

I'm able to do this with mailto and -[NSWorkspace openURL] for plain text emails, but this doesn't work for HTML emails.

推荐答案

我也对这个感兴趣,所以两天的逆向工程

I was interested in this too, so two days of reverse engineering Safaris 'Mail Contents of This Page' feature and I got it working.

UPDATE:我改进了代码,并把它放在 GitHub

UPDATE: I improved the code and put it on GitHub

- (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已经启动时才能使用。

This code doesn't check if Mail is running, so it's only working when Mail is already started.

这种方法的专业方面,它适用于所有实现MailLinkSupported和MailPageSupported的电子邮件客户端。请参见 QA1722

The pro side of this approach that it works with all email clients which implement MailLinkSupported and MailPageSupported. See QA1722.

缺点是你不能像 mailto 那样设置收件人。为此,脚本桥似乎是唯一的解决方案。请参阅此修改的 SBSendEmail示例

The downside is that you can't set recipients like with a mailto. For this the Scripting Bridge seems the only solution. See this modified SBSendEmail sample.

这篇关于如何从Cocoa发送HTML电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆