Hi guys, in this tutorial I will show you how to send email using iPhone
mail apps. Sending email from mail apps is easy and comfortable. You need to
just use a mailto url + stringByAddingPercentEscapesUsingEncoding + HTML.
Easier way to send e-mail on your iPhone application is using a mailto url.
NSURL *url = [[NSURL alloc] initWithString:@"mailto:me@me.com?subject=subject&body=Hi"];
[[UIApplication sharedApplication] openURL:url];
Of course you can use a format to include your own dynamic text, but the
url string must be url escaped. Cocoa provides a nice way to escape URLs with stringByAddingPercentEscapesUsingEncoding:
So, we can escape URL from email body as following way:
NSString *encodedBody =[eMailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodedBody =[eMailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
The only problem left was representing tabular data. The body parameter of mailto is actually parsed as HTML.
So, amazing as it seems, this code works:
So, amazing as it seems, this code works:
NSString *eMailBody = @"<table><tr><td
style='text-align:right'><b>Name</b>:</td><td>Zubair</td></tr><tr><td
style='text-align:right'><b>Surname</b>:</td><td>Raihan</td></tr><tr><td
style='text-align:right'><b>Occupation:<b/></td><td>Programmer</td></tr></table>";
NSString *encodedBody
=[eMailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString =[NSString stringWithFormat:@"mailto:me@me.com?subject=HiPhone&body=%@",
encodedBody];
NSURL *url = [[NSURL alloc] initWithString:urlString]; [[UIApplication sharedApplication] openURL:url];
That’s it now we are ready for
sending email using mail apps.
Device vs Simulator
Since the iPhone simulator does not have a
Mail.app, you can use e-mail URLs only in the actual device. As a matter of
fact, it’s better to create specific simulator/device code:
#if TARGETIPHONESIMULATOR
//compiler
specific code
#else
// device
specific code
#endif
In my app, I use a UIWebV iew in the simulator
code, so I can preview the email HTML text in the simulator.
Thanks for this post....I didn't know that there is a way to format the mail body....Nice post...keep it up bro...:-)
উত্তরমুছুনthank u..... :)
উত্তরমুছুন