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

| 如何在应用程序内的电子邮件中以表格数据的形式发送图像以及文本? 请帮助并提出建议。谢谢。
已邀请:
- (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,并通过与您自己的服务器的对话来发送邮件。
您可以使用Apple的ѭ9来从iOS应用发送邮件。它的官方文档在这里。它的用法 将MessageUI.framework添加到您的项目 导入必要的头文件
   #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];

要回复问题请先登录注册