2009-09-08 5 views
6

どのように私はココアを使用して電子メールを送信できますか?どのフレームワークを使用し、どのように使用するのか。Eメールを送信 - Cocoa

答えて

8

AppleのDeveloper Connectionには、Scripting Bridgeを使用してメールアプリケーションにスクリプトを送信する方法を示すSBSendEmailというサンプルプロジェクトがあります。

whole projectをダウンロードしてXCodeで実行すると、その動作を確認できます。特に興味があるのは、sendEmailMessage:メソッドです。Controller.m

+5

スクリプトブリッジはサンドボックスアプリケーションでは使用できません。 – jemeshsu

+0

これは正しくありません。 –

4

デフォルトのmail clientを開くか、frameworkを使用できます。これはあなたを始めるはずです。

+0

どちらも私にとっては役に立たない。誰か私に例を与えることができますか?申し訳ありませんが、私はこれに新しいですか? – lab12

+0

iPhoneまたはデスクトップ用に開発していますか? – Garrett

+0

私はデスクトップ上でXcode 3を使って開発しています。2 – lab12

2

For Growl 1.2では、MailMeディスプレイがNSTaskを使用して実行するa Python-based mail-sending programを書きました。

これは、Cocoaの他のメールフレームワークとの不満の大部分を取り除きました。そのほとんどはMailMeのような出力専用では不要なメールの受信もサポートしています。

0

チェックアウト:

http://www.collaboration-world.com/pantomime

私は数年でそれを見ていないが、私はアップルにいたとき、私たちは私たちがしたいメール機能を置き換えるためにOSでそれを含めることを提唱NeXT /アップル移行時に失われた

15

私はAppleスクリプトを使用するのが最も簡単なソリューションだと思います。ユーザーがMailにアカウントを設定しているときに使用します。

アップルスクリプト:

添付ファイルは、ファイルパスを含むスティールの配列です。あなたは、メッセージが

- (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"); 

}

デメリットを送信する前にポップアップ表示したい場合は

「をtrueに見える設定」:ユーザーがメールの下で有効なアカウントを持っている必要があります。

Pythonスクリプト(メールでのユーザーアカウントがない場合): また、Pythonスクリプトを使用してメッセージを送信することもできます。 デメリット:メールからメールを取得しないと(上記のリンゴスクリプトを使用することはできません)、またはアプリケーションに信頼できるSMTPリレーをハードコードする必要がある場合、SMTPの詳細を入力する必要があります(Gmailアカウントを設定して使用できます使用して電子メールを送信するには、このメソッドを形成

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() 

そして、私はそれを呼び出す:あなたのアプリがあまりにも多くの電子メールを送信する場合には、その目的のために、しかし、Googleはアカウント(SPAM))
が、私はこのPythonスクリプトを使用を削除することができます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

私はURコードを試しましたが、問題がUを起動するとprogramPath isntが定義されているということです。今私はNSStringを使用して私のプログラムのパスに設定しましたが、まだdidntの仕事...私は何をすべきですか??? –

+0

これはスクリプトのパスで、次のように定義されています:programPath = [[[NSBundle bundleForClass:[self class]] pathForResource:@ "emailSender" ofType:@ "py"] copy; – Tibidabo

+0

私はemailSender.pyという名前のプロジェクトにpythonのリソースコードを追加しました。コードを書きました\t NSBundle * programPath = [[[NSBundle bundleForClass:[self class]] pathForResource:@ "emailSender" ofType:@ "py"] copy; エラーはすべて消えていますが、電子メールは送信されません。コンソールにこれらの詳細が表示されます。助けてください !!! –