【fantomlife】

【道由心生<<>>万道始于足】

   ::  ::  ::  ::  :: 管理

菜鸟:IOS 学习:磨难记(第七天)

一、开发邮件发送DEMO

XCODE4.5 IOS6.0 iphone版由于没有真机环境,模拟器正常,但需要真机测试是否能发送,那位兄弟帮帮忙

二、


简单版,主视图一个发送按钮,
子视图,标题,收件人,CC,BCC, 标题,内容
主视图
子视图
 
发送
发送提示
MailAppViewController. h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h> //发送邮件框架

@interface MailAppViewController : UIViewController<MFMailComposeViewControllerDelegate>

@property(nonatomic, retain) NSString *setSubject; //标题
@property(nonatomic, retain) NSArray *toRecipients; //收件人
@property(nonatomic, retain) NSString *myImagePath; //邮件中的图片
@property(nonatomic, retain) NSString *emailBody; //邮件内容

-(IBAction)openMail:(id)sender;
-(void)setMailDefault;
@end

 

MailAppViewController.m
#import "MailAppViewController.h"

@interface MailAppViewController ()

@end

@implementation MailAppViewController
@synthesize setSubject;
@synthesize toRecipients;
@synthesize myImagePath;
@synthesize emailBody;

- (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.
}

/**
 * Description of Imgauthcode
 * outlet监控视图入口
 * @author fantom
 * @time 2013-01-21
 */
- (IBAction)openMail:(id)sender {
   @try
    {
        [self sendMail];
    }@catch (NSException *e)
    {
        
        [self mailLog:e];
    }
}


/**
 * Description of Imgauthcode
 * 发送邮件
 * @author fantom
 * @time 2013-01-21
 */
-(void)sendMail
{
    @try
    {
        
        if ([MFMailComposeViewController canSendMail])
        {
            MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; //初始化数据
            mailer.mailComposeDelegate = self;
            [self setMailDefault];
            [mailer setSubject:self.setSubject]; //标题
            [mailer setToRecipients:self.toRecipients]; //收件人
            
            UIImage *myImage = [UIImage imageNamed:self.myImagePath];
            NSData *imageData = UIImagePNGRepresentation(myImage);//邮件内容图片
            [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"mobiletutsImage"];
            

            [mailer setMessageBody:self.emailBody isHTML:NO];//邮件内容
            
            [self presentModalViewController:mailer animated:YES]; //显示视图
            [mailer release];
        }
        else //提示无法使用邮箱功能,提示用户
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"失败"
                                                            message:@"您的邮件功能暂时无法使用"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }@catch (NSException *e)
    {
        [self mailLog:e];
    }

}

/**
 * Description of Imgauthcode
 * 配置默认参数
 * @author fantom
 * @time 2013-01-21
 */
-(void)setMailDefault
{
    @try
    {
    self.setSubject = @"测试IOS邮件发送";
    self.toRecipients = [NSArray arrayWithObjects:@"gift33@163.com", @"gift33@sina.com", nil];
    self.myImagePath = @"logo.png";    
    self.emailBody = @"测试一下是否发送成功";
    }@catch (NSException *e) {
        [self mailLog:e];
    }
}

/**
 * Description of Imgauthcode
 *  关闭发送按钮提示
 * @author fantom
 * @time 2013-01-21
 */
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    @try
    {
        NSString *message = @"发送中,请等待";
        switch (result)
        {
            case MFMailComposeResultCancelled:
                message = @"Mail cancelled: 取消了操作,没有电子邮件被排队.";
                break;
            case MFMailComposeResultSaved:
                message = @"Mail saved: 保存在”草稿“文件夹中.";
                break;
            case MFMailComposeResultSent:
                message = @"Mail send: 发送队列中。已准备好发送";
                break;
            case MFMailComposeResultFailed:
                message = @"Mail failed:某个错误无法添加到队列中:无法添加到队列中";
                break;
            default:
                message = @"Mail not sent: 邮箱无法发送.";
                break;
        }
        NSLog(@"%@", message);
        //提示信息
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        // Remove the mail view
        [self dismissModalViewControllerAnimated:YES];
    }@catch (NSException *e) {
        [self mailLog:e];
    }
}

/**
 * Description of Imgauthcode
 * 错误日志处理
 * @author fantom
 * @time 2013-01-21
 */

-(void)mailLog:(NSException *) e
{
    NSLog(@"main : %@ %@", [e name], [e reason]);

}
@end

 

下载包
posted on 2013-01-21 05:57  fantomlife  阅读(203)  评论(0)    收藏  举报