ios邮件发送

    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

 

你可能感兴趣的:(邮件发送)