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

ios如何利用系统邮件发送邮件

2015-10-19 17:33 543 查看
#pragma mark - 在应用内发送邮件
//调出邮件发送窗口
- (void)showMailPicker:(NSInteger)tag
{
if ([MFMailComposeViewController canSendMail]){
[self displayMailComposerSheet:tag];
}else{
NSLog(@"Device not configured to send mail");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请配置邮件"
message:@"请先设置邮件账户:'设置'->'邮件'->'添加帐号'"
delegate:self
cancelButtonTitle:@"好"
otherButtonTitles:nil,nil];
[alert show];
//iOS8打开设定界面
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
//    return;
}

}

- (void)displayMailComposerSheet:(NSInteger)tag
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
//    [picker setSubject:@"eMail主题"];

//    // Set up recipients
//    NSString *email=detailArray[tag];
//    NSArray *toRecipients = [NSArray arrayWithObject:email];
//    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"1528693256@qq.com", nil];
//    //NSArray *bccRecipients = [NSArray arrayWithObject:@"1528693256@qq.com"];
//
//    [picker setToRecipients:toRecipients];//添加收件人
//    //[picker setCcRecipients:ccRecipients];//添加抄送
//    //[picker setBccRecipients:bccRecipients];//添加密送
//
//    // Attach an image to the email
//    //NSString *path = [[NSBundle mainBundle] pathForResource:@"swift" ofType:@"png"];
//    //NSData *myData = [NSData dataWithContentsOfFile:path];
//    //[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"swift.png"];
//
//    // 邮件正文
//    NSString *emailBody = @"测试";
//    [picker setMessageBody:emailBody isHTML:NO];

if (tag==4) {
[picker setSubject:@"商务合作"];
NSString *email=detailArray[tag];
NSArray *toRecipients = [NSArray arrayWithObject:email];
[picker setToRecipients:toRecipients];//添加收件人
// 邮件正文
NSString *emailBody = @"内容: \n联系方式:";
[picker setMessageBody:emailBody isHTML:NO];
} else if(tag==5){
[picker setSubject:@"应聘(XXX职位)"];
NSString *email=detailArray[tag];
NSArray *toRecipients = [NSArray arrayWithObject:email];
[picker setToRecipients:toRecipients];//添加收件人
// 邮件正文
NSString *emailBody = @"内容:你好,我想加入飞磨的大家庭,成为飞磨公司的(XXX职位)\n个人简介:\n工作经验:";
[picker setMessageBody:emailBody isHTML:NO];
}

[self presentViewController:picker animated:YES completion:NULL];
}

#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
//关闭邮件发送窗口
[self dismissViewControllerAnimated:YES completion:^{

}];
NSString *msg;
switch (result) {
case MFMailComposeResultCancelled:
msg = @"用户取消编辑邮件";
break;
case MFMailComposeResultSaved:
msg = @"用户成功保存邮件";
break;
case MFMailComposeResultSent:
msg = @"用户点击发送,将邮件放到队列中,还没发送";
break;
case MFMailComposeResultFailed:
msg = @"用户试图保存或者发送邮件失败";
break;
default:
msg = @"";
break;
}
//APP_ALERT_SHOW_TOP(msg);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"邮件提示"
message:msg
delegate:self
cancelButtonTitle:@"我知道了"
otherButtonTitles:nil,nil];
[alert show];

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