Following code can be use to send email in mac os:
-(IBAction)sendEmailClicked:(id)sender
{
    NSString *recipient;
    NSString *mailsender;
    NSString *mailsubject;
    NSString *mailmessage;
    recipient = [reciever stringValue];
    mailsender = [_sender stringValue];
    mailsubject = [subject stringValue];
   // mailmessage = messageBody strin
    //    NSString *bodyText = @"Your body text \n\r";
    BOOL (^checkEmail)(NSString *email);
    checkEmail = ^BOOL(NSString *email)
    {
        NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
        return [predicate evaluateWithObject:email];
    };
    if(!checkEmail(recipient))
    {
        NSLog(@"INVALID RECIPIENT EMAIL.");
        [self alert:@"Invalid recipient email."];
        return;
    }
    else if(!checkEmail(mailsender))
    {
         NSLog(@"INVALID SENDER EMAIL.");
        [self alert:@"Invalid sender email."];
        return;
    }
        NSString *emailString = [NSString stringWithFormat:@"\
                                 tell application \"Default Mail\"\n\
                                 set newMessage to make new outgoing message with properties {subject:\"%@\", content:\"%@\" & return} \n\
                                 tell newMessage\n\
                                 set visible to false\n\
                                 set sender to \"%@\"\n\
                                 make new to recipient at end of to recipients with properties {name:\"%@\", address:\"%@\"}\n\
                                 tell content\n\
                                 ",mailsubject, mailmessage, @"McAlarm alert", @"McAlarm User", mailsender];
        NSMutableArray *attachments = [[NSMutableArray alloc]init];
       [attachments addObject:[imageEdWindowController getImageLocation]];
        //add attachments to script
        for (NSString *filePath in attachments)
        {
            emailString = [emailString stringByAppendingFormat:@"make new attachment with properties {file name:\"%@\"} at after the last paragraph\n\
                           ",filePath];
            NSLog(@"emailString:%@",emailString);
        }
        //finish script
        emailString = [emailString stringByAppendingFormat:@"\
                       end tell\n\
                       send\n\
                       end tell\n\
                       end tell"];
        NSAppleScript *emailScript = [[NSAppleScript alloc] initWithSource:emailString];
    NSLog(@"emailScript:%@",emailScript);
        [emailScript executeAndReturnError:nil];
    NSLog(@"emailScriptExecuted");
        [[self window]close];
}
                       
                    
0 Comment(s)