【问题标题】:IOS JSON sending an email from MandrillIOS JSON 从 Mandrill 发送电子邮件
【发布时间】:2014-09-03 16:47:21
【问题描述】:

我有一个 IOS 应用程序,我想通过 Mandrill 发送电子邮件。我试图实现这一点,但它不起作用,我自己也很困惑。

当按下按钮从 IOS 应用程序发送电子邮件时,我会记录以下错误消息:

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

我的代码是:

    NSString *post = [NSString stringWithFormat:@"{\"key\": \"abcdefg123456\", \"raw_message\": \"From: me@mydomain.com\nTo: me@myotherdomain.com\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);

请告诉我哪里出错了。

【问题讨论】:

    标签: ios json email mandrill


    【解决方案1】:

    您似乎忘记了“内容”后面的 \"。

    尝试如下编写“post”变量:

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

    希望对你有帮助。

    【讨论】:

    • 谢谢,我确实错过了内容之后的 \。我还需要在每个 \n 之前添加一个 \
    猜你喜欢
    • 2015-04-23
    • 2015-08-30
    • 2016-07-04
    • 2013-04-16
    • 2014-05-23
    • 2017-04-30
    • 1970-01-01
    • 2017-04-03
    • 2016-07-05
    相关资源
    最近更新 更多