【发布时间】:2011-04-19 12:32:47
【问题描述】:
如何在我的应用程序内通过电子邮件发送图像和文本(表格数据形式)?
请帮助并提出建议。谢谢。
【问题讨论】:
标签: ios objective-c cocoa-touch ios4 iphone-sdk-3.0
如何在我的应用程序内通过电子邮件发送图像和文本(表格数据形式)?
请帮助并提出建议。谢谢。
【问题讨论】:
标签: ios objective-c cocoa-touch ios4 iphone-sdk-3.0
- (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: URIs(并非所有客户端都支持)或外部服务器上的图像(并非所有客户端都支持) ,出于隐私原因)。或者,当然,完全绕过 Apple 并通过与您自己的服务器对话来发送邮件。
【讨论】:
您可以使用 Apple 的 MFMailComposeViewController 从 iOS 应用程序发送邮件。它的官方文档是here。它的用法
导入必要的头文件
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
要发送邮件,请打开 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.);
}
如果要附加数据可以使用addAttachmentData:方法
[ctrller addAttachmentData:YOUR_DATA_IN_NSDATA_FORMAT
mimeType:YOUR_MIME_TYPE
fileName:YOUR_ATTACHEMENT_FILENAME];
【讨论】: