0

我尝试在不使用 MFMailComposeViewController 的情况下发送电子邮件,经过一些研究,我认为 AFNetowrking 是最好的方法,但我无法弄清楚并使其工作,而且我对 Web 编程并不十分熟悉。

我正在使用 AFHTTPClient 类,这是我所做的:

-(IBAction)sendMail
{
    NSURL *url = [NSURL URLWithString:@"http://flameapp.net"];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            txtName, @"si_contact_name1",
                            txtSubject, @"si_contact_subject1",
                            txtEmail, @"si_contact_email1",
                            txtMessage, @"si_contact_message1"
                            ,nil];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/?page_id=26" parameters:params];

    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
                            [NSURL URLWithString:@"http://flameapp.net/?page_id=26"]];

    NSURLResponse *response = [[NSURLResponse alloc] init];
    [client postPath:@"/?page_id=26" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *text = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        NSLog(@"Response: %@", text);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];
}

提前致谢。

4

2 回答 2

1

我会认真地重新考虑不使用 MFMailComposeViewController——它是内置的、熟悉的并且可以工作。

当然,AFNetworking 不能直接发送电子邮件。看起来您正在使用 AFNetworking 与接受参数然后发送电子邮件的服务器进行通信。连接 POST 请求端点由您决定——AFNetworking 的一切看起来都很好(尽管您通常不会为 POST 请求附加查询字符串——这可能只是另一个 POST 参数)

于 2012-06-11T15:10:23.463 回答
0

我不确定您遇到了什么特殊问题,但似乎在成功块内您使用的是自己的(空)response变量,而不是responseObject自动为您创建的变量。

即,如果您执行以下操作,那么您至少会看到服务器返回的内容:

NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
于 2012-07-24T07:04:19.053 回答