Working with Sheets in Cocoa
Subject:   Extension: Sending email from Address Book
Date:   2001-10-17 15:34:16
From:   mikenidel
I've been sticking pretty closely to this tutorial so far, but I finally decided I felt intrepid enough to try extending the application a bit.

Specifically, I wanted to be able to select a row or rows from the address book, click a button, and open an email with the right recipient(s) in the To: field. Turns out it isn't too hard, once I found out how to open a URL. Here's how I did it, feel free to add to or comment on this approach.

In Interface Builder: Create an action in the Controller class called sendMail. Then add a button wherever you like and connect it to the controller, using the sendMail message as the target for the button. I added a "Send Email" button to the right of the "Delete" button in the main application window.

Once you have that connected up, add the line

- (IBAction)sendMail:(id)sender;

to your Controller.h file, and add the following code to the implementation in Controller.m:

- (IBAction)sendEmail:(id)sender
NSMutableString *url = [NSMutableString stringWithString:@"mailto:"];
NSString *str;
NSEnumerator *enumerator;
NSNumber *index;

enumerator = [tableView selectedRowEnumerator];

if((index = [enumerator nextObject])) {
str = [[records objectAtIndex:[index intValue]] objectForKey:@"Email"];
[url appendString:str];

while ( (index = [enumerator nextObject]) ) {
str = [[records objectAtIndex:[index intValue]] objectForKey:@"Email"];
[url appendString:@","];
[url appendString:str];
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString:url]];

Note that if you're familiar with the mailto: URL format standard, you can make the new message open with a starting subject, body, and various other headers, using the query syntax. Try out "" and find out more info about the mailto: URL options at

Note that you have to encode any characters that aren't allowed in URLs - particularly blank spaces (%20) and such. I'm sure there are Cocoa methods to encode and decode URL strings, I just haven't found them yet.

Good Luck!


1 to 2 of 2
  1. Extension: Sending email from Address Book
    2002-01-02 21:28:53  michele [View]

  2. Extension: Sending email from Address Book
    2001-10-26 12:07:56  rainwadj [View]

1 to 2 of 2