在ios开发中,电子邮件的发送,看起来是很简单的

只要使用这个MFMailComposeViewControllerDelegate代理就好了

同时还有调用#import <MessageUI/MessageUI.h>这个库

演示一下吧

MailViewController.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
// MailViewController.h
// Mail
//
// Created by david on 13-8-2.
// Copyright (c) 2013年 WalkerFree. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface MailViewController : UIViewController<MFMailComposeViewControllerDelegate, UINavigationBarDelegate>
- (IBAction)showMailPicker:(id)sender;

@end

MailViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// MailViewController.m
// Mail
//
// Created by david on 13-8-2.
// Copyright (c) 2013年 WalkerFree. All rights reserved.
//

#import "MailViewController.h"

@interface MailViewController ()

@end

@implementation MailViewController

- (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.
}



- (IBAction)showMailPicker:(id)sender {
if([MFMailComposeViewController canSendMail]){
[self displayMailComposerSheet];
}else{
NSLog(@"Device not configured to send SMS.");
}

}

- (void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Hello from California!"];

// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"];

[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];

// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];

// Fill out the email body text
NSString *emailBody = @"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];

[self presentViewController:picker animated:YES completion:NULL];
}

#pragma mark - delegate Methods

- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Result: Mail sending canceled") ;
break;
case MFMailComposeResultSaved:
NSLog(@"Result: Mail saved") ;
break;
case MFMailComposeResultSent:
NSLog(@"Result: Mail sent") ;
break;
case MFMailComposeResultFailed:
NSLog(@"Result: Mail sending failed") ;
break;
default:
NSLog(@"Result: Mail not sent") ;
break;
}

[self dismissViewControllerAnimated:YES completion:NULL];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end

运行一下就可以了。如果提示“Device not configured to send SMS.”

那么请在设置中打开邮件那个选项吧