ios 메 일 발송

4154 단어 메 일 발송
ios 메 일 을 보 내 려 면 MessageUI. framework 를 도입 해 야 합 니 다. 장치 에 보 내 는 사람의 메 일 주 소 를 설정 해 야 합 니 다.예제 코드 는 다음 과 같다.
//
//  ViewController.h
//  EmailTest
//
//  Created by Dwen on 13-2-25.
//  Copyright (c) 2013  Dwen. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>
- (IBAction)sendMail:(id)sender;

@end

 
//
//  ViewController.m
//  EmailTest
//
//  Created by Dwen on 13-2-25.
//  Copyright (c) 2013  Dwen. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

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

- (IBAction)sendMail:(id)sender {
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (!mailClass) {
        NSLog(@"              ,    mailto    ");
        return;
    }
    [self displayMailPicker];
}

-(void)displayMailPicker{
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    mailPicker.mailComposeDelegate = self;
    
    //    
    [mailPicker setSubject: @"eMail  "];
    //     
    NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"];
    [mailPicker setToRecipients: toRecipients];
    //    
//    NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
//    [mailPicker setCcRecipients:ccRecipients];
    //    
//    NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];
//    [mailPicker setBccRecipients:bccRecipients];
    
//    //       
//    UIImage *addPic = [UIImage imageNamed: @"[email protected]"];
//    NSData *imageData = UIImagePNGRepresentation(addPic);            // png
//    //  mimeType:http://www.iana.org/assignments/media-types/index.html
//    [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
//    
//    //    pdf  
//    NSString *file = [self fullBundlePathFromRelativePath:@"   C++    .pdf"];
//    NSData *pdf = [NSData dataWithContentsOfFile:file];
//    [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"   C++    .pdf"];
    
    NSString *emailBody = @"<font color='red'>eMail</font>   ";
    //      ,   ,      ,    html  
    [mailPicker setMessageBody:emailBody isHTML:YES];
    [self presentModalViewController: mailPicker animated:YES];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    [self dismissViewControllerAnimated:YES completion:^(){
        NSLog(@"        ");
    }];
    NSString *msg;
    switch (result) {
        case MFMailComposeResultCancelled:
            msg = @"        ";
            break;
        case MFMailComposeResultSaved:
            msg = @"      ";
            break;
        case MFMailComposeResultSent:
            msg = @"      ,       ,    ";
            break;
        case MFMailComposeResultFailed:
            msg = @"         ";
            break;
        default:
            msg = @"default";
            break;
    }
}
@end

좋은 웹페이지 즐겨찾기