1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MailUtils.m Tue Apr 07 11:13:25 2009 -0700
1.3 @@ -0,0 +1,136 @@
1.4 +//
1.5 +// MailUtils.m
1.6 +// YourMove
1.7 +//
1.8 +// Created by Jens Alfke on 7/13/08.
1.9 +// Copyright 2008 Jens Alfke. All rights reserved.
1.10 +// Adapted from Apple's "SBSendEmail" sample app.
1.11 +//
1.12 +
1.13 +#import "MailUtils.h"
1.14 +#import "MailBridge.h"
1.15 +#import "CollectionUtils.h"
1.16 +
1.17 +
1.18 +@implementation OutgoingEmail
1.19 +
1.20 +
1.21 +- (id) init
1.22 +{
1.23 + self = [super init];
1.24 + if (self != nil) {
1.25 + _toRecipients = [[NSMutableArray alloc] init];
1.26 + _attachments = [[NSMutableArray alloc] init];
1.27 + }
1.28 + return self;
1.29 +}
1.30 +
1.31 +- (id) initWithSubject: (NSString*)subject body: (NSString*)body
1.32 +{
1.33 + self = [self init];
1.34 + if (self != nil) {
1.35 + self.subject = subject;
1.36 + self.body = body;
1.37 + }
1.38 + return self;
1.39 +}
1.40 +
1.41 +- (void) dealloc
1.42 +{
1.43 + [_message release];
1.44 + [_subject release];
1.45 + [_body release];
1.46 + [_sender release];
1.47 + [_toRecipients release];
1.48 + [_attachments release];
1.49 + [super dealloc];
1.50 +}
1.51 +
1.52 +
1.53 +@synthesize subject=_subject, sender=_sender, body=_body,
1.54 + toRecipients=_toRecipients, attachments=_attachments;
1.55 +
1.56 +
1.57 ++ (MailApplication*) mailApp
1.58 +{
1.59 + /* create a Scripting Bridge object for talking to the Mail application */
1.60 + MailApplication *mail = [SBApplication applicationWithBundleIdentifier:@"com.apple.Mail"];
1.61 + mail.timeout = 5*60; // in ticks
1.62 + return mail;
1.63 +}
1.64 +
1.65 ++ (BOOL) isMailRunning
1.66 +{
1.67 + return [self mailApp].isRunning;
1.68 +}
1.69 +
1.70 +- (MailOutgoingMessage*) _message
1.71 +{
1.72 + if( ! _message ) {
1.73 + MailApplication *mail = [[self class] mailApp];
1.74 +
1.75 + /* create a new outgoing message object */
1.76 + MailOutgoingMessage *emailMessage =
1.77 + [[[mail classForScriptingClass:@"outgoing message"] alloc] initWithProperties:
1.78 + $dict({@"subject", self.subject},
1.79 + {@"content", self.body})];
1.80 +
1.81 + /* set the sender, show the message */
1.82 + if( _sender )
1.83 + emailMessage.sender = _sender;
1.84 +
1.85 + /* Have to add this to a container now, else the scripting bridge complains */
1.86 + [[mail outgoingMessages] addObject: emailMessage];
1.87 +
1.88 + /* create a new recipient and add it to the recipients list */
1.89 + for( NSString *recipient in _toRecipients ) {
1.90 + MailToRecipient *theRecipient =
1.91 + [[[mail classForScriptingClass:@"to recipient"] alloc] initWithProperties:
1.92 + $dict({@"address", recipient})];
1.93 + [emailMessage.toRecipients addObject: theRecipient];
1.94 + }
1.95 +
1.96 + /* add an attachment, if one was specified */
1.97 + for( NSString *attachmentPath in self.attachments ) {
1.98 + /* create an attachment object */
1.99 + MailAttachment *theAttachment =
1.100 + [[[mail classForScriptingClass:@"attachment"] alloc] initWithProperties:
1.101 + $dict({@"fileName", attachmentPath})];
1.102 +
1.103 + /* add it to the list of attachments */
1.104 + [[emailMessage.content attachments] addObject: theAttachment];
1.105 + }
1.106 +
1.107 + /* add the object to the mail app */
1.108 + _message = [emailMessage retain];
1.109 + }
1.110 + return _message;
1.111 +}
1.112 +
1.113 +
1.114 +- (void) show
1.115 +{
1.116 + self._message.visible = YES;
1.117 + [[[self class] mailApp] activate];
1.118 +}
1.119 +
1.120 +
1.121 +- (void) send
1.122 +{
1.123 + [self._message send];
1.124 +}
1.125 +
1.126 +
1.127 +@end
1.128 +
1.129 +
1.130 +
1.131 +
1.132 +TestCase(MailUtils) {
1.133 + OutgoingEmail *m = [[OutgoingEmail alloc] initWithSubject: @"This is a test"
1.134 + body: @"Hi there! This is a test email from an automated test case. http://mooseyard.com/"];
1.135 + [m.toRecipients addObject: @"jens@mooseyard.com"];
1.136 + [m show];
1.137 + //[m send];
1.138 + [m release];
1.139 +}