添加MessageUI.framework库
#import <MessageUI/MFMailComposeViewController.h>
MFMailComposeViewControllerDelegate
//
// YBMViewController.m
// qweqw
//
// Created by K on 14-12-4.
// Copyright (c) 2014年 K. All rights reserved.
//
#import "YBMViewController.h"
@interface YBMViewController ()
@end
@implementation YBMViewController
- (void)viewDidLoad
{
//1,添加messageui。framework
//2..h文件中添加MFMailComposeViewControllerDelegate代理
//3.添加以下方法 修改出已标明
//4连真机(真机必须有邮箱登录,因为发邮件实际上是通过设备绑定的邮箱发而不是程序发的)
[super 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)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主题"];
// 添加发送者 //修改1: 收件人地址
NSArray *toRecipients = [NSArray arrayWithObject: @"1246092111@qq.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];
// 添加图片 //修改2文件名字 (附件形式)
UIImage *addPic = [UIImage imageNamed: @"flower.png"];
NSData *imageData = UIImagePNGRepresentation(addPic); // png
// NSData *imageData = UIImageJPEGRepresentation(addPic, 1); // jpeg
//修改2文件名字 (附件形式)
[mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"flower.png"];
NSString *emailBody = @"eMail 正文";
[mailPicker setMessageBody:emailBody isHTML:YES];
[self presentViewController:mailPicker animated:YES completion:nil];
[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;
}
NSLog(@"%@",msg);
[self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// YBMViewController.m
// qweqw
//
// Created by K on 14-12-4.
// Copyright (c) 2014年 K. All rights reserved.
//
#import "YBMViewController.h"
@interface YBMViewController ()
@end
@implementation YBMViewController
- (void)viewDidLoad
{
//1,添加messageui。framework
//2..h文件中添加MFMailComposeViewControllerDelegate代理
//3.添加以下方法 修改出已标明
//4连真机(真机必须有邮箱登录,因为发邮件实际上是通过设备绑定的邮箱发而不是程序发的)
[super 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)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主题"];
// 添加发送者 //修改1: 收件人地址
NSArray *toRecipients = [NSArray arrayWithObject: @"1246092111@qq.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];
// 添加图片 //修改2文件名字 (附件形式)
UIImage *addPic = [UIImage imageNamed: @"flower.png"];
NSData *imageData = UIImagePNGRepresentation(addPic); // png
// NSData *imageData = UIImageJPEGRepresentation(addPic, 1); // jpeg
//修改2文件名字 (附件形式)
[mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"flower.png"];
NSString *emailBody = @"eMail 正文";
[mailPicker setMessageBody:emailBody isHTML:YES];
[self presentViewController:mailPicker animated:YES completion:nil];
[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;
}
NSLog(@"%@",msg);
[self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Resource Reference