IOS發送電子郵件
IOS發送電子郵件
簡介
我們可以使用IOS設備中的電子郵件應用程式發送電子郵件。
實例步驟
1、建立一個簡單的View based application
2、選擇專案文件,然後選擇目標,然後新增MessageUI.framework
# 3.在ViewController.xib中新增一個按鈕,建立用於發送電子郵件的操作(action)
4、更新ViewController.h,如下所示
#import <UIKit/UIKit.h>#import <MessageUI/MessageUI.h>@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>{ MFMailComposeViewController *mailComposer;}-(IBAction)sendMail:(id)sender;@end
5、如下所示,更新ViewController.m
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; }- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}-(void)sendMail:(id)sender{ mailComposer = [[MFMailComposeViewController alloc]init]; mailComposer.mailComposeDelegate = self; [mailComposer setSubject:@"Test mail"]; [mailComposer setMessageBody:@"Testing message for the test mail" isHTML:NO]; [self presentModalViewController:mailComposer animated:YES];}#pragma mark - mail compose delegate-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ if (result) { NSLog(@"Result : %d",result); } if (error) { NSLog(@"Error : %@",error); } [self dismissModalViewControllerAnimated:YES];}@end
輸出
當運行該應用程序,會看如下的輸出結果
當點擊」send email"發送按鈕後,可以看到以下結果: