I am writing an application for Android. How can I send an email from it?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Use
.setType("message/rfc822")
or the chooser will show you all of the (many) applications that support the send intent.The strategy of using
.setType("message/rfc822")
orACTION_SEND
seems to also match apps that aren't email clients, such as Android Beam and Bluetooth.Using
ACTION_SENDTO
and amailto:
URI seems to work perfectly, and is recommended in the developer documentation. However, if you do this on the official emulators and there aren't any email accounts set up (or there aren't any mail clients), you get the following error:As shown below:
It turns out that the emulators resolve the intent to an activity called
com.android.fallback.Fallback
, which displays the above message. Apparently this is by design.If you want your app to circumvent this so it also works correctly on the official emulators, you can check for it before trying to send the email:
Find more info in the developer documentation.
Sending email can be done with Intents which will require no configuration. But then it will require user interaction and the layout will be a bit restricted.
Build and sending a more complex email without user interaction entails building your own client. The first thing is that the Sun Java API for email are unavailable. I have had success leveraging the Apache Mime4j library to build email. All based on the docs at nilvec.
I've been using this since long time ago and it seems good, no non-email apps showing up. Just another way to send a send email intent:
This is how I did it. Nice and simple.
I solved this issue with simple lines of code as the android documentation explains.
(https://developer.android.com/guide/components/intents-common.html#Email)
The most important is the flag: it is
ACTION_SENDTO
, and notACTION_SEND
The other important line is
By the way, if you send an empty
Extra
, theif()
at the end won't work and the app won't launch the email client.According to Android documentation. If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the
ACTION_SENDTO
action and include the "mailto:
" data scheme. For example: