2014-07-13 32 views
6

我有一個IOS應用程序,我想通過Mandrill發送一封電子郵件。我試圖實現這一點,但它不工作,我迷惑了自己。IOS JSON從Mandrill發送郵件

按下按鈕時,從IOS應用程序,我記錄此錯誤消息發送一封電子郵件:

{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"} 

我的代碼是:

NSString *post = [NSString stringWithFormat:@"{\"key\": \"abcdefg123456\", \"raw_message\": \"From: [email protected]\nTo: [email protected]\nSubject: Some Subject\n\nSome content.}"]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:@"https://mandrillapp.com/api/1.0/messages/send-raw.json"]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 
    NSLog(@"Post: %@", post); 

NSURLResponse *response; 
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSASCIIStringEncoding]; 
NSLog(@"Reply: %@", theReply); 

請讓我知道我錯了。 。

回答

5

看起來你忘了\內容」後 「」。

嘗試寫你的 「後」 變量如下:

NSString *post = [NSString stringWithFormat:@"{\"key\": \"abcdefg123456\", \"raw_message\": \"From: [email protected]\nTo: [email protected]\nSubject: Some Subject\n\nSome content.\"}"]; 

我希望它能幫助

+0

感謝我做在內容後錯過了\我還需要在每個\ n之前添加一個\ – Steve

相關問題