我将如何能够使用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. PHP发送电子邮件多次发送电子邮件
- 3. 通过可可应用程序发送电子邮件
- 4. vb.net发送电子邮件
- 5. aspx:发送电子邮件
- 6. Django发送电子邮件
- 7. 发送HTML电子邮件
- 8. 发送电子邮件?
- 9. VB.Net电子邮件发送
- 10. 发送电子邮件
- 11. PHP发送电子邮件
- 12. 笨 - 发送电子邮件
- 13. IIS7 - 发送电子邮件
- 14. 发送电子邮件
- 15. 如何提高电子邮件发送和传送可靠性?
- 16. 电子邮件不发送
- 17. django发送电子邮件
- 18. SpringMVC发送电子邮件
- 19. 发送电子邮件从
- 20. 发送电子邮件
- 21. 发送电子邮件nodemailer
- 22. 发送电子邮件
- 23. 发送电子邮件
- 24. SMTP:发送电子邮件
- 25. PHP发送电子邮件
- 26. 发送电子邮件
- 27. MFMailComposeViewController发送电子邮件
- 28. Codeigniter - 发送电子邮件
- 29. PHP发送电子邮件
- 30. 发送电子邮件C#
请注意,沙盒应用程序不允许使用Scripting Bridge。 – jemeshsu 2012-01-05 11:15:13
这是不正确的;你可以使用正确的授权脚本桥 – 2014-08-21 08:34:39