码迷,mamicode.com
首页 > 移动开发 > 详细

ios发送邮件

时间:2015-02-02 22:43:35      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

---恢复内容开始---

方法一:

1.需要引入库MessageUI.framework

#import <MessageUI/MessageUI.h>

#import<MessageUI/MFMailComposeViewController.h>

 

2.@interface ViewController : UIXXXXXViewController <..., MFMailComposeViewControllerDelegate>  

   @end  

3.发送执行代码。事先验证相关支持。 

技术分享
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (!mailClass) {
        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"发送邮件"
                                                         message:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"
                                                        delegate:self
                                               cancelButtonTitle:@"我知道啦"
                                               otherButtonTitles: nil] autorelease];
        [alert show];
        
        return;
    }
    if (![mailClass canSendMail]) {
        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"发送邮件"
                                                         message:@"用户没有设置邮件账户"
                                                        delegate:self
                                               cancelButtonTitle:@"我知道啦"
                                               otherButtonTitles: nil] autorelease];
        [alert show];
        return;
    }
    
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:@"Hello, World!"];
    [mc setToRecipients:[NSArray arrayWithObject:@"xxxxx@163.com"]];
//    [mc setCcRecipients:[NSArray arrayWithObject:@"xxxxx@163.com"]];
//    [mc setBccRecipients:[NSArray arrayWithObject:@"secret@gmail.com"]];
    [mc setMessageBody:@"Hello,slick!!!\n\nCome here, I need you!" isHTML:NO];
 // 添加一张图片
    UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];
    NSData *imageData = UIImagePNGRepresentation(addPic);            // png
    [mc addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
    
    //添加一个pdf附件
    NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
    NSData *pdf = [NSData dataWithContentsOfFile:file];
    [mc addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];
    
    [self presentViewController:mc animated:YES completion:nil];
    [mc release];

 回调函数:

技术分享
- (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 dismissViewControllerAnimated:YES completion:nil];
}
技术分享

方法二:

url方式

技术分享
#pragma mark - 使用系统邮件客户端发送邮件   
-(void)launchMailApp   
{     
    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]];     
}  
技术分享

即 [[UIApplicationsharedApplicationopenURL:[NSURLURLWithString:@"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"]];

 

还可使用skpsmtpmessage这样的第三方控件。

 




---恢复内容结束---

方法一:

1.需要引入库MessageUI.framework

#import <MessageUI/MessageUI.h>

#import<MessageUI/MFMailComposeViewController.h>

 

2.@interface ViewController : UIXXXXXViewController <..., MFMailComposeViewControllerDelegate>  

   @end  

3.发送执行代码。事先验证相关支持。 

技术分享
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (!mailClass) {
        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"发送邮件"
                                                         message:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"
                                                        delegate:self
                                               cancelButtonTitle:@"我知道啦"
                                               otherButtonTitles: nil] autorelease];
        [alert show];
        
        return;
    }
    if (![mailClass canSendMail]) {
        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"发送邮件"
                                                         message:@"用户没有设置邮件账户"
                                                        delegate:self
                                               cancelButtonTitle:@"我知道啦"
                                               otherButtonTitles: nil] autorelease];
        [alert show];
        return;
    }
    
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:@"Hello, World!"];
    [mc setToRecipients:[NSArray arrayWithObject:@"xxxxx@163.com"]];
//    [mc setCcRecipients:[NSArray arrayWithObject:@"xxxxx@163.com"]];
//    [mc setBccRecipients:[NSArray arrayWithObject:@"secret@gmail.com"]];
    [mc setMessageBody:@"Hello,slick!!!\n\nCome here, I need you!" isHTML:NO];
 // 添加一张图片
    UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];
    NSData *imageData = UIImagePNGRepresentation(addPic);            // png
    [mc addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
    
    //添加一个pdf附件
    NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
    NSData *pdf = [NSData dataWithContentsOfFile:file];
    [mc addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];
    
    [self presentViewController:mc animated:YES completion:nil];
    [mc release];

 回调函数:

技术分享
- (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 dismissViewControllerAnimated:YES completion:nil];
}
技术分享

方法二:

url方式

技术分享
#pragma mark - 使用系统邮件客户端发送邮件   
-(void)launchMailApp   
{     
    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]];     
}  
技术分享

即 [[UIApplicationsharedApplicationopenURL:[NSURLURLWithString:@"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"]];

 

还可使用skpsmtpmessage这样的第三方控件。

 




ios发送邮件

标签:

原文地址:http://www.cnblogs.com/dishuichuanshi/p/4268695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!