1

sendgrid.com のメール API を使用しようとしていますが、毎回失敗ブロックで終了します。また、メールの添付ファイルとして画像を送信する方法もわかりません。以下のコードで何が問題なのか、どうすれば画像を送信できるのか教えてもらえますか? 今のところ以下のコードを使用しています

-(void)sendEmail
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setValue:@"username" forKey:@"api_user"];
    [params setValue:@"sdsfddf23423" forKey:@"api_key"];
    [params setValue:@"test@gmail.com" forKey:@"to"];
    [params setValue:@"test user" forKey:@"toname"];
    [params setValue:@"Test SendGrid" forKey:@"subject"];
    [params setValue:@"Test SendGrid from iOS app" forKey:@"text"];
    [params setValue:@"noreply@gmail.com" forKey:@"from"];

    NSURL *url = [NSURL URLWithString:@"https://sendgrid.com/api"];
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: url];
    NSMutableURLRequest *request = [client requestWithMethod:POST path:@"/mail.send.json"  parameters:params];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        DLog(@"Get latest product info response : %@", response);
        NSLog(@"Success");
    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure");
    }];
    [operation start];
}

前もって感謝します。

アップデート

コードにいくつかの変更を加えましたが、以下のようにメールを正常に送信できるようになりました

-(void)sendEmailWithoutImage
{

     NSDictionary *parameters = @{@"api_user": @"username",
                                 @"api_key": @"sdsfddf23423",
                                 @"subject":@"Test SendGrid",
                                 @"from":@"noreply@gmail.com",
                                 @"to":@"test@gmail.com",
                                 @"text":@"Test SendGrid from iOS app"};

    [[MyAPIClient sharedAPIClient] POST:@"mail.send.json" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject)
    {
        NSLog(@"Success::responseObject : %@", responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"Error::Mail response : %@", error);
    }];
}

しかし、添付ファイルとして画像を送信しようとすると、400 の不正な要求が発生します。したがって、ファイルアップロードブロックにエラーがあると思います。これが私のコードです

-(void)sendEmailWithImage
{
    NSDictionary *parameters = @{@"api_user": @"username",
                                 @"api_key": @"sdsfddf23423",
                                 @"subject":@"Test SendGrid",
                                 @"from":@"noreply@gmail.com",
                                 @"to":@"test@gmail.com",
                                 @"text":@"Test SendGrid from iOS app"};

    [[MyAPIClient sharedAPIClient] POST:@"mail.send.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
    {
        UIImage *image = [UIImage imageNamed:@"redWine.png"];
        NSData *imageToUpload = UIImagePNGRepresentation(image);
        [formData appendPartWithFileData:imageToUpload name:@"files" fileName:[NSString stringWithFormat:@"%@",@"abc.png"] mimeType:@"image/png"];
    }
    success:^(NSURLSessionDataTask *task, id responseObject)
    {
        NSLog(@"Success::responseObject : %@", responseObject);
    }
    failure:^(NSURLSessionDataTask *task, NSError *error)
    {
        NSLog(@"Error::Mail response : %@", error);
    }];
} 

画像のアップロード中に何がうまくいかないのか教えてもらえますか?

ありがとう

4

1 に答える 1