Sending email without and with HTML formatting in Objective-C

November 16, 2009 § Leave a comment

This is the standard of sending email in Objective-C iPhone App.

Use a mailto url + stringByAddingPercentEscapesUsingEncoding + HTML.

Simple Email without any sentences or any HTML formatting:

NSURL *url = [[NSURL alloc] initWithString;@”mailto:myname@gmail.com?subject=Hello&body=Hi”];
[[UIApplication sharedApplication] openURL:url];

Email with HTML formatting:

NSString *emailBody = @”<table>
<tr><td style=’text-align:right’><b>FirstName</b>:</td>
<td>Michael</td></tr><tr>
<td style=’text-align:right’><b>LastName</b>:</td><td>Jackson</td></tr>
<tr><td style=’text-align:right’>
<b>Job<b/>:</td><td>Artist</td></tr></table>”;
NSString *encodedBody =
[emailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString =
[NSString stringWithFormat:@”mailto:myname@gmail.com?subject=Hello&body=%@”, encodedBody];
NSURL *url = [[NSURL alloc] initWithString:urlString];
[[UIApplication sharedApplication] openURL:url];

iPhone simulator does not have a Mail.app. You can only email url in device. However, you can consider to use compiler statements to create specific simulator/device code. You can use UIAlertView to preview the email string. Consider using UITextView to preview the text and UIWebView to preview HTML in the simulator.

#if TARGETIPHONESIMULATOR
    //compiler specific code
#else
    // device specific code
#endif

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Sending email without and with HTML formatting in Objective-C at Web Builders.

meta

%d bloggers like this: