MFMailComposeViewController发送邮件的实例
来源:互联网 发布:java new object 赋值 编辑:程序博客网 时间:2024/06/06 03:23
- 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];
- }
- MFMailComposeViewController发送邮件的实例
- MFMailComposeViewController发送邮件的实例
- MFMailComposeViewController发送邮件的实例
- MFMailComposeViewController发送邮件的实例
- MFMailComposeViewController发送邮件的实例
- MFMailComposeViewController发送邮件的实例
- MFMailComposeViewController发送邮件的实例
- iOS开发 MFMailComposeViewController(发送邮件)的使用
- 发送邮件--MFMailComposeViewController
- 集成邮件发送功能MFMailComposeViewController
- 如何将MFMailComposeViewController的发送邮件界面设为中文
- 使用MFMailComposeViewController发送邮件时防止崩溃
- iOS开发笔记之二十五——MFMailComposeViewController发送邮件实例
- 发送邮件的实例
- MFMailComposeViewController 如何发送带有图片的html
- JavaMail 发送邮件的实例
- MFMailComposeViewController 发邮件
- javamail发送邮件的简单实例
- java向mysql数据库插入中文时乱码
- request.getRemoteAddr()获取的值为0:0:0:0:0:0:0:1
- 四屏选项卡
- PadLeft例子
- C#调用word打印
- MFMailComposeViewController发送邮件的实例
- js join方法
- UITableview cell上的数据或控件覆盖问题。
- fprintf
- WIN7安装IE9 未能完成安装
- Android对Bitmap的高效使用总结(Google给的bitmap终极方案)
- 基于mjpg-streamer远程视频WEB监控 项目笔记一
- POJ1088滑雪
- Binder 框架(一)