2009-09-08 40 views

回答

8

Apple's Developer Connection有一個名爲SBSendEmail的示例項目,演示瞭如何使用腳本橋將腳本發送到郵件應用程序。

您可以下載whole project並在XCode中運行它以查看其工作原理。特別感興趣的是您的sendEmailMessage:方法Controller.m

+5

請注意,沙盒應用程序不允許使用Scripting Bridge。 – jemeshsu 2012-01-05 11:15:13

+0

這是不正確的;你可以使用正確的授權腳本橋 – 2014-08-21 08:34:39

4

您可以使用打開默認mail client或者您可以使用framework。這應該讓你開始。

+0

他們倆都不適合我。任何人都可以給我一個例子嗎?我很抱歉,但我對此很新穎? – lab12 2009-09-08 22:32:22

+0

您正在爲iPhone或桌面開發嗎? – Garrett 2009-09-08 23:03:41

+0

我正在使用Xcode 3在桌面上開發。2 – lab12 2009-09-09 00:37:49

2

對於Growl 1.2,我寫了a Python-based mail-sending program,MailMe顯示使用NSTask運行。

我在很大程度上出於對可可的其他郵件框架的不滿,其中大部分郵件框架也支持接收郵件,這對於某些輸出(如MailMe)來說是不必要的。

15

我認爲使用Apple腳本是最簡單的解決方案。當用戶在Mail中設置帳戶時使用它。

Apple腳本:

attachments是一個包含文件路徑的stings數組。

「設置爲true可見」,如果你想在郵件中發送

- (void)sendEmailWithMail:(NSString *) toAddress withSubject:(NSString *) subject Attachments:(NSArray *) attachments { 
NSString *bodyText = @"Your body text \n\r";  
NSString *emailString = [NSString stringWithFormat:@"\ 
         tell application \"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\ 
         ",subject, bodyText, @"McAlarm alert", @"McAlarm User", toAddress ]; 

//add attachments to script 
for (NSString *alarmPhoto in attachments) { 
    emailString = [emailString stringByAppendingFormat:@"make new attachment with properties {file name:\"%@\"} at after the last paragraph\n\ 
        ",alarmPhoto]; 

} 
//finish script 
emailString = [emailString stringByAppendingFormat:@"\ 
       end tell\n\ 
       send\n\ 
       end tell\n\ 
       end tell"]; 



//NSLog(@"%@",emailString); 
NSAppleScript *emailScript = [[NSAppleScript alloc] initWithSource:emailString]; 
[emailScript executeAndReturnError:nil]; 
[emailScript release]; 

/* send the message */ 
NSLog(@"Message passed to Mail"); 

}

缺點之前彈出:用戶需要有郵件下有效的帳戶。

Python腳本(如果郵件下沒有用戶帳戶): 您也可以使用python腳本發送消息。 缺點:用戶必須輸入SMTP詳細信息,除非您從郵件中抓取它們(但是您可以使用上面的Apple腳本),或者您必須在應用程序中硬編碼一個可靠的SMTP中繼(您可以設置一個Gmail帳戶並使用它用於這一目的,但如果您的應用程序發送過多的電子郵件,谷歌可以刪除自己的帳戶(SPAM))
我用這個python腳本:

import sys 
import smtplib 
import os 
import optparse 

from email.MIMEMultipart import MIMEMultipart 
from email.MIMEBase import MIMEBase 
from email.MIMEText import MIMEText 
from email.Utils import COMMASPACE, formatdate 
from email import Encoders 

username = sys.argv[1] 
hostname = sys.argv[2] 
port = sys.argv[3] 
from_addr = sys.argv[4] 
to_addr = sys.argv[5] 
subject = sys.argv[6] 
text = sys.argv[7] 

password = getpass.getpass() if sys.stdin.isatty() else sys.stdin.readline().rstrip('\n') 

message = MIMEMultipart() 
message['From'] = from_addr 
message['To'] = to_addr 
message['Date'] = formatdate(localtime=True) 
message['Subject'] = subject 
#message['Cc'] = COMMASPACE.join(cc) 
message.attach(MIMEText(text)) 

i = 0 
for file in sys.argv: 
    if i > 7: 
     part = MIMEBase('application', 'octet-stream') 
     part.set_payload(open(file, 'rb').read()) 
     Encoders.encode_base64(part) 
     part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file)) 
     message.attach(part) 
    i = i + 1 

smtp = smtplib.SMTP(hostname,port) 
smtp.starttls() 
smtp.login(username, password) 
del password 

smtp.sendmail(from_addr, to_addr, message.as_string()) 
smtp.close() 

而且我把它形成這個方法使用發送電子郵件Gmail帳戶

- (bool) sendEmail:(NSTask *) task toAddress:(NSString *) toAddress withSubject:(NSString *) subject Attachments:(NSArray *) attachments { 

     NSLog(@"Trying to send email message"); 
     //set arguments including attachments 
     NSString *username = @"[email protected]"; 
     NSString *hostname = @"smtp.gmail.com"; 
     NSString *port = @"587"; 
     NSString *fromAddress = @"[email protected]"; 
     NSString *bodyText = @"Body text \n\r"; 
     NSMutableArray *arguments = [NSMutableArray arrayWithObjects: 
            programPath, 
            username, 
            hostname, 
            port, 
            fromAddress, 
            toAddress, 
            subject, 
            bodyText, 
            nil]; 
     for (int i = 0; i < [attachments count]; i++) { 
      [arguments addObject:[attachments objectAtIndex:i]]; 
     } 

     NSData *passwordData = [@"myGmailPassword" dataUsingEncoding:NSUTF8StringEncoding]; 


     NSDictionary *environment = [NSDictionary dictionaryWithObjectsAndKeys: 
            @"", @"PYTHONPATH", 
            @"/bin:/usr/bin:/usr/local/bin", @"PATH", 
            nil]; 
     [task setEnvironment:environment]; 
     [task setLaunchPath:@"/usr/bin/python"]; 

     [task setArguments:arguments]; 

     NSPipe *stdinPipe = [NSPipe pipe]; 
     [task setStandardInput:stdinPipe]; 

     [task launch]; 

     [[stdinPipe fileHandleForReading] closeFile]; 
     NSFileHandle *stdinFH = [stdinPipe fileHandleForWriting]; 
     [stdinFH writeData:passwordData]; 
     [stdinFH writeData:[@"\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [stdinFH writeData:[@"Description" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [stdinFH closeFile]; 

     [task waitUntilExit]; 

     if ([task terminationStatus] == 0) { 
      NSLog(@"Message successfully sent"); 
      return YES; 
     } else { 
      NSLog(@"Message not sent"); 
      return NO; 
     } 
    } 

我希望它有幫助

+0

我試過你的代碼,但問題是,當你啓動它,它說programPath沒有定義。現在我確實將它設置爲使用NSString的程序的路徑,但仍然沒有工作......我該怎麼辦? – 2012-02-14 08:01:51

+0

這是腳本的路徑,並定義爲:programPath = [[[NSBundle bundleForClass:[self class]] pathForResource:@「emailSender」ofType:@「py」] copy]; – Tibidabo 2012-02-15 01:13:55

+0

我在名爲emailSender.py的項目中添加了python的資源代碼。還寫了代碼\t NSBundle * programPath = [[[NSBundle bundleForClass:[self class]] pathForResource:@「emailSender」ofType:@「py」] copy]; 所有錯誤都消失了,但沒有發送電子郵件。控制檯顯示這些細節。請幫忙 !!! – 2012-02-15 09:45:06