iOS發(fā)送電子郵件

2018-11-21 17:03 更新

IOS發(fā)送電子郵件


簡介

我們可以使用IOS設(shè)備中的電子郵件應(yīng)用程序發(fā)送電子郵件。

實(shí)例步驟

1、創(chuàng)建一個(gè)簡單的View based application

2、選擇項(xiàng)目文件,然后選擇目標(biāo),然后添加MessageUI.framework

3、在ViewController.xib中添加一個(gè)按鈕,創(chuàng)建用于發(fā)送電子郵件的操作(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

輸出

當(dāng)運(yùn)行該應(yīng)用程序,會(huì)看如下的輸出結(jié)果

iOS發(fā)送電子郵件

當(dāng)點(diǎn)擊"send email"發(fā)送按鈕后,可以看到如下結(jié)果:

iOS發(fā)送電子郵件


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號