我將如何能夠使用Cocoa發送電子郵件?我會使用哪個框架,以及如何使用它。發送電子郵件 - 可可
回答
Apple's Developer Connection有一個名爲SBSendEmail的示例項目,演示瞭如何使用腳本橋將腳本發送到郵件應用程序。
您可以下載whole project並在XCode中運行它以查看其工作原理。特別感興趣的是您的sendEmailMessage:方法Controller.m
您可以使用打開默認mail client或者您可以使用framework。這應該讓你開始。
對於Growl 1.2,我寫了a Python-based mail-sending program,MailMe顯示使用NSTask運行。
我在很大程度上出於對可可的其他郵件框架的不滿,其中大部分郵件框架也支持接收郵件,這對於某些輸出(如MailMe)來說是不必要的。
退房:
http://www.collaboration-world.com/pantomime
我還沒看着它在好幾年了,但是當我在蘋果我主張我們包括它的操作系統,以取代郵件功能,我們會在NeXT/Apple轉型中失利。
我認爲使用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;
}
}
我希望它有幫助
我試過你的代碼,但問題是,當你啓動它,它說programPath沒有定義。現在我確實將它設置爲使用NSString的程序的路徑,但仍然沒有工作......我該怎麼辦? – 2012-02-14 08:01:51
這是腳本的路徑,並定義爲:programPath = [[[NSBundle bundleForClass:[self class]] pathForResource:@「emailSender」ofType:@「py」] copy]; – Tibidabo 2012-02-15 01:13:55
我在名爲emailSender.py的項目中添加了python的資源代碼。還寫了代碼\t NSBundle * programPath = [[[NSBundle bundleForClass:[self class]] pathForResource:@「emailSender」ofType:@「py」] copy]; 所有錯誤都消失了,但沒有發送電子郵件。控制檯顯示這些細節。請幫忙 !!! – 2012-02-15 09:45:06
- 1. 發送可可電子郵件
- 2. 發送電子郵件操作不發送電子郵件
- 3. PHP發送電子郵件多次發送電子郵件
- 4. SSRS訂閱 - 通過電子郵件發送至可變電子郵件地址
- 5. 是可以發送電子郵件沒有電子郵件服務器?
- 6. 通過可可應用程序發送電子郵件
- 7. 確定電子郵件發件人併發送電子郵件
- 8. vb.net發送電子郵件
- 9. aspx:發送電子郵件
- 10. 發送HTML電子郵件
- 11. Django發送電子郵件
- 12. 發送電子郵件Progrmmatically
- 13. 發送電子郵件
- 14. 發送電子郵件?
- 15. VB.Net電子郵件發送
- 16. 發送電子郵件
- 17. 發送電子郵件Godaddy
- 18. 笨 - 發送電子郵件
- 19. PHP發送電子郵件
- 20. IIS7 - 發送電子郵件
- 21. CodeIgnitor發送電子郵件
- 22. 發送電子郵件
- 23. 如何提高電子郵件發送和傳送可靠性?
- 24. 發送電子郵件proble
- 25. PHP發送電子郵件
- 26. 發送電子郵件nodemailer
- 27. 發送電子郵件
- 28. django發送電子郵件
- 29. SparkPost發送電子郵件
- 30. 發送電子郵件
請注意,沙盒應用程序不允許使用Scripting Bridge。 – jemeshsu 2012-01-05 11:15:13
這是不正確的;你可以使用正確的授權腳本橋 – 2014-08-21 08:34:39