本文介绍了使用MFMailComposeViewController类从iPhone App发送带有IMG标签的HTML电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MFMailComposeViewController类从我的iPhone应用程序发送格式化的HTML电子邮件。我需要在电子邮件中包含一个图像,我将IMG标签添加到我的电子邮件中

I am using MFMailComposeViewController class to send out formatted HTML email from my iPhone app. I need to include an image in the email and I added am IMG tag to my emailbody

- (IBAction)shareWithOther
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"My Message Subject"];

    NSString *emailBody = @"<h3>Some and follow by an image</h3><img src=\"SG10002_1.jpg\"/>and then more text.";
    [picker setMessageBody:emailBody isHTML:YES];

    [self presentModalViewController:picker animated:YES];
    [picker release];
}

图像文件SG10002_1.jpg已添加到我的资源文件夹中,但图像没有显示在邮件正文中(仅显示为[?])。
有人可以告诉我我做错了,比如图像的路径是错误还是有更好的方法呢?

the image file, "SG10002_1.jpg" was added to my Resource Folder, but the image didn't show up in the message body (only displayed as [?]).Can someone please tell me what I am doing wrong like if the path of the image is wrong or is there a better way to do this?

谢谢很多。

推荐答案

我坚信(根据你的问题)你的形象 SG10002_1.jpg 位于主包中。

如果是这样,那么下面的代码应该适合你。这是问题的完全黑客。

I strongly believe (from your question) that your image SG10002_1.jpg is located in main bundle.
If it is so, then below code should work for you. It's a complete hack from this question.

- (void)createEmail {
//Create a string with HTML formatting for the email body
    NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain];
 //Add some text to it however you want
    [emailBody appendString:@"<p>Some email body text can go here</p>"];
 //Pick an image to insert
 //This example would come from the main bundle, but your source can be elsewhere
    UIImage *emailImage = [UIImage imageNamed:@"myImageName.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:@"My Inline Image Document"];
    [emailDialog setMessageBody:emailBody isHTML:YES];

    [self presentModalViewController:emailDialog animated:YES];
    [emailDialog release];
    [emailBody release];
}

这篇关于使用MFMailComposeViewController类从iPhone App发送带有IMG标签的HTML电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:30