jens@17: // jens@17: // MailUtils.m jens@17: // YourMove jens@17: // jens@17: // Created by Jens Alfke on 7/13/08. jens@17: // Copyright 2008 Jens Alfke. All rights reserved. jens@17: // Adapted from Apple's "SBSendEmail" sample app. jens@17: // jens@17: jens@17: #import "MailUtils.h" jens@17: #import "MailBridge.h" jens@17: #import "CollectionUtils.h" jens@17: jens@17: jens@17: @implementation OutgoingEmail jens@17: jens@17: jens@17: - (id) init jens@17: { jens@17: self = [super init]; jens@17: if (self != nil) { jens@17: _toRecipients = [[NSMutableArray alloc] init]; jens@17: _attachments = [[NSMutableArray alloc] init]; jens@17: } jens@17: return self; jens@17: } jens@17: jens@17: - (id) initWithSubject: (NSString*)subject body: (NSString*)body jens@17: { jens@17: self = [self init]; jens@17: if (self != nil) { jens@17: self.subject = subject; jens@17: self.body = body; jens@17: } jens@17: return self; jens@17: } jens@17: jens@17: - (void) dealloc jens@17: { jens@17: [_message release]; jens@17: [_subject release]; jens@17: [_body release]; jens@17: [_sender release]; jens@17: [_toRecipients release]; jens@17: [_attachments release]; jens@17: [super dealloc]; jens@17: } jens@17: jens@17: jens@17: @synthesize subject=_subject, sender=_sender, body=_body, jens@17: toRecipients=_toRecipients, attachments=_attachments; jens@17: jens@17: jens@17: + (MailApplication*) mailApp jens@17: { jens@17: /* create a Scripting Bridge object for talking to the Mail application */ jens@17: MailApplication *mail = [SBApplication applicationWithBundleIdentifier:@"com.apple.Mail"]; jens@17: mail.timeout = 5*60; // in ticks jens@17: return mail; jens@17: } jens@17: jens@17: + (BOOL) isMailRunning jens@17: { jens@17: return [self mailApp].isRunning; jens@17: } jens@17: jens@17: - (MailOutgoingMessage*) _message jens@17: { jens@17: if( ! _message ) { jens@17: MailApplication *mail = [[self class] mailApp]; jens@17: jens@17: /* create a new outgoing message object */ jens@17: MailOutgoingMessage *emailMessage = jens@17: [[[mail classForScriptingClass:@"outgoing message"] alloc] initWithProperties: jens@17: $dict({@"subject", self.subject}, jens@17: {@"content", self.body})]; jens@17: jens@17: /* set the sender, show the message */ jens@17: if( _sender ) jens@17: emailMessage.sender = _sender; jens@17: jens@17: /* Have to add this to a container now, else the scripting bridge complains */ jens@17: [[mail outgoingMessages] addObject: emailMessage]; jens@17: jens@17: /* create a new recipient and add it to the recipients list */ jens@17: for( NSString *recipient in _toRecipients ) { jens@17: MailToRecipient *theRecipient = jens@17: [[[mail classForScriptingClass:@"to recipient"] alloc] initWithProperties: jens@17: $dict({@"address", recipient})]; jens@17: [emailMessage.toRecipients addObject: theRecipient]; jens@17: } jens@17: jens@17: /* add an attachment, if one was specified */ jens@17: for( NSString *attachmentPath in self.attachments ) { jens@17: /* create an attachment object */ jens@17: MailAttachment *theAttachment = jens@17: [[[mail classForScriptingClass:@"attachment"] alloc] initWithProperties: jens@17: $dict({@"fileName", attachmentPath})]; jens@17: jens@17: /* add it to the list of attachments */ jens@17: [[emailMessage.content attachments] addObject: theAttachment]; jens@17: } jens@17: jens@17: /* add the object to the mail app */ jens@17: _message = [emailMessage retain]; jens@17: } jens@17: return _message; jens@17: } jens@17: jens@17: jens@17: - (void) show jens@17: { jens@17: self._message.visible = YES; jens@17: [[[self class] mailApp] activate]; jens@17: } jens@17: jens@17: jens@17: - (void) send jens@17: { jens@17: [self._message send]; jens@17: } jens@17: jens@17: jens@17: @end jens@17: jens@17: jens@17: jens@17: jens@17: TestCase(MailUtils) { jens@17: OutgoingEmail *m = [[OutgoingEmail alloc] initWithSubject: @"This is a test" jens@17: body: @"Hi there! This is a test email from an automated test case. http://mooseyard.com/"]; jens@17: [m.toRecipients addObject: @"jens@mooseyard.com"]; jens@17: [m show]; jens@17: //[m send]; jens@17: [m release]; jens@17: }