您的位置:首页 > 移动开发 > IOS开发

ios-发送邮件两种方式实现

2015-10-05 23:54 393 查看
代码的思路很清晰 比较简单
#import "ViewController.h"
//导入
#import <MessageUI/MessageUI.h>

@interface ViewController ()<MFMailComposeViewControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//发邮件
#if 0
//方法一:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:1143344@163.com"]];
#endif

//方法二:
//判断当前设置是否支持发送邮件
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
//添加收件人
[mailVC setToRecipients:@[@"1148828736@qq.com",@"test@163.com"]];
//添加抄送人
[mailVC setCcRecipients:@[@"1111@163.com"]];

//添加"密送人"
[mailVC setBccRecipients:@[@"22222@163.com"]];

//添加主题
[mailVC setSubject:@"百度招聘"];

//添加邮件内容
// [mailVC setMessageBody:@"<h1>该还钱了</h1><br><h4>该还钱了</h4>" isHTML:YES];
[mailVC setMessageBody:@"哈哈哈哈哈哈" isHTML:NO];

//添加附件
[mailVC addAttachmentData:UIImagePNGRepresentation([UIImage imageNamed:@"Transfer_Refunded"]) mimeType:@"image/png" fileName:@"test.png"];

//设置代理
mailVC.mailComposeDelegate = self;

[self presentViewController:mailVC animated:YES completion:nil];
}
else
{
NSLog(@"不支持发送邮件");
}

}

#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];

/*
MFMailComposeResultCancelled,  取消发送
MFMailComposeResultSaved,      保存
MFMailComposeResultSent,       发送成功
MFMailComposeResultFailed      发送失败
*/
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: