ios - 如何在电子邮件正文中发送图像

标签 ios image email

<分区>

我正在开发应用程序,我想在其中发送电子邮件正文中的图片。我正在使用以下代码,邮件已发送但图像未显示在图像正文中,任何人都可以帮助我吗?

//Create a string with HTML formatting for the email body
    NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"<html><body>"];
    //Add some text to it however you want
    [emailBody appendString:baseStr];
    //Pick an image to insert
    //This example would come from the main bundle, but your source can be elsewhere
    UIImage *emailImage = [UIImage imageNamed:@"face.png"];
    //Convert the image into data
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
    //Create a base64 string representation of the data using NSData+Base64
    NSString *base64String = [imageData base64EncodedString];
    //Add the encoded string to the emailBody string
    //Don't forget the "<b>" tags are required, the "<p>" tags are optional
    [emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'></b></p>",base64String]];
    //You could repeat here with more text or images, otherwise
    //close the HTML formatting
    [emailBody appendString:@"</body></html>"];
    NSLog(@"%@",emailBody);

    //Create the mail composer window
    MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
    emailDialog.mailComposeDelegate = self;
    [emailDialog setSubject:@"A Message from Emoji App"];
    NSArray *toRecipients = [NSArray arrayWithObjects:@"mandeepz.rimt@gmail.com", nil];
    [emailDialog setToRecipients:toRecipients];
    [emailDialog setMessageBody:emailBody isHTML:YES];


    [self presentViewController:emailDialog animated:YES completion:nil];

最佳答案

试试这个:

NSData *imageData = UIImagePNGRepresentation(imgShare);
        NSString *imageStr = [imageData base64EncodingWithLineLength:0];

        NSString *strHtml = [NSString stringWithFormat:@"<html><body><p>Header: %@</p><p><b><img src='data:image/png;base64,%@'></b></p></body></html>",strTitle,imageStr];

        MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
        [[mailViewController navigationBar] setTintColor:[UIColor blackColor]];
        mailViewController.mailComposeDelegate = self;
        [mailViewController setSubject:AlertTitle];
        [mailViewController setMessageBody:strHtml isHTML:YES];
        mailViewController.modalPresentationStyle=UIModalPresentationFormSheet;
        [vcParent presentViewController:mailViewController animated:YES completion:nil];

关于ios - 如何在电子邮件正文中发送图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23211282/

上一篇:ios - 如何为 Facebook API 验证 UIViewController

下一篇:ios - 使用 Storyboard IOS 7 将 UIviewcontroller 显示为 subview

相关文章:

image - 在 MVC 4 中上传/显示图像

图像扭曲过滤器 - 算法和光栅化

email - Jenkins 附件-语法是什么?

java - 将多个 Intent 用于不同的 Activity 和最终的电子邮件 Intent

ios - 将 componentsseparatedbystring 添加到具有核心数据的谓词中

ios - 如何使用选择器将每个用户的多个图像存储到核心数据中?

ios - ios文件:///var/mobile/Containers and file:///private/var/mobile/Containers?之间有什么区别

linux - 黑客向我的邮件服务器发送垃圾邮件

ios - Coredata 中的关系错误

ios - 为什么当我在描边路径颜色之前填充路径颜色时 CGContext 不绘制任何内容。