菜鸟:IOS 学习:磨难记 (第七天)
2013-01-21 11:09
302 查看
一、开发邮件发送DEMO
XCODE4.5 IOS6.0 iphone版由于没有真机环境,模拟器正常,但需要真机测试是否能发送,那位兄弟帮帮忙二、
简单版,主视图一个发送按钮,
子视图,标题,收件人,CC,BCC, 标题,内容
MailAppViewController.h
#import <UIKit/UIKit.h> #import <MessageUI/MessageUI.h> //发送邮件框架 @interface MailAppViewController : UIViewController<MFMailComposeViewControllerDelegate> @property(nonatomic, retain) NSString *setSubject; //标题 @property(nonatomic, retain) NSArray *toRecipients; //收件人 @property(nonatomic, retain) NSString *myImagePath; //邮件中的图片 @property(nonatomic, retain) NSString *emailBody; //邮件内容 -(IBAction)openMail:(id)sender; -(void)setMailDefault; @end
MailAppViewController.m
#import "MailAppViewController.h" @interface MailAppViewController () @end @implementation MailAppViewController @synthesize setSubject; @synthesize toRecipients; @synthesize myImagePath; @synthesize emailBody; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /** * Description of Imgauthcode * outlet监控视图入口 * @author fantom * @time 2013-01-21 */ - (IBAction)openMail:(id)sender { @try { [self sendMail]; }@catch (NSException *e) { [self mailLog:e]; } } /** * Description of Imgauthcode * 发送邮件 * @author fantom * @time 2013-01-21 */ -(void)sendMail { @try { if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; //初始化数据 mailer.mailComposeDelegate = self; [self setMailDefault]; [mailer setSubject:self.setSubject]; //标题 [mailer setToRecipients:self.toRecipients]; //收件人 UIImage *myImage = [UIImage imageNamed:self.myImagePath]; NSData *imageData = UIImagePNGRepresentation(myImage);//邮件内容图片 [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"mobiletutsImage"]; [mailer setMessageBody:self.emailBody isHTML:NO];//邮件内容 [self presentModalViewController:mailer animated:YES]; //显示视图 [mailer release]; } else //提示无法使用邮箱功能,提示用户 { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"失败" message:@"您的邮件功能暂时无法使用" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } }@catch (NSException *e) { [self mailLog:e]; } } /** * Description of Imgauthcode * 配置默认参数 * @author fantom * @time 2013-01-21 */ -(void)setMailDefault { @try { self.setSubject = @"测试IOS邮件发送"; self.toRecipients = [NSArray arrayWithObjects:@"gift33@163.com", @"gift33@sina.com", nil]; self.myImagePath = @"logo.png"; self.emailBody = @"测试一下是否发送成功"; }@catch (NSException *e) { [self mailLog:e]; } } /** * Description of Imgauthcode * 关闭发送按钮提示 * @author fantom * @time 2013-01-21 */ - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { @try { NSString *message = @"发送中,请等待"; switch (result) { case MFMailComposeResultCancelled: message = @"Mail cancelled: 取消了操作,没有电子邮件被排队."; break; case MFMailComposeResultSaved: message = @"Mail saved: 保存在”草稿“文件夹中."; break; case MFMailComposeResultSent: message = @"Mail send: 发送队列中。已准备好发送"; break; case MFMailComposeResultFailed: message = @"Mail failed:某个错误无法添加到队列中:无法添加到队列中"; break; default: message = @"Mail not sent: 邮箱无法发送."; break; } NSLog(@"%@", message); //提示信息 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; // Remove the mail view [self dismissModalViewControllerAnimated:YES]; }@catch (NSException *e) { [self mailLog:e]; } } /** * Description of Imgauthcode * 错误日志处理 * @author fantom * @time 2013-01-21 */ -(void)mailLog:(NSException *) e { NSLog(@"main : %@ %@", [e name], [e reason]); } @end
下载包:
MailApp.zip
相关文章推荐
- 菜鸟:IOS 学习:磨难记 (第五天)
- 菜鸟:IOS 学习:磨难记 (第三天)
- 菜鸟:IOS 学习:磨难记 (第四天)
- 菜鸟:IOS 学习:磨难记 (第六天)
- 菜鸟:IOS 学习:磨难记(第一天)
- 菜鸟:IOS 学习:磨难记 (第二天)
- IOS开发---菜鸟学习之路--(二十三)-直接利用键值对的方式来处理数据的感想
- iOS 学习小菜鸟之蓝牙corebluetooth
- IOS开发---菜鸟学习之路--(四)-登陆界面
- [iOS菜鸟的学习之路]定位功能的实现
- IOS开发---菜鸟学习之路--(十九)-利用NSUserDefaults存储数据
- iOS菜鸟学习——UIButton响应传参数
- 今天开通博客,记录我作为一个小菜鸟在iOS学习中的点点滴滴
- 菜鸟学习ios
- IOS 学习第七天 C语言 数组
- iOS菜鸟学习——图片resize的实现
- IOS开发---菜鸟学习之路--(十五)-如何实现拍照功能
- IOS开发---菜鸟学习之路--(十六)-将Image转换为Base64
- IOS开发---菜鸟学习之路--(二十)-二维码扫描功能的实现
- iOS菜鸟学习--如何避免两个按钮同时响应