0

I am working on app, where i wanna sent an image in email body. I am using following code for the same, mail sent but image not shown in image body, Can any one help me for the same.

//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];
Mandeep
  • 107
  • 1
  • 14

2 Answers2

1

Try this:

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];
Pooja M. Bohora
  • 1,311
  • 1
  • 14
  • 42
0

Dear i have same problem in Android when i send image in body of Email it will shown like this "[obj]" and if i send image as attachment then image will send. But not send when i send it as a body.

Prateek Sharma
  • 651
  • 9
  • 18