开源SMTP协议实现 skpsmtpmessage
源码地址:skpsmtpmessage 是由Skorpiostech, Inc.为我们带来的一个SMTP协议的开源实现,使用Objective-c 实现,iOS系统的项目可以直接调用 =]
googlecode http://code.google.com/p/skpsmtpmessage/github:svn checkout http://skpsmtpmessage.googlecode.com/svn/trunk/ skpsmtpmessage-read-onlygit clone https://github.com/kailoa/iphone-smtp.git
skpsmtpmessage能够实现不使用iOS系统提供的发送email的两种方法,发送email的功能。
iOS系统框架提供的两种发送Email的方法
使用openURL来实现发邮件的功能:
NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
#import <MessageUI/MFMailComposeViewController.h>
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"My Subject"];
[controller setMessageBody:@"Hello there." isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
if (result == MFMailComposeResultSent) {
NSLog(@"It's away!");
}
[self dismissModalViewControllerAnimated:YES];
}
enum MFMailComposeResult {
MFMailComposeResultCancelled,//用户取消编辑邮件
MFMailComposeResultSaved,//用户成功保存邮件
MFMailComposeResultSent,//用户点击发送,将邮件放到队列中
MFMailComposeResultFailed//用户试图保存或者发送邮件失败
};
typedef enum MFMailComposeResult MFMailComposeResult; // iOS3.0以上有效
+ (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
//如果用户没有设置邮件账户,则会返回NO,你可以用根据返回值来决定是使用MFMailComposeViewController 还是 mailto://的传统方法,也或者,你可以选择上文中提到的skpsmtpmessage来实现发送Email的功能。
- (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename;
//NSData类型的attachment自然不必多说,关于mimeType需要一点说明,官方文档里给出了一个链接http://www.iana.org/assignments/media-types/ ,这里列出的所有的类型都应该支持。关于mimeType的用处,更多需要依靠搜索引擎了 =]
因此在这里使用第三方的发送邮件的实现就比较合适,我们可以根据自己的UI设计需求来定制相应的视图以适应整体的设计。
在SKPSMTPMessage类中,并没有对视图进行任何的要求,它提供的都是数据层级的处理,你之需要定义好相应的发送要求就可以实现邮件发送了。至于是以什么样的方式获取这些信息,就可以根据软件的需求来确定交互方式和视图样式了。
SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
testMsg.fromEmail = @"test@gmail.com";
testMsg.toEmail =@"to@gmail.com";
testMsg.relayHost = @"smtp.gmail.com";
testMsg.requiresAuth = YES;
testMsg.login = @"test@gmail.com";
testMsg.pass = @"test";
testMsg.subject = [NSString stringWithCString:"测试" encoding:NSUTF8StringEncoding];
testMsg.bccEmail = @"bcc@gmail.com";
testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS!
// Only do this for self-signed certs!
// testMsg.validateSSLChain = NO;
testMsg.delegate = self;
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
[NSString stringWithCString:"测试正文" encoding:NSUTF8StringEncoding],kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];
NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath];
NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,
@"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];
[testMsg send];
-(void)messageSent:(SKPSMTPMessage *)message;
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error;
沒有留言:
張貼留言