|
Q: I want to add a feature to my application that allows a user to easily email my technical support address. What's the easiest way to do this?A: The Message framework ( Listing 1: Creating an email message in Cocoa
- (IBAction)sendMailCocoa:(id)sender
// Create a mail message in the user's preferred mail client
// by opening a mailto URL. The extended mailto URL format
// is documented by RFC 2368 and is supported by Mail.app
// and other modern mail clients.
//
// This routine's prototype makes it easy to connect it as
// the action of a user interface object in Interface Builder.
{
NSURL * url;
// Create the URL.
url = [NSURL URLWithString:@"mailto:dts@apple.com"
"?subject=Hello%20Cruel%20World!"
"&body=Share%20and%20Enjoy"
];
assert(url != nil);
// Open the URL.
(void) [[NSWorkspace sharedWorkspace] openURL:url];
}
Listing 2: Creating an email message in plain C
static void SendMailCarbon(void)
// Create a mail message in the user's preferred mail client
// by opening a mailto URL. The extended mailto URL format
// is documented by RFC 2368 and is supported by Mail.app
// and other modern mail clients.
{
OSStatus err;
CFURLRef url;
static const char kMailtoURL[] =
"mailto:dts@apple.com"
"?subject=Hello%20Cruel%20World!"
"&body=Share%20and%20Enjoy";
url = CFURLCreateWithBytes(
NULL,
(const UInt8 *) kMailtoURL,
sizeof(kMailtoURL),
kCFStringEncodingASCII,
NULL
);
assert(url != NULL);
err = LSOpenCFURLRef(url, NULL);
assert(err == noErr);
CFRelease(url);
}
Things get more complex if you want to create a message with an attachment. The underlying technology for opening a URL is the Document Revision History
Posted: 2004-11-05 |
|