使用MFMailComposeViewController类从iPhone应用程序发送带有IMG标记的HTML电子邮件
我正在使用MFMailComposeViewController类从我的iPhone应用程序发送格式化的HTML电子邮件。我需要在电子邮件中包含一个图像,并将am IMG标签添加到我的电子邮件正文中使用MFMailComposeViewController类从iPhone应用程序发送带有IMG标记的HTML电子邮件,iphone,iphone-sdk-3.0,ipad,Iphone,Iphone Sdk 3.0,Ipad,我正在使用MFMailComposeViewController类从我的iPhone应用程序发送格式化的HTML电子邮件。我需要在电子邮件中包含一个图像,并将am IMG标签添加到我的电子邮件正文中 - (IBAction)shareWithOther { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = se
- (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];
}
-(iAction)与其他人共享
{
MFMailComposeViewController*选择器=[[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate=self;
[picker setSubject:@“我的邮件主题”];
NSString*emailBody=@“一些,然后是图像和更多文本。”;
[picker setMessageBody:emailBody isHTML:YES];
[自我呈现ModalviewController:picker动画:是];
[选择器释放];
}
图像文件“SG10002_1.jpg”已添加到我的资源文件夹中,但该图像未显示在邮件正文中(仅显示为[?])。
有人能告诉我我做错了什么,比如图像的路径是错误的,或者有更好的方法吗
非常感谢。您不能在邮件中使用具有相对路径的图像,因为这样会尝试从收件人邮件客户端查找文件
您可以使用base64编码对象(google html base64图像)嵌入图像,也可以将图像上载到可公开访问的web服务器,并从邮件中引用图像的绝对URL,这样,收件人的邮件客户端就可以始终访问它。您不能在邮件中使用这样的相对路径的图像,因为这样会尝试从收件人的邮件客户端查找文件
您可以使用base64编码对象(google html base64图像)嵌入图像,也可以将图像上载到可公开访问的web服务器,并引用邮件中图像的绝对URL,这样收件人的邮件客户端就可以始终访问图像。将其添加为图像/jpeg附件。它将出现在您的邮件底部,但在签名上方
还有很多其他可能的方法,但它们都有点垃圾。将其添加为图像/jpeg附件。它将出现在您的邮件底部,但在签名上方
还有很多其他可能的方法,但它们都有点垃圾。这是为我工作的代码 类mailClass=(NSClassFromString(@“MFMailComposeViewController”)
if(mailClass!=nil)
{
//我们必须始终检查当前设备是否配置为发送电子邮件
UIImage*viewImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
MFMailComposeViewController*composeVC=[[MFMailComposeViewController alloc]init];
composeVC.mailComposeDelegate=self;
[composeVC设置主题:@“测试”];
NSString*messageBody=@;
[composeVC setMessageBody:messageBody isHTML:NO];
UIImage*artworkImage=viewImage;
NSData*ArtWorkJPEG表示=零;
如果(artworkImage)
{
ArtWorkJPEG表示法=UIImageJPEG表示法(artworkImage,0.7);
}
if(艺术表现)
{
[composeVC addAttachmentData:ArtworkJPEG表示mimeType:@“image/jpeg”文件名:@“Quote.jpg”];
}
NSString*emailBody=@“查找更多应用程序”;//添加代码
常量字符*urtfstring=[emailBody UTF8String];
NSData*HtmlData=[NSData数据带字节:urtfstring长度:strlen(urtfstring)];
[composeVC addAttachmentData:HtmlData mimeType:@“text/html”文件名:@”“;
//添加代码
[self-presentModalViewController:composeVC动画:是];
[composeVC发布];
[自我解散Modalviewcontrolleranimated:是];
UIGraphicsSendImageContext();
这是为我工作的代码
类mailClass=(NSClassFromString(@“MFMailComposeViewController”)
if(mailClass!=nil)
{
//我们必须始终检查当前设备是否配置为发送电子邮件
UIImage*viewImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
MFMailComposeViewController*composeVC=[[MFMailComposeViewController alloc]init];
composeVC.mailComposeDelegate=self;
[composeVC设置主题:@“测试”];
NSString*messageBody=@;
[composeVC setMessageBody:messageBody isHTML:NO];
UIImage*artworkImage=viewImage;
NSData*ArtWorkJPEG表示=零;
如果(artworkImage)
{
ArtWorkJPEG表示法=UIImageJPEG表示法(artworkImage,0.7);
}
if(艺术表现)
{
[composeVC addAttachmentData:ArtworkJPEG表示mimeType:@“image/jpeg”文件名:@“Quote.jpg”];
}
NSString*emailBody=@“查找更多应用程序”;//添加代码
常量字符*urtfstring=[emailBody UTF8String];
NSData*HtmlData=[NSData数据带字节:urtfstring长度:strlen(urtfstring)];
[composeVC addAttachmentData:HtmlData mimeType:@“text/html”文件名:@”“;
//添加代码
[self-presentModalViewController:composeVC动画:是];
[composeVC发布];
[自我解散Modalviewcontrolleranimated:是];
UIGraphicsSendImageContext();
从您的问题来看,我坚信您的图像SG10002\u 1.jpg
位于主捆绑包中。
若真是这样,那个么下面的代码应该适合你们。这是一个完整的问题
- (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];
}
-(无效)创建电子邮件{
//为电子邮件正文创建具有HTML格式的字符串
NSMutableString*emailBody=[[NSMutableString alloc]initWithString:@']retain];
//添加一些你想要的文字
[emailBody appendString:@“某些电子邮件正文
- (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];
}