1、导入MessageUI.framework包
2、引入头文件#import <MessageUI/MessageUI.h>
3、实现代理MFMailComposeViewControllerDelegate
代码如下:
1、监测手机是否遇有首发邮件功能
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
2、可以发送邮件
-(void)displayComposerSheet
{
NSLog(@"可以发送邮件~~~~~~~~~~~~");
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
//设置主题
[mailPicker setSubject: @"eMail主题"];
// 添加发送者
NSArray *toRecipients = [NSArray arrayWithObject: @"243832207@qq.com"];
//NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
//NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", nil];
[mailPicker setToRecipients: toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];
// 添加图片
// UIImage *addPic = [UIImage imageNamed: @"123.jpg"];
// NSData *imageData = UIImagePNGRepresentation(addPic); // png
// NSData *imageData = UIImageJPEGRepresentation(addPic, 1); // jpeg
// [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];
NSString *emailBody = @"您好:您订阅的杂志,请点击一下连接进行支付!";
[mailPicker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController: mailPicker animated:YES];
[mailPicker release];
}
3、不能发送邮件
-(void)launchMailAppOnDevice
{
NSLog(@"不能发送邮件~~~~~~~~~~~~");
NSString *recipients = @"mailto:first@example.com&subject=my email!";
//@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";
NSString *body = @"&body=email body!";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
}
4、代理方法
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSString *msg;
switch (result)
{
case MFMailComposeResultCancelled:
msg = @"邮件发送取消";
break;
case MFMailComposeResultSaved:
msg = @"邮件保存成功";
[Utils alertWithTitle:nil message:msg];
break;
case MFMailComposeResultSent:
msg = @"邮件发送成功";
[Utils alertWithTitle:nil message:msg];
break;
case MFMailComposeResultFailed:
msg = @"邮件发送失败";
[Utils alertWithTitle:nil message:msg];
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
本文介绍如何在iOS应用中实现邮件发送功能,包括检测设备是否支持邮件发送、使用MFMailComposeViewController展示邮件编辑界面并设置邮件内容,以及在不支持直接发送的情况下通过URL Scheme启动邮件客户端。
2965

被折叠的 条评论
为什么被折叠?



