iOS5 Display a MIME message - iphone

I am looking for the best way to display a MIME message to the user. In fact, it is not an email message, it is a newsgroup (NNTP) message that is encapsulated in a MIME body. Currently I am displaying it in an UILabel but there are several problems, such as the quotation on replies and internal links. It would be great to display it similar to the built-in email app from Apple.
Does anyone have some suggestions on this?

Okay, I have erased the MIME header from the message and set up a basic HTML structure using the following code in the ViewDidLoad...
//setting up html body
NSMutableString *body = [[NSMutableString alloc] init];
NSString *head = #"<HTML><HEAD></HEAD><body>";
NSString *footer = #"</body></HTML>";
//css settings
NSMutableString *css = [[NSMutableString alloc] init];
[css appendString:#"word-wrap: break-word;"];
[css appendString:#"font-family: 'Helvetica', Verdana, Arial, serif;"];
[css appendFormat:#"font-size: 11pt;"];
//building html string
[body appendString:head];
[body appendFormat:#"<div style=\"%#\">", css];
NSString *HTMLbody = [MYPLAINTEXTBODYSTRING stringByReplacingOccurrencesOfString:#"\n" withString:#"<br/>"];
[body appendString:HTMLbody];
[body appendString:#"</div>"];
[body appendString:footer];
self.MYSTRINGBODYPROPERTY = body;
... and load the before created body in the UIWebView.
[[[bodyWebView subviews] lastObject] setScrollEnabled:NO];
[bodyWebView loadHTMLString:self.body baseURL:nil];
[bodyWebView sizeToFit];
Now everything looks similar to Apples Mail App. Thanks.

Though I haven't tried it, I would guess a WebView could be best to handle this.

Related

MFMailComposer place Thanks,Regards text after attachment image

Hi I'm using MFMailComposer to send mail in my application, where i'm attaching an image and in body html content is there and finally i'm adding Thanks,Regards message to mail but all the text content including thanks,Regards is coming as one set then followed by my image attachment then sent from my iPhone signature text is coming.
I want to place thanks,regards text before sent from my iPhone signature text,how can i achieve this?
I have used NSData+Base64 by Matt Gallagher for converting image into base64 so add in your project:
Firstly create emailBody like this:
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:#"<html><body>"] ;
[emailBody appendString:#"<p>Check Attachment</p>"];
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
[emailBody appendString:[NSString stringWithFormat:#"<p><b>%#</b></p>",yourString]];// yourString after image here
//close the HTML formatting
[emailBody appendString:#"</body></html>"];
Use like this
[MFMailDialog setMessageBody:emailBody isHTML:YES];
Credit goes to this answer.

EXC_BAD_ACCESS In Sending Mail With Attachement in iPhone

I have create program to send mail with my some detail from sqlite database
It is working fine if I am not using attachment but if i uncomment that like i m getting exception like this:
Thread 1: EXC_BAD_ACCESS(code=1, address=0x474e5091
here is My code:
MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
mailView.mailComposeDelegate = (id) self;
NSArray *recipients = [NSArray arrayWithObject:#"chintan_zwt#yahoo.com"];
[mailView setToRecipients:recipients];
[mailView setSubject:#"Try Mail From User Application"];
NSString *body = [[NSString alloc] initWithFormat:#"First Name : %# <br>",lblFirstName.text];
body = [body stringByAppendingFormat:#"Last Name : %#<br>",lblLastName.text];
body = [body stringByAppendingFormat:#"Username : %#<br>",lblUsername.text];
body = [body stringByAppendingFormat:#"Password : %#<br>",lblPassword.text];
body = [body stringByAppendingFormat:#"Birthdate : %#<br>",lblBD.text];
[mailView addAttachmentData:[UIImagePNGRepresentation(u.Img1) bytes]
mimeType:#"image/png"
fileName:#"a.png"];
[mailView setMessageBody:body isHTML:YES];
[self presentModalViewController:mailView animated:YES];
I m getting data correctly from database and i m able to display image on screen but problem is only when i attach image to mail
Here in
UIImagePNGRepresentation(u.Img1)
U is an object of User Class (user define class) and Img1 is object of UIImage
First of the addAttachmentData:mimeType:fileName: method need a NSData object, so there is not need o call the bytes method on the NSData.
If you try this in you code you can check wether the UIImagePNGRepresentation() method returns a valid NSData object. Just place a breakpoint on the line and check what the values are in the debugger.
NSData *imageDate = UIImagePNGRepresentation(u.Img1);
[mailView addAttachmentData:imageDate mimeType:#"image/png" fileName:#"a.png"];

Why MFMailComposer with <img> is not showing the image in mail?

I am sending some images in mail using MFMailComposer. I am converting the image to Base64 and using <img> tag to add images to the HTML body(I am not adding it as attachment).
[htmlString appendFormat:
#"<img src='data:image/png;base64,%#' width=300 height=200 />", imageAsBase64];
The images are displaying correctly in MFMailComposer, but there are no images displayed in the actual mail which is sent from the MFMailComposer.
What should I do to make it work?
I had same problem before couple of weeks and I came to know that Gmail is not supporting embedded images. You can see images in email in other mail provider like your domain email but not in Gmail.
Try to send another email and you can see images. You need to add images as attachment then you can see images and it will display bottom of your email body.
Hope this help.
You have to add the images as an attachment. The rendered email that you see with HTML doesn't get rendered properly with the missing image URL.
here is an example: the caveat is that if you want to include things like a PDF you must include an image otherwise mfmailcomposer will fail... this in an apple bug.
I found the solution... Isubmitted a bug on Apple radar about it. MFMailcomposer has a bug in which you have to send an image along with your extra attachments in order to get the weird items like a pdf to work... try this and replace the pdf with your card:
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
NSString *emailSubject = [NSString localizedStringWithFormat:#"MedicalProfile"];
[controller setSubject:emailSubject];
NSString *fileName = [NSString stringWithFormat:#"%#.pdf", profileName];
NSString *saveDirectory = NSTemporaryDirectory();
NSString *saveFileName = fileName;
NSString *documentPath = [saveDirectory stringByAppendingPathComponent:saveFileName];
*** YOU MUST INCLUDE AN IMAGE OR THE PDF ATTATCHMENT WILL FAIL!!!***
// Attach a PDF file to the email
NSData *pdfData = [NSData dataWithContentsOfFile:documentPath];
[controller addAttachmentData:pdfData mimeType:#"application/pdf" fileName:fileName];
// Attach an image to the email
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"miniDoc" ofType:#"png"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
[controller addAttachmentData:imageData mimeType:#"image/png" fileName:#"doctor"];
[controller setMessageBody:[NSString stringWithFormat:#"%#'s Medical Profile attatched!", profileName] isHTML:NO];
[self presentModalViewController:controller animated:YES];
controller.mailComposeDelegate = self;
[controller release];

A better way to create the body of a NSMutableURLRequest

I'm new to objective-c and am wondering if there is a better way that the following to create the HTTP POST body that the method I have below.
- (NSData*) generateBody: (NSDictionary*) requestParams {
NSMutableData *body = [[[NSMutableData alloc] initWithCapacity:1024] autorelease];
BOOL firstParam = YES;
for (NSString* key in requestParams) {
NSString *value = [requestParams valueForKey:key];
if ( firstParam ) {
firstParam = NO;
} else {
[body appendData:[#"&" dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[key dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"=" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
}
return body;
}
Yes.
Have a look at ASIHTTPRequest , specifically, the ASIFormDataRequest class (just search for it in that link)
It's like a better version of NSURLConnection :) It also seems to be becoming the industry standard way to do web requests and an essential part of every iOS developers library of tools :)
If it accepts JSON formatted data try looking at NSJSONSerialization in the Apple docs. Works great.

Emailing HTML from within an iPhone app is stopping at special characters

I have an iPhone app that will let users email some pre-determined text as HTML.
I'm having a problem in that if the text contains special characters within the text (e.g., ampersand &, >, <), the NSString variable that I use for sending the body of the email gets truncated at the special character.
I'm not sure how to fix this (I tried using the method stringByAddingPercentEscapesUsingEncoding…but this hasn't fixed the problems).
Thoughts on what I'm doing wrong / how to fix it?
Here is sample code showing what I'm trying to do
Thanks!!!
- (void)send_an_email:(id)sender {
NSString *subject_string = [NSString stringWithFormat:#"Summary of %#", commercial_name];
NSString *body_string = [NSString stringWithFormat:#"%#<br /><br />", [self.dl email_message]]; // email_message returns the body of text that should be shipped as html. If email_message contains special characters, the text truncates at the special character
NSString *full_string = [NSString stringWithFormat:#"mailto:?to=&subject=%#&body=%#", [subject_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [body_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:full_string]];
}
This works for me to send email from within the app. The way you have it, it quits the app and opens Mail. Try something like this:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
... do your email setup code
[picker setMessageBody:emailBody isHTML:YES];
Here is a good tutorial