如何在iOS中将图像嵌入电子邮件的HTML正文

7
我试图在iPad发送的HTML邮件正文中包含一张图片,但似乎不可能。我已经尝试使用CID方法,但在iOS中似乎无法获取/设置附件的CID。
我还尝试使用`src="data:image/png;base64, blahblahblah"`嵌入图像。当您撰写邮件时,它似乎可以工作,但当接收邮件时,什么也没有出现。
有什么想法吗?
更多细节: 我们不需要在邮件底部附加JPEG/PNG的解决方案。使用`[composer addAttachmentData:mimeType:fileName:]`很容易实现。 我们正在寻找内联嵌入图像在HTML格式的电子邮件中的解决方案。您可以将链接包装在IMG标记周围,以便当收件人点击IMG时,他/她将链接到应用程序的iTunes页面。

这里有同样的问题。你得到了cid吗?怎么得到的?谢谢。 - Ricardo
我在这里发布了一篇关于如何实现此功能的博客:http://blog.tinymission.com/blog/blogengine.web/post/2011/12/29/Inline-Attachments-Using-MFMailComposeViewController.aspx - Hahnemann
你只想发送一张图片还是同时包含文字?也许这篇帖子可以帮到你:https://dev59.com/rkzSa4cB1Zd3GeqPqNkI - Daniele B
@Carlos,你解决了你的问题吗?请告诉我怎么做!我也想在我的应用程序中添加这个功能。谢谢! - MasterRazer
多或少。看看我的回答吧! - Carlos
3个回答

6

github下载NSData+base64类别。

然后进行以下操作:

NSData *imageData = [NSData dataWithContentsOfFile:pathInDocumentDirectory(imagePath)];
NSString *base64String = [imageData base64EncodedString];
NSString *imageString = [NSString stringWithFormat:@"data:image/png;base64,%@", base64String];

最后,将imageString放置在HTML正文中,您想要显示此图像的位置。

希望对您有所帮助!


2
这很接近,但不太可行,因为像GMail这样的一些网络客户端不会呈现带有数据URI的img标签。 :-( 我已经使用一个微小的红点PNG作为数据URI进行了测试,在iPad邮件客户端和Yahoo邮件中,图像显示得很好,但在GMail中失败了(尽管检查原始电子邮件时可以看到base64字符串存在)。 - Ron
我所能做的最好的办法就是让我的IMG标签指向一个PNG图像,该图像托管在外部网站上。但这也不是一个很好的解决方案,因为许多Web客户端默认情况下会阻止外部图像。无论如何,这个base64的解决方案非常接近,所以我很高兴授予赏金。 :-) - Ron
很遗憾,Gmail网页无法解释它:/ - Michal Zaborowski

1

iPhone电子邮件附件

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Hello"];


// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"]; 
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com",           @"third@example.com", nil]; 
NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];  
[picker setBccRecipients:bccRecipients];

// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];

// Fill out the email body text
NSString *emailBody = @"It is raining";
[picker setMessageBody:emailBody isHTML:NO];

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

0

在 Gmail 中显示图片,您需要执行以下操作:

MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
    mailCont.mailComposeDelegate = self; 

    NSMutableString *emailBody = [[NSMutableString alloc] initWithCapacity:20];

    NSString *linkimg = @"https://idrivethru.com/iDriveThruWeb/faces/javax.faces.resource/idrivethru_logo.png?ln=images";

    //Add the image
    [emailBody appendFormat:@"<p><a href = 'https://idrivethru.com/'> <img src='%@' align='centre' alt='iDriveThru.com'> </a></p><br/>", linkimg];

    [emailBody appendString:@"<p>This is an email with an embeded image right <b>above</b> this text</p>"];

    //NSLog(@"%@",emailBody);

    [mailCont setMessageBody:emailBody isHTML:YES];
    [self presentViewController:mailCont animated:YES completion:nil];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接