我现在有点卡住了。首先,我对ios& amp; 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];
}
}
我尝试过点这个链接this,this和that,其中基本上涉及这个'边界'和'多个/表单数据'。我确实遵循了这些提示并且我能够收到邮件,但它没有带附件,所以......是的,我需要帮助这个
答案 0 :(得分: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];