Android uses Java Mail API to send emails

Source: Internet
Author: User

Recently, we are considering adding a user feedback function for an existing application, you can use the feedback function to return your comments and suggestions and program problems in a way that is more in line with your habits. On the Internet, there are some well-implemented feedback program services, including bug submission, program suggestions, and program usage problems. However, such services are basically not completely open-source and free, in addition, the positioning of the program feedback is that the user can send his idea back by writing text (without recording the system status during crash ), therefore, we decided to implement the feedback function based on a full understanding of the implementation mechanism.

You do not need to build your own server to send emails, but also meet my feedback management habits. Therefore, you decided to use the feedback function by sending emails in the application.

Response on stackoverflow. Later, we found that GmailSender directly inherits the javax. mail. Authenticator class, implements the getPasswordAuthentication method, and uses Session. getDefaultInstance (props, this )(getDefaultInstance(Properties props, Authenticator authenticator)) Statement to write the authenticator attribute while creating the session.

To send emails within the Android app, note the following two possible modifications:

1. This answer uses the Gmail account as the account for sending emails. If you use another account, you need to separately set the sending server and port number.

For mailbox 126 or 163, you only need to modify the properties settings:

        Properties props = "mail.transport.protocol", "smtp""mail.host""mail.smtp.auth", "true""mail.smtp.port", "25"); 

Reference: http://www.cnblogs.com/peakzheng/archive/2012/02/04/2338671.html

2. The mail sending operation in the UI thread (main thread) needs to be transferred to a separate thread for processing.

After Google released the honeycomb version, it imposes more stringent requirements on the operations in the main thread. To ensure user experience, network data transmission is not allowed in the main thread, because network data transmission in the main thread may cause the main interface to not respond to user clicks for a long time, this results in a poor user experience (reference ). For lightweight network operations, the AsyncTask class is usually used in Android systems.

In a later version, Android prompts android when the application accesses the network on the main interface. OS. networkOnMainThreadException error. However, when using Java Mail API to send emails in the application, I encountered a situation where the program runs on the simulator of Version 2.3, and the receiver can receive the sent email. The same Code cannot be sent successfully on a mobile phone of version 4.2.2, and there is no error message in the logcat interface of Eclipse. The answer on stackoverflow is http://stackoverflow.com/a/2033124/1767800. some people have also commented on it.

However, after the sender. sendMail () function is transferred to the asyncTask class, emails can be sent successfully on the mobile phone 4.2.2. The specific implementation is:

1) create a class that inherits from AsyncTask

 SendMailTask   AsyncTask<URL, Integer, Long>        .sender= totalSize = 0.sender.sendMail("This is Subject""This is Body""senderAddress""receiverAddress"  

2) send an email using the following statement:

             SendMailTask(sender).execute();

Shows the emails received through gmail.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.