iOS 发送邮件MFMailComposeViewController

发送邮件使用MFMailComposeViewController来显示界面


1、添加框架、导入头文件,及设置代理

1-1MessageUi.framework

1-2#import <MessageUI/MessageUI.h>

1-3MFMailComposeViewControllerDelegate


2、发送邮件调用方法

- (void)sendEMail

{

    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

    

    if (mailClass)

    {

        if ([mailClass canSendMail])

        {

            [self displayComposerSheet];

        }

        else

        {

            [self launchMailAppOnDevice];

        }

    }

    else

    {

        [self launchMailAppOnDevice];

    }

}


// 可以发送邮件的话

- (void)displayComposerSheet

{

    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];

    mailPicker.mailComposeDelegate = self;

    

    // 设置主题

    [mailPicker setSubject:@"eMail主题"];

    

    // 添加发送者

    NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.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 = @"eMail 正文";

    [mailPicker setMessageBody:emailBody isHTML:YES];

    

    [self presentModalViewController:mailPicker animated:YES];

}


- (void)launchMailAppOnDevice

{

    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]];

}


3、代理回调方法

- (void)mailComposeController:(MFMailComposeViewController *)controller

didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error

{

    switch (result)

    {

        case MFMailComposeResultCancelled:NSLog(@"邮件发送取消");break;

        case MFMailComposeResultSaved:NSLog(@"邮件保存成功");break;

        case MFMailComposeResultSent:NSLog(@"邮件发送成功");break;

        case MFMailComposeResultFailed:NSLog(@"邮件发送失败");break;

        default:break;

    }

    

    [self dismissModalViewControllerAnimated:YES];

}


转载于:https://my.oschina.net/potato512/blog/647720

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值