从应用内发送电子邮件中的图像和文字

如何在我的应用程序内的电子邮件中发送图像以及表格数据forms的文本?

请帮忙,提出build议。 谢谢。

- (void)sendMailWithImage:(UIImage *)image { if([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init]; if(mailController!=nil) { mailController.mailComposeDelegate = self; NSData *imageData = UIImagePNGRepresentation(image); [mailController addAttachmentData:imageData mimeType:@"image/png" fileName:@"MyImageName"]; [mailController setSubject:yourSubject]; [mailController setMessageBody:yourBody isHTML:NO]; [self presentModalViewController:mailController animated:YES]; [mailController release]; } else { //Do something like show an alert } } 

希望这可以帮助

看看MessageComposer示例应用程序。 基本上你使用addAttachmentData:mimeType:fileName:

这是从MessageComposer应用程序:

 NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"]; NSData *myData = [NSData dataWithContentsOfFile:path]; [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"]; 

您可以发送图像作为附件,使用MFMailComposerController发送邮件。

 -(void)displayComposerSheet { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"Test Subject"]; // Attach an image to the email NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",imageName] ofType:@"png"]; NSData *myData = [NSData dataWithContentsOfFile:path]; [picker setMessageBody:body isHTML:NO]; if (picker != nil) { [self presentModalViewController:picker animated:YES]; [picker release]; } } 

您使用MFMailComposerController类允许用户撰写和发送邮件。 您可以使用addAttachmentData:mimeType:fileName:方法以及使用setMessageBody:isHTML:方法的消息正文(纯文本或HTML)附加图像和其他文件。

请注意,目前没有办法使用multipart/related在HTML中包含图像,您必须使用data: URI (所有客户端不支持)或外部服务器上的图像(也不受所有客户端的支持,以保护隐私原因)。 或者,当然,完全绕过苹果,并通过与自己的服务器的谈话发送邮件。

您可以使用Apple的MFMailComposeViewController从iOS应用程序发送邮件。 它的官方文件在这里 。 它的用法

  1. 将MessageUI.framework添加到您的项目
  2. 导入必要的头文件

      #import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h> 
  3. 要发送邮件,请打开MFMailComposerController

     if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *ctrller = [[MFMailComposeViewController alloc] init]; ctrller.mailComposeDelegate = self; [ctrller setSubject:@"Subject Goes Here."]; [ctrller setMessageBody:@"Your message goes here." isHTML:NO]; [self presentModalViewController:ctrller animated:YES]; [ctrller release]; //if not using ARC } else { NSLog(@Device is unable to send email in its current state.); } 
  4. 如果你想附加数据,你可以使用addAttachmentData:方法

     [ctrller addAttachmentData:YOUR_DATA_IN_NSDATA_FORMAT mimeType:YOUR_MIME_TYPE fileName:YOUR_ATTACHEMENT_FILENAME];