<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by cketti on Medium]]></title>
        <description><![CDATA[Stories by cketti on Medium]]></description>
        <link>https://medium.com/@cketti?source=rss-c50fca12222f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*SCY1lRcoq7mEfM0b.jpeg</url>
            <title>Stories by cketti on Medium</title>
            <link>https://medium.com/@cketti?source=rss-c50fca12222f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 05:49:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cketti/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Android: Sending Email using Intents]]></title>
            <link>https://medium.com/@cketti/android-sending-email-using-intents-3da63662c58f?source=rss-c50fca12222f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3da63662c58f</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[cketti]]></dc:creator>
            <pubDate>Fri, 08 Jan 2016 07:56:18 GMT</pubDate>
            <atom:updated>2017-09-17T23:36:47.750Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc-nd/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Many things on Android are as easy as starting an Activity using the right Intent. Sending an email to a specific recipient is one of those things.</p><p>Sadly, there is <a href="http://stackoverflow.com/questions/2197741/how-can-i-send-emails-from-my-android-application">much</a> <a href="http://www.mkyong.com/android/how-to-send-email-in-android/">bad</a> <a href="http://www.tutorialspoint.com/android/android_sending_email.htm">advice</a> out there on how to send emails using Intents. Because I write an open source <a href="https://github.com/k9mail/k-9">email app</a> I have a pretty good understanding of what goes on behind the scenes. That’s why I believe I know better than all those other people :)</p><h4>ACTION_SENDTO</h4><p>To start the Activity to compose an email in your user’s favorite email app you use an Intent with the <a href="https://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO">ACTION_SENDTO</a> action. Unfortunately, the documentation is very sparse. It simply states:</p><blockquote><strong>Activity Action</strong>: Send a message to someone specified by the data.</blockquote><blockquote><strong>Input</strong>: getData() is URI describing the target.</blockquote><blockquote><strong>Output</strong>: nothing.</blockquote><p>This doesn’t mention email at all because ACTION_SENDTO is very generic and can also be used to send e.g. SMS, MMS or XMPP messages.<br>To send an email we have to use a <em>mailto</em> URI as defined by <a href="https://tools.ietf.org/html/rfc6068">RFC 6068</a>. In its simplest form such a URI consists of “mailto:” followed by an email address, e.g. <em>mailto:alice@example.org</em>. You might recognize this syntax from email links in HTML documents.</p><p>Creating such an Intent might look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3577eb8c61f49e3723edd19a0ac668d5/href">https://medium.com/media/3577eb8c61f49e3723edd19a0ac668d5/href</a></iframe><p>Most of the time you want to provide more data than just a recipient. And the <em>mailto</em> specification allows us to also specify CC and BCC recipients, a subject, and even text for the email body.</p><pre>mailto:bob@example.org?cc=alice@example.com&amp;subject=Important%20message&amp;body=Hi%20there</pre><p>Generating such a <em>mailto</em> URI is a bit tedious because you have to <a href="https://tools.ietf.org/html/rfc3986#section-2.1">percent-encode</a> certain characters as you can see in the example above.<br>Android’s <a href="https://developer.android.com/reference/android/net/Uri.Builder.html"><em>Uri.Builder</em></a> class would be the perfect fit since it makes it easy to add query parameters while taking care of the encoding. Unfortunately, building <em>Uri</em> instances doesn’t work very well for non-hierarchical URIs like <em>mailto</em> URIs. One way to work around that is to manually construct the URI and dealing with the encoding.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d94ad5393538b7d8caa8e322090c885/href">https://medium.com/media/2d94ad5393538b7d8caa8e322090c885/href</a></iframe><p><a href="http://stackoverflow.com/a/9462834/1800174">Some</a> <a href="http://stackoverflow.com/a/15022153/1800174">code</a> <a href="http://developer.android.com/guide/components/intents-common.html#Email">samples</a> advocate the use of <a href="https://developer.android.com/reference/android/content/Intent.html#EXTRA_SUBJECT">EXTRA_SUBJECT</a>, <a href="https://developer.android.com/reference/android/content/Intent.html#EXTRA_TEXT">EXTRA_TEXT</a>, etc. Those are certainly easier to use. However, the Android API reference only mentions them in connection with <a href="https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND">ACTION_SEND</a> and <a href="https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND_MULTIPLE">ACTION_SEND_MULTIPLE</a>. Those are more general Intents for sharing content to nobody in particular.<br>The fact that the Extras also work with ACTION_SENDTO was an undocumented “feature” of AOSP Email and the Gmail app. That functionality then had to be copied by all other Android email clients because apps were and are using those Extras.<br>But since all major email clients <a href="https://github.com/cketti/EmailIntentBuilder/wiki/EmailClientCompatibilityList">support</a> the use of query parameters in <em>mailto</em> URIs I see no reason to use an undocumented feature and advice against it.</p><p>It is also worth mentioning that you shouldn’t use <a href="https://developer.android.com/reference/android/content/Intent.html#createChooser(android.content.Intent, java.lang.CharSequence)">Intent.createChooser()</a> to launch an email Intent. Users most likely have a preferred email app that they want to be able to select as default app. The standard behavior when resolving an Intent deals with that just fine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*IzGfmb0dvfCbDFQtJvo8VQ.png" /><figcaption>Allow users to select a default app</figcaption></figure><p>There are a couple of other things that need to be considered. Line breaks in the body, for example, need to be encoded as <em>%0D%0A</em> (CRLF).<br>Because it’s no fun to deal with all of that when all you want to do is send an email, I wrote a small library to take care of the dirty details for you.</p><h4>EmailIntentBuilder — Making life easier</h4><p>The goal of the Email Intent Builder library is to make creating email Intents as easy as possible.<br>You can find it on <a href="http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22de.cketti.mailto%22%20AND%20a%3A%22email-intent-builder%22">Maven Central</a>, the <a href="https://github.com/cketti/EmailIntentBuilder">source code</a> on GitHub.</p><p>Building an email Intent is a simple matter of calling some methods on the builder:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0f82cfaf57d29a7b70e10aca1a6c0ac/href">https://medium.com/media/e0f82cfaf57d29a7b70e10aca1a6c0ac/href</a></iframe><p>Most of the time you also want to launch the Intent. The <em>start()</em> method will do that for you while also taking care of not throwing an exception if no app could be found to handle the Intent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c933cae298684ad514b9623174533bc/href">https://medium.com/media/5c933cae298684ad514b9623174533bc/href</a></iframe><h4>What about Attachments?</h4><p>Unfortunately, the ACTION_SENDTO Intent doesn’t support attachments. If you need to send an email containing one or more attachments you should use Android’s more generic share mechanism, i.e. ACTION_SEND or ACTION_SEND_MULTIPLE.<br>A lot of people have realized that this leads to quite a few more than just email apps showing up in the app chooser dialog. How to properly deal with that is material for another post.</p><p>I’d love to hear about your experiences dealing with email on Android. Let me know on <a href="https://twitter.com/cketti">Twitter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3da63662c58f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>