使用SendGrid从iOS应用发送电子邮件

时间:2013-10-16 13:51:51

标签: image ios7 afnetworking email-attachments sendgrid

我正在尝试使用来自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);
    }];
} 

你能告诉我上传图片时出错了吗?

由于

2 个答案:

答案 0 :(得分:3)

稍微修改了你的代码。看起来发送的参​​数和URL路径存在问题。

此外,由于您已经在使用AFNetworking发出POST请求,因此您可以按照他们的文档和示例了解如何在此处发送照片:http://cocoadocs.org/docsets/AFNetworking/2.0.1/

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


    NSURL *url = [NSURL URLWithString:@"https://sendgrid.com/api/"];
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: url];
    NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"mail.send.json"  parameters:parameters];
    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: %@", response);
     } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"%@",error);
     }];
    [operation start];

<强>更新**

创建了Sendgrid-ios library,以便更轻松地发送电子邮件和照片附件。

//create Email Object
gridmail *msg = [gridmail user:@"username" andPass:@"password"];

//set parameters
msg.to = @"foo@bar.com";
msg.subject = @"subject goes here";
msg.from = @"me@bar.com";
msg.text = @"hello world";   
msg.html = @"<h1>hello world!</h1>";


//Image attachment
[msg attachImage:self.photo];

//Send email through Web API Transport
[msg sendWithWeb];

答案 1 :(得分:0)

点击此链接,这是一个完美的工作示例代码。 http://sendgrid.com/blog/send-email-in-ios-with-new-sendgrid-library/