IOS使用MessageUI Framework 发送邮件

使用MessageUI Framework 中的 MFMailComposeViewController 发送邮件,另外其中的MFMessageComposeViewController用于发送短信。使用非常简单:

1. 导入框架:MessageUI.framework
2. 添加协议:<MFMailComposeViewControllerDelegate>

3. #import <MessageUI/MessageUI.h>

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
//设置委托
mc.mailComposeDelegate = self;
//设置邮件主题
[mc setSubject:@"Hello World!"];
//设置收件人
[mc setToRecipients:[NSArray arrayWithObjects:@"[email protected]"]];
//设置cc
[mc setCcRecipients:[NSArray arrayWithObjects:@"[email protected]"]];
//设置bcc
[mc setBccRecipients:[NSArray arrayWithObjects:@"[email protected]"]];
//设置邮件内容
//[mc setMessageBody:@"This is a test mail" isHTML:NO];
//设置邮件内容,支持HTML
[mc setMessageBody:@"<HTML><B>Hello, Joe!</B><BR/>What do you know?</HTML>" isHTML:YES];
//为邮件添加附件
NSString *path = [[NSBundle mainBundle] pathForResource:@"orange" ofType:@"png"];
NSData *data = [NSData dataWithContentsOfFile:path];
[mc addAttachmentData:data mimeType:@"image/png" fileName:@"orange"];
[self presentModalViewController:mc animated:YES];
[mc release];


#pragma mark -
#pragma mark Delegate Method
-(void)mailComposeController:(MFMailComposeViewController *)controller 
    didFinishWithResult:(MFMailComposeResult)result
    error:(NSError *)error
{
    switch(result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail send canceled...");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved...");
            break;
        case MFMailComposeResultSend:
            NSLog(@"Mail send...");
            break;
        case MFMailComposeResultFailed:    
            NSLog(@"Mail send errored:%@...",[error localizedDescription]);
            break;
        default:
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

你可能感兴趣的:(ios,Objective-C,MessageUI)