ios 发送邮件和短信

50 篇文章 0 订阅
30 篇文章 0 订阅

      应用app 很多的时候会涉及到应用程序发送短信和邮件。发送邮件和短信,有两种方法:

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

这种方法很简单:

NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];
    //添加收件人
    NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
    [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
    //添加抄送
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];  
    [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
    //添加密送
    NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];  
    [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
    //添加主题
    [mailUrl appendString:@"&subject=my email"];
    //添加邮件内容
    [mailUrl appendString:@"&body=<b>email</b> body!"];
    NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];  
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];  

缺点就是会让程序短时间的退出,用户体验当然不佳。
2.另一种常见的方法就是使用MFMailComposeViewController来实现发邮件的功能,它在MessageUI.framework中,你需要在项目中加入该框架,并在使用的文件中导入MFMailComposeViewController.h头文件。

代码如下:

-(void)sendMail
{
    Class mailClass =(NSClassFromString(@"MFMailComposeViewController"));
    if (!mailClass)
    {
        GeneralAlertView(nil, @"当前的系统版本不支持应用内邮件功能");
        return;
    }
    if(![mailClass canSendMail])
    {
        GeneralAlertView(nil, @"用户没有设置邮件账户");
        return;
    }
    [self openMail];
}
-(void)openMail
{
     MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];  
    mailPicker.mailComposeDelegate = self;  
    
    //设置主题  
    [mailPicker setSubject: @"eMail主题"];  
    //添加收件人
    NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
    [mailPicker setToRecipients: toRecipients];  
    //添加抄送
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];  
    [mailPicker setCcRecipients:ccRecipients];      
    //添加密送
    NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];  
    [mailPicker setBccRecipients:bccRecipients];  
    
    // 添加一张图片  
    UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];  
    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:@"123.pdf"];
    NSData *pdf = [NSData dataWithContentsOfFile:file];
    [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"123.pdf"];  

    NSString *emailBody = @"<font color='red'>eMail</font> 正文";  
    [mailPicker setMessageBody:emailBody isHTML:YES];  
    [self presentModalViewController: mailPicker animated:YES];  
    [mailPicker release];
}

同理如果是发送短信导入
MFMessageComposeViewController

- (void)messageShare
{
    Class messageClass=NSClassFromString(@"MFMessageComposeViewController");
    NSLog(@"can send SNS %d",[messageClass canSendText]);
    if (messageClass)
    {
        if ([messageClass canSendText])
        {
            [self displaySMS:@“share content”];
        }
        else
        {
            GeneralAlertView(nil, @"你的设备不能发送短信");
        }
    }
    else
    {
        GeneralAlertView(nil, @"iOS版本过低,iOS4.0以上才支持程序内发送短信");
    }
}
-(void)displaySMS
{
MFMessageComposeViewController *messageComposeVC = [[MFMessageComposeViewController alloc] init];
    messageComposeVC.navigationBar.tintColor = [UIColor colorWithRed:86.0/255 green:193.0/255 blue:243.0/255 alpha:1.0];
    messageComposeVC.messageComposeDelegate = self;
    messageComposeVC.body = body;
    messageComposeVC.title = @"么卡短信分享";
    [self presentModalViewController:messageComposeVC animated:YES];
    [messageComposeVC release];
}

这种方法是比较常见的,但是缺点在于只能使用定制的发送界面,没有办法自定义。

3.利用开源代码SKPSMTPMessage

我们可以根据自己的UI设计需求来定制相应的视图以适应整体的设计。可以使用比较有名的开源SMTP协议来实现。在SKPSMTPMessage类中,并没有对视图进行任何的要求,它提供的都是数据层级的处理,你之需要定义好相应的发送要求就可以实现邮件发送了。至于是以什么样的方式获取这些信息,就可以根据软件的需求来确定交互方式和视图样式了。

 
 SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init]; 	
testMsg.fromEmail = @"test@gmail.com"; 		
testMsg.toEmail =@"to@gmail.com"; 		
testMsg.relayHost = @"smtp.gmail.com"; 		
testMsg.requiresAuth = YES; 		
testMsg.login = @"test@gmail.com"; 		
testMsg.pass = @"test"; 		
testMsg.subject = [NSString stringWithCString:"测试" encoding:NSUTF8StringEncoding]; 	
testMsg.bccEmail = @"bcc@gmail.com"; 		
testMsg.wantsSecure = YES;
 // smtp.gmail.com doesn't work without TLS!   		
// Only do this for self-signed certs! 		
// testMsg.validateSSLChain = NO; 
testMsg.delegate = self;   
NSDictionary *plainPart = [NSDictionary  dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, [NSString stringWithCString:"测试正文" encoding:NSUTF8StringEncoding], kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];   NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];
 NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath]; 
  NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys: @"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey, @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey, [vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];  
testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil]; 
 [testMsg send];

该类也提供了相应的Delegate方法来让你更好的获知发送的状态.

-(void)messageSent:(SKPSMTPMessage *)message;
 -(void)messageFailed:(SKPSMTPMessage *)message  error:(NSError *)error;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值