ios 发邮件

1.新建立一个项目,view based application(with storyboard.ARC)

2.需要引入UIMessage Library ,如下图

 

添加library

 

3.编辑ViewController.h文件

如下

 

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>

-(IBAction)sendMail:(id)sender;
@end

4.在ViewController.m文件中添加如下方法

 

-(IBAction)sendMail:(id)sender
{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:@"Apple , Welcome to china."];
NSArray *toRecipients = [NSArray arrayWithObjects:@"bu.xianggang@gmail.com", nil];
[mail setToRecipients:toRecipients];
[mail setCcRecipients:toRecipients];

NSString *emailBody = @”hello apple”;

[mail setMessageBody:emailBody isHTML:NO];
mail.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:mail animated:YES];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”ERROR” message:@”Email is not supported on your device” delegate:self cancelButtonTitle:@”Cancel” otherButtonTitles: nil];
[alert show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@”Cancelled”);
break;
case MFMailComposeResultSaved:
NSLog(@”Message saved in drafts folder”);
break;

case MFMailComposeResultSent:
NSLog(@”Message is sent”);
break;

case MFMailComposeResultFailed:
NSLog(@”Message not sent due to error”);
break;
default:
NSLog(@”Mail not sent”);
break;
}
[self dismissModalViewControllerAnimated:YES];
}

OK,大功搞成.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值