【问题标题】:Send email from iOS app using SendGrid使用 SendGrid 从 iOS 应用程序发送电子邮件
【发布时间】:2013-10-16 13:51:51
【问题描述】:

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

谁能告诉我上传图片时出了什么问题?

谢谢

【问题讨论】:

    标签: image ios7 afnetworking email-attachments sendgrid


    【解决方案1】:

    只是稍微修改了您的代码。发送的参数和 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];
    

    【讨论】:

    • 感谢您的链接。我不知道 AFNetworking v2.0 。实际上我的帐户不是由 SendGrid 提供的,这就是我无法发送电子邮件的原因。
    • 很高兴为您配置帐户
    • 请看我的更新。现在我可以发送电子邮件,但是如果我将图像添加为附件,则会出现一些问题。你能告诉我出了什么问题吗?谢谢。
    • +1 用于库。感谢新图书馆...我非常感谢您的帮助。在我的代码中,唯一的问题是files。将其更改为 files[abc.png] 解决了我的问题。
    • 我可以从使用 SendGrid 发送的电子邮件正文中删除 If you'd like to unsubscribe and stop receiving these emails click here 行吗?如果没有,如果用户点击 unsubscribe 会发生什么?我希望我的用户收到这些电子邮件,因为它包含订单信息和付款信息。谢谢
    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 2021-08-13
    • 2018-11-08
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多