在程序中可能要发送邮件,我们自己完全可以实现一个邮件界面,然后自己写底层数据格式,实现发送邮件。但是iphone api中已经提供了系统写邮件界面的接口。实现的界面如下:

主要使用了MFMailComposeViewController,用来显示界面。
其中要在项目中添加下图的Frameworks。

实现代码:
#import "sendMailViewController.h"
@implementation sendMailViewController
-(IBAction)sendMail
{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// message.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
//message.text = @"Result: canceled";
break;
case MFMailComposeResultSaved:
//message.text = @"Result: saved";
break;
case MFMailComposeResultSent:
//message.text = @"Result: sent";
break;
case MFMailComposeResultFailed:
//message.text = @"Result: failed";
break;
default:
// message.text = @"Result: not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.title=@"New Message";
picker.mailComposeDelegate = self;
//[picker setSubject:@"New Message"];
// 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"];
// [picker setToRecipients:toRecipients];
// [picker setCcRecipients:ccRecipients];
// [picker setBccRecipients:bccRecipients];
// Attach an image to the email
//NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
// NSData *myData = [NSData dataWithContentsOfFile:path];
// [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy/MM/dd"];
NSDate *date=[[NSDate alloc] init];
NSString *content=[[NSString alloc]
initWithFormat:@"App:1.0.iOS:%@ Date:%@",
[[UIDevice currentDevice] systemVersion],[dateFormatter stringFromDate:date]];
[date release];
[dateFormatter release];
[picker setMessageBody:content isHTML:NO];
[content release];
[self presentModalViewController:picker animated:YES];
[picker release];
}
-(void)launchMailAppOnDevice
{
NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";
NSString *body = @"&body=It is raining in sunny California!";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
@end
源代码:http://easymorse-iphone.googlecode.com/svn/trunk/sendMail/