自己的软件里面需要用到,先准备一下知识点。
http://www.cocoachina.com/newbie/basic/2011/1226/3782.html
这篇文章,描述了三种方法
1、使用openURL来实现发邮件的功能:
http://www.istar.name/blog/ios-send-email
2、使用MFMailComposeViewController来实现发邮件的功能,它在MessageUI.framework中,你需要在项目中加入该框架,并在使用的文件中导入MFMailComposeViewController.h头文件。
3、我们可以根据自己的UI设计需求来定制相应的视图以适应整体的设计。可以使用比较有名的开源SMTP协议来实现。
在SKPSMTPMessage类中,并没有对视图进行任何的要求,它提供的都是数据层级的处理,你之需要定义好相应的发送要求就可以实现邮件发送了。至于是以什么样的方式获取这些信息,就可以根据软件的需求来确定交互方式和视图样式了。
这篇文章详细描述了第二种方法,因为提到了附近,而我正好是想要用到附近,所以也加在这里。
1. 添加MessageUI.framework
2. 在头文件中引用并添加委托
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface LoginViewController : UIViewController <MFMailComposeViewControllerDelegate>
@end
|
3. 实现代码:
#import "LoginViewController.h"
@implementation LoginViewController
//send email
- (IBAction)contact:(id)sender{
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
if
([MFMailComposeViewController canSendMail]) {
[mc setToRecipients:[NSArray arrayWithObjects:@
"me@istar.name"
, nil]];
[self presentModalViewController:mc animated:YES];
[mc release];
}
}
//the delegate
- (
void
)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error {
switch
(result)
{
case
MFMailComposeResultCancelled:
NSLog(@
"Mail send canceled..."
);
break
;
case
MFMailComposeResultSaved:
NSLog(@
"Mail saved..."
);
break
;
case
MFMailComposeResultSent:
NSLog(@
"Mail sent..."
);
break
;
case
MFMailComposeResultFailed:
NSLog(@
"Mail send errored: %@..."
, [error localizedDescription]);
break
;
default
:
break
;
}
[self dismissModalViewControllerAnimated:YES];
}
@end
|
4. 使用说明:
(1)创建视图控制器:
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
|
(2)设置邮件主题:
[mc setSubject:@
"Hello, World!"
];
|
(3)设置收件人,收件人有三种:
//设置主收件人
[mc setToRecipients:[NSArray arrayWithObjects:@
"me@istar.name"
,
"@dave@iphonedevbook.com"
, nil];
//设置cc
[mc setCcRecipients:[NSArray arrayWithObject:@
"me@istar.name"
]];
//设置bcc
[mc setBccRecipients:[NSArray arrayWithObject:@
"me@istar.name"
]];
|
(4)设置邮件主体,有两种格式。
//一种是纯文本
[mc setMessageBody:@
"Star!!!\n\nCome here, I miss you!"
isHTML:NO];
//一个是html格式
[mc setMessageBody:@
"<HTML><B>Hello, Star!</B><BR/>What do you know?</HTML>"
isHTML:YES];
|
(5)添加附件
//添加附件需要三个参数,一个是NSData类型的附件,一个是mime type,一个附件的名称。
NSString *path = [[NSBundle mainBundle] pathForResource:@
"blood_orange"
ofType:@
"png"
];
NSData *data = [NSData dataWithContentsOfFile:path];
[mc addAttachmentData:data mimeType:@
"image/png"
fileName:@
"blood_orange"
];
|
本文固定链接: http://www.istar.name/blog/ios-send-email | Star's Blog
本文介绍在iOS应用中实现邮件发送功能的方法,包括使用openURL、MFMailComposeViewController及自定义SMTP实现的方式。重点介绍了MFMailComposeViewController的具体实现过程,如添加MessageUI框架、配置邮件视图控制器、设置邮件主题、收件人等。

374

被折叠的 条评论
为什么被折叠?



