使用MFMailComposeViewController类从iPhone App发送带有IMG标签的HTML电子邮件

时间:2010-07-20 10:51:29

标签: iphone iphone-sdk-3.0 ipad

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

- (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”已添加到我的资源文件夹中,但图像未显示在邮件正文中(仅显示为[?])。 有人可以告诉我我做错了什么,比如图像的路径是错误的还是有更好的方法来做到这一点?

非常感谢。

4 个答案:

答案 0 :(得分:12)

我坚信(根据您的问题)您的图片SG10002_1.jpg位于主要包中 如果是这样,那么下面的代码应该适合你。这是this问题的完全破解。

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

答案 1 :(得分:3)

您不能在邮件中使用具有相关路径的图像,因为它会尝试从收件人邮件客户端查找文件。

您可以使用base64编码对象(google html base64图像)嵌入图像,也可以将图像上传到可公开访问的Web服务器,并从邮件中引用图像的绝对URL,这样收件人的邮件客户端始终可以访问它。

答案 2 :(得分:1)

将其添加为image / jpeg附件。它将显示在邮件的底部,但会显示在签名上方。

还有很多其他潜在的方法,但它们都有点废话。

答案 3 :(得分:0)

以下是为我工作的代码,

类mailClass =(NSClassFromString(@“MFMailComposeViewController”));

        if (mailClass != nil)
        {

            // We must always check whether the current device is configured for sending emails


            UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();
            MFMailComposeViewController *composeVC = [[MFMailComposeViewController alloc] init];
            composeVC.mailComposeDelegate = self;
            [composeVC setSubject:@"test"];
            NSString *messageBody = @"";
            [composeVC setMessageBody:messageBody isHTML:NO];
            UIImage *artworkImage = viewImage;
            NSData *artworkJPEGRepresentation = nil;
            if (artworkImage)
            {
                artworkJPEGRepresentation = UIImageJPEGRepresentation(artworkImage, 0.7);
            }
            if (artworkJPEGRepresentation) 
            {
                [composeVC addAttachmentData:artworkJPEGRepresentation mimeType:@"image/jpeg" fileName:@"Quote.jpg"];
            }

            NSString *emailBody = @"Find out more  App at <a href='http://itunes.apple.com/us/artist/test/id319692005' target='_self'>Test</a>";//add code
            const char *urtfstring = [emailBody UTF8String];
            NSData *HtmlData = [NSData dataWithBytes:urtfstring length:strlen(urtfstring)];
            [composeVC addAttachmentData:HtmlData mimeType:@"text/html" fileName:@""];
            //Add code
            [self presentModalViewController:composeVC animated:YES];
            [composeVC release];
            [self dismissModalViewControllerAnimated:YES];
            UIGraphicsEndImageContext();