集成邮件发送功能MFMailComposeViewController

来源:互联网 发布:删除主键sql 编辑:程序博客网 时间:2024/06/16 08:59

注意:用这个类的话 必须苹果设备邮件客户端 登录任意一个邮箱否则无法使用

#import "ViewController.h"

#import <MessageUI/MessageUI.h>

@interface ViewController ()<MFMailComposeViewControllerDelegate>

@end


@implementation ViewController


- (void)viewDidLoad

{

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(0, 40, 320, 50);

    [button setTitle:@"Mail"forState: UIControlStateNormal];

    [button addTarget:selfaction: @selector(sendEMail)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview: button];

}



//点击按钮后,触发这个方法

-(void)sendEMail

{

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

    

    if (mailClass != nil)

    {

        if ([mailClass canSendMail])

        {

            [selfdisplayComposerSheet];

        }

        else

        {

            [selflaunchMailAppOnDevice];

        }

    }

    else

    {

        [selflaunchMailAppOnDevice];

    }

}

//可以发送邮件的话

-(void)displayComposerSheet

{

    MFMailComposeViewController *mailPicker = [[MFMailComposeViewControlleralloc]init];

    

    mailPicker.mailComposeDelegate =self;

    

    //设置主题

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

    

    // 添加发送者

    NSArray *toRecipients = [NSArrayarrayWithObject:@"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 = [UIImageimageNamed:@"123.png"];

    NSData *imageData = UIImagePNGRepresentation(addPic);            // png

    // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg

    [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.png"];

    

    NSString *emailBody = @"eMail 正文";

    [mailPicker setMessageBody:emailBody isHTML:YES];

    

    [selfpresentViewController:mailPickeranimated:YEScompletion:nil];

}


-(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 = [NSStringstringWithFormat:@"%@%@", recipients, body];

    NSString *charactersToEscape =@"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";

    NSCharacterSet *allowedCharacters = [[NSCharacterSetcharacterSetWithCharactersInString:charactersToEscape]invertedSet];

    email = [email stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];

    

    [[UIApplicationsharedApplication]openURL: [NSURLURLWithString:email]];

}


- (void)mailComposeController:(MFMailComposeViewController *)controller

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

{

    NSString *msg;

    

    switch (result)

    {

        caseMFMailComposeResultCancelled:

            msg = @"邮件发送取消";

            break;

        caseMFMailComposeResultSaved:

            msg = @"邮件保存成功";

            [self alertWithTitle:nil msg:msg];

            break;

        caseMFMailComposeResultSent:

            msg = @"邮件发送成功";

            [self alertWithTitle:nil msg:msg];

            break;

        caseMFMailComposeResultFailed:

            msg = @"邮件发送失败";

            [self alertWithTitle:nil msg:msg];

            break;

        default:

            break;

    }

    [selfdismissViewControllerAnimated:YEScompletion:nil];

}


- (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg

{

    UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:_title_

                                                    message:msg

                                                   delegate:nil

                                          cancelButtonTitle:@"确定"

                                          otherButtonTitles:nil];

    [alert show];

}

阅读全文
0 0