转自:http://blog.csdn.net/bl1988530/article/details/6597230
MFMailComposeViewController发送邮件的实例
- iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面.
- 项目中需要添加MessageUi.framework。头文件加入MFMailComposeViewControllerDelegate。#import <MessageUI/MessageUI.h>
- sendMailViewController.m文件的实现:
- - (void)viewDidLoad
- {
- UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- button.frame = CGRectMake(0, 40, 320, 50);
- [button setTitle: @"Mail" forState: UIControlStateNormal];
- [button addTarget: self action: @selector(sendEMail) forControlEvents: UIControlEventTouchUpInside];
- [self.view addSubview: button];
- }
-
- - (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
- message:msg
- delegate:nil
- cancelButtonTitle:@"确定"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
-
- //点击按钮后,触发这个方法
- -(void)sendEMail
- {
- Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
-
- if (mailClass != nil)
- {
- if ([mailClass canSendMail])
- {
- [self displayComposerSheet];
- }
- else
- {
- [self launchMailAppOnDevice];
- }
- }
- else
- {
- [self launchMailAppOnDevice];
- }
- }
- //可以发送邮件的话
- -(void)displayComposerSheet
- {
- MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
-
- mailPicker.mailComposeDelegate = self;
-
- //设置主题
- [mailPicker setSubject: @"eMail主题"];
-
- // 添加发送者
- NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
- //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
- //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", nil];
- [mailPicker setToRecipients: toRecipients];
- //[picker setCcRecipients:ccRecipients];
- //[picker setBccRecipients:bccRecipients];
-
- // 添加图片
- UIImage *addPic = [UIImage imageNamed: @"123.jpg"];
- NSData *imageData = UIImagePNGRepresentation(addPic); // png
- // NSData *imageData = UIImageJPEGRepresentation(addPic, 1); // jpeg
- [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];
-
- NSString *emailBody = @"eMail 正文";
- [mailPicker setMessageBody:emailBody isHTML:YES];
-
- [self presentModalViewController: mailPicker animated:YES];
- [mailPicker release];
- }
- -(void)launchMailAppOnDevice
- {
- NSString *recipients = @"mailto:first@example.com&subject=my email!";
- //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";
- NSString *body = @"&body=email body!";
-
- NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
- email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
-
- [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
- }
- - (void)mailComposeController:(MFMailComposeViewController *)controller
- didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
- {
- NSString *msg;
-
- switch (result)
- {
- case MFMailComposeResultCancelled:
- msg = @"邮件发送取消";
- break;
- case MFMailComposeResultSaved:
- msg = @"邮件保存成功";
- [self alertWithTitle:nil msg:msg];
- break;
- case MFMailComposeResultSent:
- msg = @"邮件发送成功";
- [self alertWithTitle:nil msg:msg];
- break;
- case MFMailComposeResultFailed:
- msg = @"邮件发送失败";
- [self alertWithTitle:nil msg:msg];
- break;
- default:
- break;
- }
-
- [self dismissModalViewControllerAnimated:YES];
- }
分享到:
相关推荐
IOS 两种发送电子邮件示例代码,包含上传附件。
这是一个ios利用smtp发送邮件的sample。它使用了一个开源的框架,适合于不使用ios自带邮件发送的项目使用
如题,ios开发中调用系统进行邮件发送。模拟器,邮件发布出去。真机测试可以发送成功
完美解析系统发邮件功能,发送事件+发送成功结果事件委托
ios app异常处理可以发送邮件,ios app异常处理可以发送邮件的代码实现。
这是用来发送邮件的demo,需要真机
ios调用系统的API达到发送邮件的功能。
本文件代码主要教你如果发送电子邮件,相信对你是非常有的。
当IOS运行过程中产生异常,程序CRASH,自动将异常发送到指定邮箱。通过邮箱查看跟踪CRASH原因。
ios app发送邮件(必须先在iphone中设置发送邮件账号),代码的实现。
IOS通过两种方法发送Email的示例源代码。
IOS应用源码之使用MessageUI发送带附件的邮件 .zip
iOS,通过手机邮箱发送日志附件,代码示例
IOS应用源码——使用MessageUI发送带附加的邮件MailTest.rar
ios应用源码之使用messageui发送带附加的邮件mailtest .
IOS应用源码之邮件发送Document文件.zip
IOS应用源码——使用MessageUI发送带附加的邮件MailTest.zip
iOS发送邮件,sdk开放的接口是MFMailComposeViewController类库,功能都有,可惜的是ui不好定制,该类库的UI可能与自己的项目风格不太协调。利用 SMTPSender,用户可以自定义发送邮件的界面,只需要用到 SMTPSender...
iOS异常捕捉提示,发送邮件及写入异常日志。 提供 断言, 错误的具体位置(类名及行数)、所有异常报告内容、及异常报告文件路径接口。 并统一展示 异常日志 。
不同平台发送邮件方法IOS,Android,PC有问题请联系我