iOS开发之发送邮件

自己的软件里面需要用到,先准备一下知识点。

http://www.cocoachina.com/newbie/basic/2011/1226/3782.html

这篇文章,描述了三种方法


1、使用openURL来实现发邮件的功能:

http://www.istar.name/blog/ios-send-email

2、使用MFMailComposeViewController来实现发邮件的功能,它在MessageUI.framework中,你需要在项目中加入该框架,并在使用的文件中导入MFMailComposeViewController.h头文件。

3、我们可以根据自己的UI设计需求来定制相应的视图以适应整体的设计。可以使用比较有名的开源SMTP协议来实现。

在SKPSMTPMessage类中,并没有对视图进行任何的要求,它提供的都是数据层级的处理,你之需要定义好相应的发送要求就可以实现邮件发送了。至于是以什么样的方式获取这些信息,就可以根据软件的需求来确定交互方式和视图样式了。


这篇文章详细描述了第二种方法,因为提到了附近,而我正好是想要用到附近,所以也加在这里。

1. 添加MessageUI.framework
2. 在头文件中引用并添加委托

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface LoginViewController : UIViewController <MFMailComposeViewControllerDelegate>
@end

3. 实现代码:

#import "LoginViewController.h"
@implementation LoginViewController
//send email
- (IBAction)contact:(id)sender{   
     MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
     mc.mailComposeDelegate = self;
     if ([MFMailComposeViewController canSendMail]) {
         [mc setToRecipients:[NSArray arrayWithObjects:@ "me@istar.name" , nil]];
     [self presentModalViewController:mc animated:YES];
     [mc release];
     }
}
//the delegate
- ( void )mailComposeController:(MFMailComposeViewController*)controller 
           didFinishWithResult:(MFMailComposeResult)result 
                         error:(NSError*)error { 
     switch (result)
     {
         case MFMailComposeResultCancelled:
             NSLog(@ "Mail send canceled..." );
             break ;
         case MFMailComposeResultSaved:
             NSLog(@ "Mail saved..." );
             break ;
         case MFMailComposeResultSent:
             NSLog(@ "Mail sent..." );
             break ;
         case MFMailComposeResultFailed:
             NSLog(@ "Mail send errored: %@..." , [error localizedDescription]);
             break ;
         default :
             break ;
     }
     [self dismissModalViewControllerAnimated:YES];
}
@end

4. 使用说明:

(1)创建视图控制器:

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;

(2)设置邮件主题:

[mc setSubject:@ "Hello, World!" ];

(3)设置收件人,收件人有三种:

//设置主收件人
[mc setToRecipients:[NSArray arrayWithObjects:@ "me@istar.name" , "@dave@iphonedevbook.com" , nil];
//设置cc
[mc setCcRecipients:[NSArray arrayWithObject:@ "me@istar.name" ]];
//设置bcc
[mc setBccRecipients:[NSArray arrayWithObject:@ "me@istar.name" ]];

(4)设置邮件主体,有两种格式。

//一种是纯文本
[mc setMessageBody:@ "Star!!!\n\nCome here, I miss you!" isHTML:NO];
//一个是html格式
[mc setMessageBody:@ "<HTML><B>Hello, Star!</B><BR/>What do you know?</HTML>" isHTML:YES];

(5)添加附件
//添加附件需要三个参数,一个是NSData类型的附件,一个是mime type,一个附件的名称。

NSString *path = [[NSBundle mainBundle] pathForResource:@ "blood_orange" ofType:@ "png" ];
NSData *data = [NSData dataWithContentsOfFile:path];
[mc addAttachmentData:data mimeType:@ "image/png" fileName:@ "blood_orange" ];  

本文固定链接: http://www.istar.name/blog/ios-send-email | Star's Blog


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值