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

编程入门 行业动态 更新时间:2024-10-25 07:22:37
本文介绍了使用SendGrid从iOS应用程序发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用来自 sendgrid 的邮件api,但每次都是它以失败块结束。 另外,我不明白如何将图像作为附件发送到电子邮件中。在下面的代码中可以告诉我什么是错的吗?我怎么发送图像?我现在使用以下代码

I am trying to to use mail api from sendgrid but everytime it finishes with failure block. Also I don't understand how to send the image as an attachment in the email. Can anybody tell me whats wrong in below code & how can I send image ? I am using below code for now

-(void)sendEmail { NSMutableDictionary *params = [[NSMutableDictionary alloc]init]; [params setValue:@"username" forKey:@"api_user"]; [params setValue:@"sdsfddf23423" forKey:@"api_key"]; [params setValue:@"test@gmail" 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" forKey:@"from"]; NSURL *url = [NSURL URLWithString:@"sendgrid/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]; }

提前致谢。

更新

我在代码和代码中做了一些更改现在我可以成功发送电子邮件,如下所示

I made some changes in code & now I can send the email successfully as below

-(void)sendEmailWithoutImage { NSDictionary *parameters = @{@"api_user": @"username", @"api_key": @"sdsfddf23423", @"subject":@"Test SendGrid", @"from":@"noreply@gmail", @"to":@"test@gmail", @"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坏请求。所以我认为我的文件上传块有一些错误。这是我的代码

But when I try to send the image as attachment then it result in 400 bad request. So I think there is some error in my file uploading block. Here is my code

-(void)sendEmailWithImage { NSDictionary *parameters = @{@"api_user": @"username", @"api_key": @"sdsfddf23423", @"subject":@"Test SendGrid", @"from":@"noreply@gmail", @"to":@"test@gmail", @"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); }]; }

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

Can you anybody tell me whats going wrong while uploading the image ?

谢谢

推荐答案

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

Just modified your code a little. It looks like there was an issue with the parameters being sent and the URL path.

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

Also since you are already using AFNetworking to make your POST request, you can follow their docs and example on how to send a photo over here: cocoadocs/docsets/AFNetworking/2.0.1/

NSDictionary *parameters = @{@"api_user": @"username", @"api_key": @"sdsfddf23423", @"Test SendGrid":@"test", @"from":@"noreply@gmail", @"to":@"test@gmail", @"text":@"Test SendGrid from iOS app"}; NSURL *url = [NSURL URLWithString:@"sendgrid/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库,以便更轻松地发送电子邮件和照片附件。

Created a Sendgrid-ios library to make it easier to send an email and photo attachment.

//create Email Object gridmail *msg = [gridmail user:@"username" andPass:@"password"]; //set parameters msg.to = @"foo@bar"; msg.subject = @"subject goes here"; msg.from = @"me@bar"; msg.text = @"hello world"; msg.html = @"<h1>hello world!</h1>"; //Image attachment [msg attachImage:self.photo]; //Send email through Web API Transport [msg sendWithWeb];

更多推荐

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

本文发布于:2023-10-13 00:21:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1486281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   发送电子邮件   SendGrid   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!