{"id":2937,"date":"2012-01-06T23:47:13","date_gmt":"2012-01-06T18:47:13","guid":{"rendered":"http:\/\/aasims.wordpress.com\/?p=2937"},"modified":"2025-04-27T19:26:55","modified_gmt":"2025-04-27T19:26:55","slug":"emailsend-application-in-iphone","status":"publish","type":"post","link":"https:\/\/aasimnaseem.com\/blog\/emailsend-application-in-iphone\/","title":{"rendered":"EmailSend Application in iPhone"},"content":{"rendered":"<p>Hello everyone, Hope all is good around you.<\/p>\n<p>Here is another tutorial for iOS applications. I&#8217;m composing a series of tutorials on different topics; you can find the rest of the post under iOS categories.<\/p>\n<p>So here we go; in this application we will see how to send email on an iPhone. So let&#8217;s see how it will work.<\/p>\n<p><!--more--><\/p>\n<p>Step 1: Open Xcode. Create a new project using the View Base application. Give the application \u201cEmailSend\u201d.<\/p>\n<p>Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.<\/p>\n<p>Step 3: Expand classes and notice Interface Builder created the ViewController class for you. Expand Resources and notice the template generated a separate nib, <strong>EmailSendViewController.xib<\/strong> for the EmailSend application.<\/p>\n<p>Step 4: We need to add MessageUI.framework in the application.<\/p>\n<p>Step 5: Open the EmailSendViewController.h file and make the following changes:<\/p>\n<p>[sourcecode language=&#8221;objc&#8221;]<br \/>\n#import &amp;lt;UIKit\/UIKit.h&amp;gt;<br \/>\n#import &amp;lt;MessageUI\/MessageUI.h&amp;gt;<br \/>\n@interface\u00a0EmailSendViewController\u00a0:\u00a0UIViewController<br \/>\n&amp;lt;MFMailComposeViewControllerDelegate&amp;gt;{<br \/>\n}<br \/>\n&#8211;\u00a0(IBAction)SendMail:(id)sender;<br \/>\n@end<br \/>\n[\/sourcecode]<\/p>\n<p>Step 6: Double click the EmailSendViewController.xib file and open it to the interface Builder. Drag the round rect button from the library and place it to the View window. Now select the button and bring up Connection Inspector and connect Touch Up Inside to the File\u2019s Owner icon and select SendMail: method. Now Save the .xib file, close it and go back to the Xcode.<\/p>\n<p>Step 7: Open the AlertViewViewController.m file and make the following changes:<\/p>\n<p>[sourcecode language=&#8221;objc&#8221;]<\/p>\n<p>#import &amp;quot;EmailSendViewController.h&amp;quot;<br \/>\n@implementation\u00a0EmailSendViewController<br \/>\n&#8211;\u00a0(void)dealloc<br \/>\n{<br \/>\n[super dealloc];<br \/>\n}<br \/>\n&#8211;\u00a0(void)didReceiveMemoryWarning<br \/>\n{<br \/>\n\/\/ Releases the view if it doesn\u2019t have a superview.<br \/>\n[super didReceiveMemoryWarning];<br \/>\n\/\/ Release any cached data, images, etc that aren\u2019t in use.<br \/>\n}<br \/>\n#pragma mark \u2013 View lifecycle<br \/>\n\/*<br \/>\n\/\/ Implement viewDidLoad to do additional setup after loading the view, typically from a nib.<br \/>\n&#8211; (void)viewDidLoad<br \/>\n{<br \/>\n[super viewDidLoad];<br \/>\n}<br \/>\n*\/<br \/>\n&#8211;\u00a0(void)viewDidUnload<br \/>\n{<br \/>\n[super viewDidUnload];<br \/>\n\/\/ Release any retained subviews of the main view.<br \/>\n\/\/ e.g. self.myOutlet = nil;<br \/>\n}<br \/>\n-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation<br \/>\n{<br \/>\n\/\/ Return YES for supported orientations<br \/>\nreturn\u00a0(interfaceOrientation\u00a0==\u00a0UIInterfaceOrientationPortrait);<br \/>\n}<br \/>\n&#8211;\u00a0(IBAction)SendMail:(id)sender<br \/>\n{<br \/>\nif\u00a0([MFMailComposeViewController canSendMail])<br \/>\n{<br \/>\nMFMailComposeViewController\u00a0*mailer\u00a0=\u00a0[[MFMailComposeViewController alloc]\u00a0init];<br \/>\nmailer.mailComposeDelegate\u00a0=\u00a0self;<br \/>\n[mailer setSubject:@&amp;quot;A Message from iPhone&amp;quot;];<br \/>\nNSArray\u00a0*toRecipients\u00a0=\u00a0[NSArray\u00a0arrayWithObjects:@&amp;quot;&amp;quot;,\u00a0@&amp;quot;&amp;quot;,\u00a0nil];<br \/>\n[mailer setToRecipients:toRecipients];<br \/>\nNSString\u00a0*emailBody\u00a0=\u00a0@&amp;quot;Have you Nice Day&amp;quot;;<br \/>\n[mailer setMessageBody:emailBody isHTML:NO];<br \/>\n\/\/ only for iPad<br \/>\n\/\/ mailer.modalPresentationStyle = UIModalPresentationPageSheet;<br \/>\n[self presentModalViewController:mailer animated:YES];<br \/>\n[mailer release];<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nUIAlertView\u00a0*alert\u00a0=\u00a0[[UIAlertView alloc]\u00a0initWithTitle:@&amp;quot;Failure&amp;quot;<br \/>\nmessage:@&amp;quot;Your device doesn\u2019t support the composer sheet&amp;quot;<br \/>\ndelegate:nil<br \/>\ncancelButtonTitle:@&amp;quot;OK&amp;quot;<br \/>\notherButtonTitles:\u00a0nil];<br \/>\n[alert show];<br \/>\n[alert release];<br \/>\n}<br \/>\n}<br \/>\n#pragma mark \u2013 MFMailComposeController delegate<br \/>\n&#8211;\u00a0(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:<br \/>\n(MFMailComposeResult)result error:(NSError*)error<br \/>\n{<br \/>\nswitch\u00a0(result)<br \/>\n{<br \/>\ncase\u00a0MFMailComposeResultCancelled:<br \/>\nNSLog(@&amp;quot;Mail cancelled&amp;quot;);<br \/>\nbreak;<br \/>\ncase\u00a0MFMailComposeResultSaved:<br \/>\nNSLog(@&amp;quot;Mail saved&amp;quot;);<br \/>\nbreak;<br \/>\ncase\u00a0MFMailComposeResultSent:<br \/>\nNSLog(@&amp;quot;Mail send&amp;quot;);<br \/>\nbreak;<br \/>\ncase MFMailComposeResultFailed:<br \/>\nNSLog(@&amp;quot;Mail failed&amp;quot;);<br \/>\nbreak;<br \/>\ndefault:<br \/>\nNSLog(@&amp;quot;Mail not sent&amp;quot;);<br \/>\nbreak;<br \/>\n}<br \/>\n[self dismissModalViewControllerAnimated:YES];<br \/>\n}<br \/>\n@end<\/p>\n<p>[\/sourcecode]<\/p>\n<p>Step 8: Now compile and run the application on the simulator.<\/p>\n<p>Thats all. If you found anything confusing, do let me know or comment here.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/s07.flagcounter.com\/count\/rA3\/bg_FFFFFF\/txt_000000\/border_FFFFFF\/columns_6\/maxflags_248\/viewers_0\/labels_1\/pageviews_1\/flags_0\/\" alt=\"free counters\" border=\"0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone, Hope all is good around you. Here is another tutorial for iOS applications. I&#8217;m composing a series of tutorials on different topics; you can find the rest of the post under iOS categories. So here we go; in this application we will see how to send email on&#8230;<\/p>\n","protected":false},"author":1,"featured_media":5171,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[554,560,729,730,731,732,1117],"class_list":["post-2937","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","tag-iphone","tag-iphone-email-tutorial","tag-mfmailcomposeresultcancelled","tag-mfmailcomposeresultfailed","tag-mfmailcomposeresultsent","tag-mfmailcomposeviewcontrollerdelegatemfmailcomposeviewcontrollerdelegate","tag-xcode"],"_links":{"self":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/2937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/comments?post=2937"}],"version-history":[{"count":3,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/2937\/revisions"}],"predecessor-version":[{"id":5172,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/2937\/revisions\/5172"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media\/5171"}],"wp:attachment":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media?parent=2937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/categories?post=2937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/tags?post=2937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}