我接近最终的应用程序,最后一步是实现一个功能,我需要将多个文件发送到一个消息。另外我想模仿邮件附件行为,如pict。
我实现了这个目标相反,对于发送多个文件,我有一个问题:只有第一个文件出现在收到的消息上;其他人都输了。
因此,请查看我的代码,尤其是循环循环,以了解可能的错误和/或建议。谢谢你的时间。
- (IBAction)simple3:(id)sender{
NSLog(@"The document: %@", theDocument);
NSString* url = [theDocument absoluteString];
NSLog(@"The document: %@", url);
NSString* from = @"Excited Sundsx <mailgun@xvcvx.com>";
NSString* to = @"sxxxxx@gmail.com";
NSString* subject = @"Attach From Objective-c App";
NSString* text = @"Hello World";
//test
NSImage *myImage = [[NSImage alloc]initWithContentsOfURL:theDocument];
if (myImage != nil)
{
NSLog(@"Image seem to be ok");
}else
NSLog(@"Image seem to be wrong");
NSData *imageData;
NSString *image_name;
NSImage *image;
//-- Convert string into URL
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.mailgun.net/v3/bbbbbbb.me/messages"]];
NSString *authStr = @"api:key-00000000000000000000";
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
//-- Append data into posr url using following method
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"from"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",from] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"to"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",to] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"subject"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",subject] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"text"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",text] dataUsingEncoding:NSUTF8StringEncoding]];
// here loop through mutableArray populated from openPal [openPanel URLs]
lika as pict.
NSLog(@"ARRAYCOUNT: %lu", (unsigned long)filesArrayPath.count);
for (int y = 0; y < [filesArrayPath count]; y++) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
image_name = [filesArrayPath objectAtIndex:y];
NSData *dataImg = [[NSData alloc]initWithContentsOfURL:[filesArrayPath objectAtIndex:y]];
NSLog(@"added %i", y+1);
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"attachment\"; filename=\"%@\"\r\n",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dataImg]];
}
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//-- Sending data into server through URL
[request setHTTPBody:body];
//-- Getting response form server
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//-- JSON Parsing with response data
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Result = %@",result);
}
...有关该怎么做的更多信息,我附上了cUrl中的代码:
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
-F from='Excited User <YOU@YOUR_DOMAIN_NAME>' \
-F to='foo@example.com' \
-F cc='bar@example.com' \
-F bcc='baz@example.com' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!' \
--form-string html='<html>HTML version of the body</html>' \
-F attachment=@files/cartman.jpg \
-F attachment=@files/cartman.png
答案 0 :(得分:1)
for (int y = 0; y < [filesArrayPath count]; y++) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *image_name = [filesArrayPath objectAtIndex:y];
NSData *dataImg = [[NSData alloc]initWithContentsOfURL:[filesArrayPath objectAtIndex:y]];
NSLog(@"added %i", y+1);
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"attachment\"; filename=\"%@\"\r\n",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dataImg]];
}