0

我现在有点卡住了。首先,我对ios和objective-c有点陌生,我正在尝试实现一个功能,我需要将多个pdf文件发送到一封电子邮件,而不使用mailpickercontroller,而是使用NSURLConnection。截至目前,此代码可以向这些 pdf 文件中的每一个发送邮件,如果有人计划在他们的电子邮件中发送 100 个 pdf,这是非常糟糕的

NSString *defaultEmail = [config objectForKey:@"DEFAULT_EMAIL"];

NSArray *badgeDataLists = [self getBadgeDatas]; //list of nsdata from the pdf file

if ([NSURLConnection instancesRespondToSelector:@selector(originalRequest)] &&  defaultEmail && ![defaultEmail isEqualToString:@""])
{
    NSString * email = defaultEmail;
    NSString * path = [NSString stringWithFormat:@"http://app.receptionsignin.com/mail_notification.php?key=k6j3h5u6nvh3i5jsl60ao5uqjgmso4u1&type=badge&email=%@&for=%@", email, @"All Here to See people", nil];

    for (NSData *badgeData in badgeDataLists){

        NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        [req setHTTPMethod:@"POST"];

         [req setHTTPBody:badgeData];
         [req addValue: @"application/pdf" forHTTPHeaderField:@"Content-Type"];
         [req addValue: [NSString stringWithFormat:@"%d", badgeData.length] forHTTPHeaderField:@"Content-Length"];
        NSLog(@"Content %@", [[NSString alloc] initWithData:[req HTTPBody] encoding:NSUTF8StringEncoding]);
        [NSURLConnection sendAsynchronousRequest:req queue:nil completionHandler:nil];

    }                

}

我已经尝试按照这个链接这个这个那个,基本上它涉及这个“边界”和“多个/表单数据”。我确实遵循了这些提示并且我能够收到邮件,但它没有附带附件,所以......是的,我需要这方面的帮助

4

1 回答 1

0

好吧,提供的链接对我来说不是很有帮助,但我确实设法解决了这个问题。这是解决问题的代码。请注意,badgeDataLists 只是图片的 NSData。之后,您让位于此“路径”上的脚本处理多个文件

  NSString * email = defaultEmail;
    NSString * path = @"some link";
    NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    [req setHTTPMethod:@"POST"];

    NSMutableData* body = [NSMutableData data];

    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int len = 64;
    //Creates a random boundary
    NSMutableString *boundary = [NSMutableString stringWithCapacity: len];

    for (int i=0; i<len; i++) {
        [boundary appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
    }
    [req setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
    NSData *boundaryData = [[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding];
    [body appendData:boundaryData];

    int i = 0;
    for (NSData *badgeData in badgeDataLists){

        // File upload
        [body appendData: [[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"badge[]\"; filename=\"%@\"\r\n\r\n",    [NSString stringWithFormat:@"badge%u.pdf",i++]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/pdf\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData: badgeData]; // It just return NSData with loaded file in it
        if (i != [badgeDataLists count]){
            [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        }
    }
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [req setValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];
    [req setHTTPBody:body];
    [NSURLConnection sendAsynchronousRequest:req queue:nil completionHandler:nil];
于 2013-07-23T19:33:34.767 回答