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

时间:2014-04-22 05:43:18

标签: ios image email

我正在使用app,我想在电子邮件正文中发送图像。我使用以下代码相同,邮件发送但图像未显示在图像正文中,任何人都可以帮助我。

//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];

2 个答案:

答案 0 :(得分:1)

试试这个:

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];

答案 1 :(得分:0)

亲爱的,我在Android中遇到同样的问题,当我在电子邮件正文中发送图像时,它会像这样显示&#34; [obj]&#34;如果我发送图像作为附件,那么图像将发送。但是当我把它作为一个整体发送时不发送。