{"id":299,"date":"2011-01-10T19:48:28","date_gmt":"2011-01-10T18:48:28","guid":{"rendered":"http:\/\/www.albertopasca.it\/whiletrue\/?p=299"},"modified":"2018-05-25T17:37:22","modified_gmt":"2018-05-25T15:37:22","slug":"objective-c-send-mail-with-attachments","status":"publish","type":"post","link":"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/","title":{"rendered":"[Objective-C] Send mail with attachments"},"content":{"rendered":"<p>Hi all,<br \/>\nwith these lines of code you can use\u00a0<strong>Apple iOS Mail system<\/strong>\u00a0to send an email\u00a0<strong>with an attachment<\/strong>!<\/p>\n<p>It\u2019s so easy that i don\u2019t know this method!<\/p>\n<p><!--more--><\/p>\n<p>First, import\u00a0<strong>MessageUI<\/strong>\u00a0framework, after include in your header file (.h) the import to the framework:<\/p>\n<p>[code lang=&#8221;java&#8221; autolinks=&#8221;false&#8221; collapse=&#8221;false&#8221; firstline=&#8221;1&#8243; gutter=&#8221;true&#8221; htmlscript=&#8221;false&#8221; light=&#8221;false&#8221; padlinenumbers=&#8221;false&#8221; smarttabs=&#8221;true&#8221; tabsize=&#8221;4&#8243; toolbar=&#8221;false&#8221;]#import messageui\/MessageUI.h[\/code]<\/p>\n<p>and set the\u00a0<em>delegate<\/em>\u00a0to<\/p>\n<p>[code lang=&#8221;java&#8221; autolinks=&#8221;false&#8221; collapse=&#8221;false&#8221; firstline=&#8221;1&#8243; gutter=&#8221;true&#8221; htmlscript=&#8221;false&#8221; light=&#8221;false&#8221; padlinenumbers=&#8221;false&#8221; smarttabs=&#8221;true&#8221; tabsize=&#8221;4&#8243; toolbar=&#8221;false&#8221;]mfmailcomposeviewcontrollerdelegate[\/code]<\/p>\n<p>Now, create a button in your view and attach\u00a0<em>mailil<\/em>\u00a0function!<\/p>\n<p>[code lang=&#8221;java&#8221; autolinks=&#8221;false&#8221; collapse=&#8221;false&#8221; firstline=&#8221;1&#8243; gutter=&#8221;true&#8221; htmlscript=&#8221;false&#8221; light=&#8221;false&#8221; padlinenumbers=&#8221;false&#8221; smarttabs=&#8221;true&#8221; tabsize=&#8221;4&#8243; toolbar=&#8221;false&#8221;]-\u00a0(void)\u00a0mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:\u00a0(MFMailComposeResult)result error:(NSError*)error\u00a0{<br \/>\n  [self dismissModalViewControllerAnimated:YES];<br \/>\n}<\/p>\n<p>&#8211;\u00a0(IBAction)\u00a0mailIt\u00a0{<br \/>\n  MFMailComposeViewController\u00a0*picker\u00a0=\u00a0[[MFMailComposeViewController alloc]\u00a0init];<br \/>\n  picker.mailComposeDelegate\u00a0=\u00a0self;<\/p>\n<p>  [picker setSubject:@&quot;This is the subject&quot;];<\/p>\n<p>  UIImage\u00a0*roboPic\u00a0=\u00a0[UIImage imageNamed:@&quot;anImage.jpg&quot;];<br \/>\n  NSData\u00a0*imageData\u00a0=\u00a0UIImageJPEGRepresentation(roboPic,\u00a01);<br \/>\n  [picker addAttachmentData:imageData mimeType:@&quot;image\/jpg&quot;\u00a0fileName:@&quot;anImage.jpg&quot;];<\/p>\n<p>  NSString\u00a0*emailBody\u00a0=\u00a0@&quot;This is the email body!!&quot;;<br \/>\n  [picker setMessageBody:emailBody isHTML:YES];<\/p>\n<p>  [self presentModalViewController:picker animated:YES];<br \/>\n  [picker release];<br \/>\n}[\/code]<\/p>\n<p>done!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi all, with these lines of code you can use\u00a0Apple iOS Mail system\u00a0to send an email\u00a0with an attachment! It\u2019s so easy that i don\u2019t know this method!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[233,232,167],"class_list":["post-299","post","type-post","status-publish","format-standard","hentry","category-generic","tag-cs","tag-email","tag-objectivec"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Objective-C] Send mail with attachments - while (true) {\u00a0}<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Objective-C] Send mail with attachments - while (true) {\u00a0}\" \/>\n<meta property=\"og:description\" content=\"Hi all, with these lines of code you can use\u00a0Apple iOS Mail system\u00a0to send an email\u00a0with an attachment! It\u2019s so easy that i don\u2019t know this method!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/\" \/>\n<meta property=\"og:site_name\" content=\"while (true) {\u00a0}\" \/>\n<meta property=\"article:published_time\" content=\"2011-01-10T18:48:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-05-25T15:37:22+00:00\" \/>\n<meta name=\"author\" content=\"Alberto Pasca\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@lupasca\" \/>\n<meta name=\"twitter:site\" content=\"@lupasca\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alberto Pasca\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/objective-c-send-mail-with-attachments\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/objective-c-send-mail-with-attachments\\\/\"},\"author\":{\"name\":\"Alberto Pasca\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"headline\":\"[Objective-C] Send mail with attachments\",\"datePublished\":\"2011-01-10T18:48:28+00:00\",\"dateModified\":\"2018-05-25T15:37:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/objective-c-send-mail-with-attachments\\\/\"},\"wordCount\":220,\"publisher\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"keywords\":[\"cs\",\"email\",\"objectivec\"],\"articleSection\":[\"generic\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/objective-c-send-mail-with-attachments\\\/\",\"url\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/objective-c-send-mail-with-attachments\\\/\",\"name\":\"[Objective-C] Send mail with attachments - while (true) {\u00a0}\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\"},\"datePublished\":\"2011-01-10T18:48:28+00:00\",\"dateModified\":\"2018-05-25T15:37:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/objective-c-send-mail-with-attachments\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/objective-c-send-mail-with-attachments\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/objective-c-send-mail-with-attachments\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Objective-C] Send mail with attachments\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\",\"url\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\",\"name\":\"while (true) {\u00a0}\",\"description\":\"notes from a developer, tutorials, how-to, ideas\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\",\"name\":\"Alberto Pasca\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"caption\":\"Alberto Pasca\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\"},\"description\":\"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.\",\"sameAs\":[\"https:\\\/\\\/www.albertopasca.it\",\"albertopasca\",\"https:\\\/\\\/x.com\\\/lupasca\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Objective-C] Send mail with attachments - while (true) {\u00a0}","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/","og_locale":"en_US","og_type":"article","og_title":"[Objective-C] Send mail with attachments - while (true) {\u00a0}","og_description":"Hi all, with these lines of code you can use\u00a0Apple iOS Mail system\u00a0to send an email\u00a0with an attachment! It\u2019s so easy that i don\u2019t know this method!","og_url":"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/","og_site_name":"while (true) {\u00a0}","article_published_time":"2011-01-10T18:48:28+00:00","article_modified_time":"2018-05-25T15:37:22+00:00","author":"Alberto Pasca","twitter_card":"summary_large_image","twitter_creator":"@lupasca","twitter_site":"@lupasca","twitter_misc":{"Written by":"Alberto Pasca","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/#article","isPartOf":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/"},"author":{"name":"Alberto Pasca","@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"headline":"[Objective-C] Send mail with attachments","datePublished":"2011-01-10T18:48:28+00:00","dateModified":"2018-05-25T15:37:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/"},"wordCount":220,"publisher":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"keywords":["cs","email","objectivec"],"articleSection":["generic"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/","url":"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/","name":"[Objective-C] Send mail with attachments - while (true) {\u00a0}","isPartOf":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#website"},"datePublished":"2011-01-10T18:48:28+00:00","dateModified":"2018-05-25T15:37:22+00:00","breadcrumb":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.albertopasca.it\/whiletrue\/objective-c-send-mail-with-attachments\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.albertopasca.it\/whiletrue\/"},{"@type":"ListItem","position":2,"name":"[Objective-C] Send mail with attachments"}]},{"@type":"WebSite","@id":"https:\/\/www.albertopasca.it\/whiletrue\/#website","url":"https:\/\/www.albertopasca.it\/whiletrue\/","name":"while (true) {\u00a0}","description":"notes from a developer, tutorials, how-to, ideas","publisher":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.albertopasca.it\/whiletrue\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac","name":"Alberto Pasca","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","caption":"Alberto Pasca"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g"},"description":"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.","sameAs":["https:\/\/www.albertopasca.it","albertopasca","https:\/\/x.com\/lupasca"]}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/comments?post=299"}],"version-history":[{"count":3,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":861,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/299\/revisions\/861"}],"wp:attachment":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}