[url]http://nilvec.com/sending-email-without-user-interaction-in-android/
[/url]
by nilvec
To send emails from an Activity in Android the easiest way is to fire up the built-in mail application with an Intent, and let it take care of the low-level details about how to assemble the message, negotiate a secure connection with the mail server and send it using the proper protocol (as suggested here). However, there may be cases where this is not applicable, like when the mail should be sent in the background (from a Service, for instance) with no user intervention. For some users it may not sound like the best idea to just send out mails on their behalf without explicit permission, but there are legitimate use cases where “silent” emailing is necessary. What SMSForwarder does (automatic forwarding of incoming SMS to a mailbox) is one such case. If anyone is concerned about privacy — it is easy to set up an additional email account and use it for archiving SMS. SMSForwarder does not use the primary Gmail account used by Google apps in Android, the SMTP user and server should be set up directly in the application.
So the question is how to send mails with no user interaction in Android. Since SMTP is not a difficult protocol, it could be tempting to come up with a bunch of simple SMTP client classes and send outgoing emails directly to the mail server of the recipient (technically speaking, to the MX of the domain the user has an email address in). However, there are a few catches. Any reasonable mail server can store emails for temporary network errors, and this is quite difficult to do correctly and efficiently, especially on a mobile device. MX records also have priorities, and in theory they should be used in increasing priority order when sending emails to a domain. There are lots of picky SMTP servers out there, so a simplistic approach may not always be adequate. And, as always, we should not reinvent the wheel.
If one wants to handle SMTP or IMAP in Java the obvious choice of library is javax.mail that was released as open source by Sun some time ago. This is a great package, with outstanding support for SMTP including ESMTP commands like AUTH and STARTTLS — these are required by virtually every mail server if anyone wants to use them as a relay (that is, sending mail through them to an external mail server). It supports MIME too, so one can use it as a full-featured email library — as a matter of fact, javax.mail is *the* library to do emailing in Java. The only drawback is its binding to javax.activation, which in turn needs a handful of AWT classes. Most Android developers are now getting why it is not so straightforward to use javax.mail on Android: since Android implements its own widgets in order to stay away from the somewhat bloated and bulky AWT, either these AWT classes would be needed to be brought to Android, or the dependency on AWT should be eliminated. Some people claim they have done it, but only a couple of jars can be found with no source code, and most developers are having difficulties even just making them work.
The Jakarta Commons Net project also produces an extensive networking library with SMTP support which basically has no external dependencies like javax.mail. Unfortunately, it also has its own catch: it only supports basic SMTP commands and semantics, no ESMTP, thus making it a no-go for any serious mail client (good luck relaying a mail via an average mail server without AUTH or STARTTLS). Nevertheless, this library seemed to be an ideal candidate to extend it with the necessary ESMTP features and make SMSForwarder be able to send mails.
What we did is we implemented the EHLO, AUTH and STARTTLS commands in the commons net library, reusing some code snippets from javax.mail. The currently supported authentication mechanisms are ‘plain’ and ‘login’, but we are also planning to add support for digest-md5 authentication. The patched library is tested with Gmail and a few other mail servers too.
You can find the patch that can be applied on top of the latest stable Jakarta Commons Net library (version 2.0) here. For base64 encoding/decoding the Commons Codec library is used. A conveniently packaged, patched library (with source code) can be downloaded here.
分享到:
相关推荐
Here is the method CTestEmailDlg::OnSend method, which is used to pass the user specified information to the email object and call the IMail::Send method after setting everything. Collapse | Copy ...
This book will teach you to understand how to develop apps which supports calling, sending SMS and email. These are all explained chronologically and in a simple manner for better understanding. For ...
标题 "Some java code for sending email" 提供的信息表明,这个压缩包包含了一些用Java语言编写的用于发送电子邮件的代码。描述中的"send me email at : sunjunbin@gmail.com"是作者提供的一种方式,如果你对这些...
Java Sending Email, java Tutorial
标题中的“这是一款完整的Visual Basic电子邮件发送客户端”表明这是一个使用Visual Basic编程语言开发的应用程序,其功能是允许用户发送电子邮件。在IT领域,这通常涉及到SMTP(Simple Mail Transfer Protocol)...
在给定的标题和描述中提到的"Visual Basic code for Sending email using MAPI control",涉及到的是使用VB通过MAPI(Messaging Application Programming Interface)控件来发送电子邮件的技术。 MAPI是一种在操作...
http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-android-app的源码,已实现可发送邮件。
This book covers Android app design fundamentals in Android Studio using Java programming language. The author assumes you have no experience in app development. The book starts with the installation ...
Defer, schedule, and batch work on the Android system without compromising the battery life and user experience Use the JNI interface to execute concurrent work on native layer Pull and push data from...
在本例中,我们主要关注使用`IMAPI`进行IMAP操作,因为标题提到了`email`、`imap`和`imapi`。 在C#中,由于.NET Framework没有内置对`MAPI`的直接支持,我们需要使用COM Interop来调用Windows的`MAPI`服务。`...
Defer, schedule, and batch work on the Android system without compromising the battery life and user experience Use the JNI interface to execute concurrent work on native layer Pull and push data from...
Read the article Android: Sending Email using Intents if you want to learn what motivated the creation of this library. Usage Creating a simple email intent is as easy as this: Intent emailIntent = ...
Email Sending Failed(解决方案).md
We're doing it like everyone does: sending an e-mail to the user, and waiting for the user to click a link. The only validation we do is to check the e-mail with usual regex... but we need to go a ...
API resolved without sending a response for(解决方案).md
### Oracle 10g 提示 java.lang.Exception: Exception in sending Request :: null 解决方案 在使用Oracle 10g的过程中,部分用户可能会遇到一个特定的问题:在登录企业管理器(Enterprise Manager, EM)时,系统会...
Chapter 8: Android in the Enterprise 137 The basics 138 Understanding the Android ecosystem 138 Device administration capabilities 139 Device administration API 140 Policies 141 DeviceAdminReceiver ...
5. **执行一键boot或ADB命令**:现在你可以尝试再次运行之前导致“sending boot FAILED”错误的一键boot程序,或者使用ADB(Android Debug Bridge)命令行工具来执行刷机操作。由于设备已经处于正确的fastboot状态,...